Problems&Solutions on Android Development for Beginner Version 3

Eclipse adding Javadoc via Shortcut Key

  •  /**[enter] before a class or method will produce documentation comments in Eclipse. 
  • shift-alt-J.
  • You can always configure them via Window->Preferences->Java->Code Style->Code Templates->Comments

Eclipse Generate Javadoc Wizard: what is “Javadoc Command”?

the path to the javadoc command line tool that comes with the JDK (in the bin directory, same as java and javac).
e.g. C:\Program Files\Java\jdk1.7.0_07\bin

TimePicker set Is24HourView not working

 TimePicker picker = (TimePicker) findViewById(;
    Calendar calendar = Calendar.getInstance();

    int h = calendar.get(Calendar.HOUR_OF_DAY);
    int m = calendar.get(Calendar.MINUTE);


calling non-static method in static method in Java

You could create an instance of the class you want to call the method on, e.g.
new Foo().nonStaticMethod();

 * Get the method name for a depth in call stack. <br />
 * Utility function
 * @param depth depth in the call stack (0 means current method, 1 means call method, ...)
 * @return method name
public static String getMethodName(final int depth)
  final StackTraceElement[] ste = Thread.currentThread().getStackTrace();

  //System. out.println(ste[ste.length-depth].getClassName()+"#"+ste[ste.length-depth].getMethodName());
  // return ste[ste.length - depth].getMethodName();  //Wrong, fails for depth = 0
  return ste[ste.length - 1 - depth].getMethodName(); //Thank you Tom Tresansky

I use JRE 6 and gives me incorrect method name.
It works if I write ste[2 + depth].getMethodName().

0 is getStackTrace(),
1 is getMethodName(int depth) and
2 is invoking method.

String.xml String Array

XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
    <string-array name="planets_array">
This application code retrieves a string array:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

How to retrieve 2D array from xml string resource for Android?

The <string-array> element of a resources file can only be used for single dimension arrays. In other words, everything between <item> and </item> is considered to be a single string.

If you want to store data in the way you describe (effectively pseudo-XML), you'll need to get the items as a single String[] using getStringArray(...) and parse the <name> and <codes> elements yourself.

Personally I'd possibly go with a de-limited format such as...

...then just use split(...).
Alternatively, define each <item> as a JSONObject such as...

Android Studio Auto Format shortcut
I set it to automatically clean up on check-in, which is usually good enough for me. If something is too ugly, I'll just hit the shortcut (Ctrl-Alt-L, Return). And I see they have an option for auto-formatting pasted code, although I've never used that.

How to show method parameter
just Ctrl+P


Change Theme 
Ctrl+Alt+S then type appearance
My lovely theme is Darcular.

Full Screen Mode
View/Full Screen Mode

Show editor

Show previous component
F12 // I usually use F12 to swap between editor and logcat.

How can I convert an Integer to localized month name in Java?

Example for locale en-us:
1 -> January
2 -> February
Example for locale es-mx:
1 -> Enero
2 -> Febrero
import java.text.DateFormatSymbols;
public String getMonth(int month) {    return new DateFormatSymbols().getMonths()[month-1];}
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// If the file selection was successful
if (resultCode == RESULT_OK) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();

try {
// Create a file instance from the URI
final File file = FileUtils.getFile(uri);
"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
super.onActivityResult(requestCode, resultCode, data);

Fragment Activity Lifecycle

Though a Fragment's lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such as onResume(), but also important are methods related to interactions with the activity and UI generation.
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.
public class LeakyFragment extends Fragment {
    private View mLeak; // retained
    public void onCreate(Bundle savedInstanceState) {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLeak = inflater.inflate(R.layout.whatever, container, false);
        return mLeak;

    public void onDestroyView() {
        // not cleaning up.

The fix in this example is just changing onDestroyView to

public void onDestroyView() {
    mLeak = null; // now cleaning up!

Popular posts from this blog

เทคนิค ชนะ เกมเศรษฐี 14 ตารวด!!! ( Let's get Rich )

Using Google Chrome Inspect Element to Analyze the website

Easy way to download whole youtube play list and convert them to mp3