May 29, 2013

If you copy the contents of this blog...

If you copy the contents of this blog, please include a LINK to the original post.

May 21, 2013

CalendarView

android.widget.CalendarView, added in API level 11, is a calendar widget for displaying and selecting dates. The range of dates supported by this calendar is configurable. A user can select a date by taping on it and can scroll and fling the calendar to a desired date. To handle user action of changing the date, implement CalendarView.OnDateChangeListener.

android.widget.CalendarView
android.widget.CalendarView


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


package com.example.androidcalendarview;

import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
 
 CalendarView calendar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  calendar = (CalendarView)findViewById(R.id.calendar);
  calendar.setOnDateChangeListener(new OnDateChangeListener(){

   @Override
   public void onSelectedDayChange(CalendarView view, 
     int year, int month, int dayOfMonth) {
    Toast.makeText(getApplicationContext(), 
      "Year: " + year + "\n" +
      "Month: " + month + "\n" +
      "Day of Month: " + dayOfMonth, 
      Toast.LENGTH_LONG).show();
    
   }});
 }

}


May 13, 2013

Implement EditTextPreference in PreferenceFragment

EditTextPreference is a Preference that allows for string input. It is a subclass of DialogPreference and shows the EditText in a dialog. This EditText can be modified either programmatically via getEditText(), or through XML by setting any EditText attributes on the EditTextPreference.

EditTextPreference in PreferenceFragment
EditTextPreference in PreferenceFragment


Works on previous article of "ListPreference example".

Modify /res/xml/preferences.xml to add <EditTextPreference>.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="PreferenceCategory CheckBoxPreference">
        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="summary" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory ListPreference">
        <ListPreference
            android:key="PREF_LIST"
            android:title="ListPreference title"
            android:summary="ListPreference summary"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory Intent">
        <PreferenceScreen
                android:title="Android Coding"
                android:summary="android-coding.blogspot.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://android-coding.blogspot.com/" />
        </PreferenceScreen>
        <PreferenceScreen
                android:title="Android Developers"
                android:summary="developer.android.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://developer.android.com/" />
        </PreferenceScreen>
        <PreferenceScreen
                android:title="Google"
                android:summary="www.google.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://www.google.com/" />
        </PreferenceScreen>
    </PreferenceCategory>
    
    <PreferenceCategory
            android:title="PreferenceCategory EditTextPreference">
        <EditTextPreference
                android:key="PREF_EDITTEXT"
                android:title="EditText title"
                android:summary="EditText summary"
                android:dialogTitle="EditText Dialog" />

    </PreferenceCategory>

</PreferenceScreen>


Modify /res/layout/activity_main.xml to add TextView, setting_edittext, to display the EditTextPreference.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/setpreference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Preference" />
    <TextView
        android:id="@+id/setting_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/setting_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/setting_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Modify MainActivity.java to update preferences once activity returned.
package com.example.androidpreferencefragment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 Button buttonSetPreference;
 TextView settingCheckBox, settingList;
 TextView settingEditText;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonSetPreference = (Button)findViewById(R.id.setpreference);
  settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
  settingList = (TextView)findViewById(R.id.setting_list);
  settingEditText = (TextView)findViewById(R.id.setting_edittext);
  
  buttonSetPreference.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class);
    startActivityForResult(intentSetPref, 0);
   }});
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false);
  settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString());
  String prefList = sharedPreferences.getString("PREF_LIST", "no selection");
  settingList.setText("LIST preference = " + prefList);
  
  String prefEditText = sharedPreferences.getString("PREF_EDITTEXT", "default");
  settingEditText.setText("EDITTEXT preference = " + prefEditText);
 }


}


Keep using PrefFragment.java and PrefActivity.java in the article "PreferenceFragment example for Android 3.0 (HoneyComb)".

May 9, 2013

Open page in PreferenceFragment with intent

Add Preference of <intent> with android:action="android.intent.action.VIEW" and android:data="http://...", user can click the item to open browser to visit the specified web address.

Example:

PreferenceFragment with intent
PreferenceFragment with intent


<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="PreferenceCategory CheckBoxPreference">
        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="summary" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory ListPreference">
        <ListPreference
            android:key="PREF_LIST"
            android:title="ListPreference title"
            android:summary="ListPreference summary"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory Intent">
        <PreferenceScreen
                android:title="Android Coding"
                android:summary="android-coding.blogspot.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://android-coding.blogspot.com/" />
        </PreferenceScreen>
        <PreferenceScreen
                android:title="Android Developers"
                android:summary="developer.android.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://developer.android.com/" />
        </PreferenceScreen>
        <PreferenceScreen
                android:title="Google"
                android:summary="www.google.com">
            <intent android:action="android.intent.action.VIEW"
                    android:data="http://www.google.com/" />
        </PreferenceScreen>
    </PreferenceCategory>

</PreferenceScreen>


May 8, 2013

Display ListPreference selected item on summary

Refer to the article "Implement ListPreference in PreferenceFragment", the default display on summary field is hard-coded in /res/xml/preferences.xml. Such that, the user cannot know the current selected item of ListPreference, without open the ListPreference dialog.

To update summary of ListPreference programmatically, call ListPreference.setSummary().

Display ListPreference selected item on summary
Display ListPreference selected item on summary


Implement updateListPrefSummary_PREF_LIST() to update summary of ListPreference with key="PREF_LIST", call it in onCreate() in fragment start-up. And also implement OnSharedPreferenceChangeListener to call it when ListPreference with key="PREF_LIST" changed. Register and Unregister in onResume() and onPause().

