Aug 19, 2013

Cloud save using Play Games services


Game On!: Cloud Save
Join Todd and Bruno from the Play Games team as they share exciting news and updates, and go over some of the finer points of enabling cloud save using Play Games services.

Aug 15, 2013

Implement horizontal and scrollable Radio Button Group

This example implement RadioButton Group horizontally and scrollable.

horizontal and scrollable Radio Button Group
horizontal and scrollable Radio Button Group


<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <RadioGroup
        android:id="@+id/radiogroup_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/normalradiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="normal option 1" />

        <RadioButton
            android:id="@+id/normalradiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="normal option 2" />
    </RadioGroup>
    
    <RadioGroup
        android:id="@+id/radiogroup2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="option 1" />

        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="option 2" />
    </RadioGroup>

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/radiogroup3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radiobuttona"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="option A" />

            <RadioButton
                android:id="@+id/radiobuttonb"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="option B" />

            <RadioButton
                android:id="@+id/radiobuttonc"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="option C" />
            <RadioButton
                android:id="@+id/radiobuttond"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="option D" />

            <RadioButton
                android:id="@+id/radiobuttone"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="option E" />
        </RadioGroup>
    </HorizontalScrollView>

</LinearLayout>


In the example, <RadioGroup> with id=radiogroup_vertical is default vertical RadioGroup. <RadioGroup> with id=radiogroup2 is horizontal RadioGroup with android:orientation="horizontal". <RadioGroup> with id=radiogroup3 is another horizontal RadioGroup, but with 5 option expect more space than device's screen width. So place it in a <HorizontalScrollView> to make it svrollable.

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)".

Infolinks In Text Ads