Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Leap year date time Java Android example


AD MOB

GregorianCalendar cal = new GregorianCalendar(); Boolean b = cal.isLeapYear(2012); // true, Android example.



public class MainActivity extends Activity {
TextView txtV;
	Context cntx;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtV = (TextView)findViewById(R.id.idLabel);
        cntx = this;
        StringBuilder strBuild = new StringBuilder();
        
         GregorianCalendar cal = new GregorianCalendar();
         Boolean b = cal.isLeapYear(2012); // true
         strBuild.append("Is leap year 2012? " + b + "

"); b = cal.isLeapYear(2014); // false strBuild.append("Is leap year 2014? " + b + "
"); txtV.setText(strBuild); } }

397LW NO topic_id




AD

Další témata ....(Topics)


113

Problem in parsing the package Android 2.1 | problem-in-parsing-the-package-android-2-1


Eclipse.
If install on device signed with proguard *.apk package and try to reinstall it by only signed package get error in parsing the package.
Uninstall the first application with bigger size from device and try install signed package which has a smaller size. Delete from download list previous downloaded *.apk.
This problem occurs when that your downloaded apk file size is less than the
size of the original apk.
370

Android Studio failed to convert drawable into a drawable | android-studio-failed-to-convert-drawable-into-a-drawable


Try this:
- check names of drawables (file name must contain only abc...xyz 012...789 _ . in Resources folder ,
names have to start with lower case
MyImage.jpg == problem ,
names with space
my image.jpg == problem,
names with -
my-image.png == problem)
- check duplicate names with other extension ( my_image.jpg - my_image.png makes the problem)
- restart Android Studio
- if problem persist:
- check c:\Users\me\AndroidStudioProjects\myProject\myModule\build\intermediates\res\merged\debug\ drawable folder for corupted names or delete ALL drawable folders
- synk projekt, rebuild projekt
- if problem persist:
- restart Android Studio, wait for complete closure of the Android Studio!
388

Call requires permission which may be rejected by user | call-requires-permission-which-may-be-rejected-by-user


Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`




try {
// your code for example: 
//  LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);         
//  Criteria criteria = new Criteria();
//        provider = locationManager.getBestProvider(criteria, false);
//  locationManager.requestLocationUpdates(provider, 400, 1, this);
        } catch (SecurityException e) {
            e.printStackTrace();
        }


41

Preferences settings save open read write application Android example | preferences-settings-save-open-red-application-android-example


Android app setup preferences, settings, open, read, write, onStop(), onPause(), getSharedPreferences(), SharedPreferences.Editor getInt(), getBoolean(), getString() , putInt(), putString(), putBoolean() example source code.



import android.content.SharedPreferences;

public class MainClass extends Activity {
  public static final String PREFERENCES_NAME = "MyPrefsFile";
  String myNewString;
  int myNewInt;
  Boolean myNewBool;


   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // read old settings if exist
      SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
			
     String sDefault = "Hello!";
      String sStringFromPrefFile = settings.getString("myString", sDefault); // new text  or default Hello!
          
      int nDefaultIndex = 2; //
      int nIndexFromPrefFile = settings.getInt("myInt", nDefaultIndex); // 4 or default 2
			
      Boolean bDefault = false;
      Boolean bFromPrefFile = settings.getBoolean("silentMode", bDefault); // true or default false
        
        
        // new settings will saved in onStop or onPause
        myNewString = "new text";
        myNewInt = 4;
        myNewBool = true;
     }   

    @Override
    protected void onPause() {
    	super.onPause();
        Toast.makeText(getApplicationContext(), "onPause "
        		, Toast.LENGTH_SHORT).show();

    	savePreferences();
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	savePreferences();
    }
    /**save settings*/	
    public void savePreferences(){
    	try {
    		SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
    		SharedPreferences.Editor editor = settings.edit();
 
    		editor.putString("myString", myNewString);
     		editor.putInt("nMyInt", myNewInt);
    		editor.putBoolean("silentMode", myNewBool); // 
    		
    		editor.commit();
    	} catch (Exception e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    }	
}
351

Fragment FragmentBasic.zip null pointer TextView article | fragment-fragmentbasic-zip-null-pointer-textview-article


Example have error code:
//developer.android.com/training/basics/fragments/creating.html

Try to change ArticleFragment.java

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      //www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    TextView articleText;
    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//        Bundle savedInstanceState) {
//
//        // If activity recreated (such as from screen rotate), restore
//        // the previous article selection set by onSaveInstanceState().
//        // This is primarily necessary when in the two-pane layout.
//        if (savedInstanceState != null) {
//            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
//        }
//
//        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.article_view, container, false);
//    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.article_view, container, false);
        articleText = (TextView) rootView.findViewById(R.id.article);
        return rootView;
    }
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            updateArticleView(mCurrentPosition);
        }
    }

    public void updateArticleView(int position) {
        //TextView article = (TextView) getActivity().findViewById(R.id.article); //Error: article=null.
        if (articleText != null)
            articleText.setText(Ipsum.Articles[position]);
        mCurrentPosition = position;
    }
/* ERROR   public void updateArticleView(int position) {
        TextView article = (TextView) getActivity().findViewById(R.id.article);
        article.setText(Ipsum.Articles[position]);
        mCurrentPosition = position;
    }*/

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}


Editace: 2011-09-29 20:44:21
Počet článků v kategorii: 397
Url:leap-year-date-time-java-android-example

AD
11 Share
Share
Tweet