package com.example.androidpreferencefragment;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;

public class PrefFragment extends PreferenceFragment 
 implements OnSharedPreferenceChangeListener{

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  addPreferencesFromResource(R.xml.preferences);
  
  updateListPrefSummary_PREF_LIST();
 }
 
 @Override
 public void onResume() {
  super.onResume();
  getPreferenceScreen().getSharedPreferences()
   .registerOnSharedPreferenceChangeListener(this);
 }
 
 @Override
 public void onPause() {
  super.onPause();
  getPreferenceScreen().getSharedPreferences()
   .unregisterOnSharedPreferenceChangeListener(this);
 }

 //Apply for ListPreference with key="PREF_LIST"
 private void updateListPrefSummary_PREF_LIST(){
  ListPreference preference = (ListPreference)findPreference("PREF_LIST");
  CharSequence entry = ((ListPreference) preference).getEntry();
  preference.setSummary("current selection: " + entry);
 }

 @Override
 public void onSharedPreferenceChanged(
   SharedPreferences sharedPreferences, String key) {
  
  //if changed SharedPreference is ListPreference with key="PREF_LIST",
  // update summary
  if(key.equals("PREF_LIST")){
   updateListPrefSummary_PREF_LIST();
  };
  
 }

}


May 7, 2013

Implement ListPreference in PreferenceFragment

ListPreference A Preference that displays a list of entries as a dialog. This preference will store a string into the SharedPreferences.

ListPreference in PreferenceFragment
ListPreference in PreferenceFragment

Works on previous article of "PreferenceFragment example".

Modify /res/xml/preferences.xml to add <ListPreference>.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="PreferenceCategory CheckBoxPreference">
        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="summary" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory ListPreference">
        <ListPreference
            android:key="PREF_LIST"
            android:title="ListPreference title"
            android:summary="ListPreference summary"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues" />
    </PreferenceCategory>

</PreferenceScreen>


Create /res/values/arrays.xml to define the list items.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="listentries">
        <item>List item 1</item>
        <item>List item 2</item>
        <item>List item 3</item>
        <item>List item 4</item>
    </string-array>
    <string-array name="listvalues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>
     
</resources>


Keep using PrefFragment.java and PrefActivity.java in last article.

Modify MainActivity.java
package com.example.androidpreferencefragment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 Button buttonSetPreference;
 TextView settingCheckBox, settingList;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonSetPreference = (Button)findViewById(R.id.setpreference);
  settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
  settingList = (TextView)findViewById(R.id.setting_list);
  
  buttonSetPreference.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class);
    startActivityForResult(intentSetPref, 0);
   }});
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false);
  settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString());
  String prefList = sharedPreferences.getString("PREF_LIST", "no selection");
  settingList.setText("LIST preference = " + prefList);
 }

}


/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/setpreference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Preference" />
    <TextView
        android:id="@+id/setting_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/setting_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Next:
The default display on summary field is hard-coded in /res/xml/preferences.xml. Such that, the user cannot know the current selected item of ListPreference, without open the ListPreference dialog. To display ListPreference selected item on summary, read it.

May 6, 2013

PreferenceFragment example for Android 3.0 (HoneyComb)

PreferenceFragment was introduce in Android 3.0 (HoneyComb), API level 11.

PreferenceFragment example
PreferenceFragment example

Create /res/xml/preferences.xml to define our preferences:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="PreferenceCategory A">
        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="summary" />
    </PreferenceCategory>

</PreferenceScreen>


PrefFragment.java
package com.example.androidpreferencefragment;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class PrefFragment extends PreferenceFragment {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  addPreferencesFromResource(R.xml.preferences);
 }

}


PrefActivity.java
package com.example.androidpreferencefragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class PrefActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  PrefFragment prefFragment = new PrefFragment();
  FragmentManager fragmentManager = getFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  fragmentTransaction.replace(android.R.id.content, prefFragment);
  fragmentTransaction.commit();
 }

}


MainActivity.java
package com.example.androidpreferencefragment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 Button buttonSetPreference;
 TextView settingCheckBox;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonSetPreference = (Button)findViewById(R.id.setpreference);
  settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
  
  buttonSetPreference.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class);
    startActivityForResult(intentSetPref, 0);
   }});
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false);
  settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString());
 }

}


Layout, /res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/setpreference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Preference" />
    <TextView
        android:id="@+id/setting_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Also need to modify AndroidManifest.xml to add activity of PrefActivity.

Next:
- Implement ListPreference in PreferenceFragment

May 2, 2013

Get class name of a object using Java

To get class name of a object, call its getClass().getName().

Example:

Get class name of a object
Get class name of a object


package com.example.androidclassname;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView info;
 ImageView image;
 LinearLayout screen;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  screen = (LinearLayout)findViewById(R.id.screen);
  info = (TextView)findViewById(R.id.info);
  image = (ImageView)findViewById(R.id.image);
  
  screen.setOnClickListener(onClickListener);
  info.setOnClickListener(onClickListener);
  image.setOnClickListener(onClickListener);
 }

 OnClickListener onClickListener = 
   new OnClickListener(){

    @Override
    public void onClick(View v) {
     
     String className = v.getClass().getName();
     String simpleName = v.getClass().getSimpleName();
     String canonicalName = v.getClass().getCanonicalName();
     
     if(canonicalName == null){
      canonicalName = "null";
     }
     
     String s = "Name: " + className + "\n"
       + "SimpleName: " + simpleName + "\n"
       + "CanonicalName: " + canonicalName + "\n";
     info.setText(s);
    }
  
 };

}


Infolinks In Text Ads