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 |
<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.
Works on previous article of "ListPreference example".
Modify /res/xml/preferences.xml to add <EditTextPreference>.
Modify /res/layout/activity_main.xml to add TextView, setting_edittext, to display the EditTextPreference.
Modify MainActivity.java to update preferences once activity returned.
Keep using PrefFragment.java and PrefActivity.java in the article "PreferenceFragment example for Android 3.0 (HoneyComb)".
![]() |
| 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:
Example:
![]() |
| 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().
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().
To update summary of ListPreference programmatically, call ListPreference.setSummary().
![]() |
| 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();
};
}
}
Subscribe to:
Posts (Atom)



