Saturday, June 30, 2012

list view control with collection framework

BeanMatrixDemo.java

package com.beanmatrix.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.*;

public class BeanMatrixDemoActivity extends Activity {
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        ArrayList<String> al = new ArrayList<String>();
        System.out.println("Initial size of al: " +
        al.size());
        // add elements to the array list
        al.add("Milk");
        al.add("Butter");
        al.add("Yogurt");
        al.add("Toothpaste");
        al.add("IceCream");
        // Remove elements from the array list
        al.remove("Toothpaste");
        al.remove(2);
       // display the array list
         ListView listView = (ListView) findViewById(R.id.mylist);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, al);
        listView.setAdapter(adapter);

      }
}


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

Monday, June 18, 2012

Example of Checkbox in android application with Eclipse

File : BeanMatrixDemo/res/values/String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BeanMatrixDemoActivity!</string>
<string name="app_name">BeanMatrixDemo</string>
<string name="btn_display">Display</string>
<string name="chk_java">java</string>
<string name="chk_Asp">ASP.NET</string>
<string name="chk_cpp">C++</string>
</resources>
File : BeanMatrixDemo/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


<CheckBox
    android:id="@+id/chkJava"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_java" />

<CheckBox
        android:id="@+id/chkAsp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_Asp"
        android:checked="true" />

<CheckBox
        android:id="@+id/chkCpp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_cpp" />

<Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>


File:  BeanMatrixDemo/src/com/beanmatrix/demo/beanmatrixactivity.java
package com.beanmatrix.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class BeanMatrixCheckbox extends Activity{
       private CheckBox chkJava, chkAsp, chkCpp;
         private Button btnDisplay;

         @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.checkbox);
              addListenerOnchkJava();
              addListenerOnButton();
         }

         public void addListenerOnchkJava() {
              chkJava = (CheckBox) findViewById(R.id.chkJava);
              chkJava.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                       //is chkJava checked?
                       if (((CheckBox) v).isChecked()) {
                           Toast.makeText(BeanMatrixCheckbox.this,
                               "Bro, try Android :)", Toast.LENGTH_LONG).show();
                     }
                }
              });
         }

         public void addListenerOnButton() {
              chkJava = (CheckBox) findViewById(R.id.chkJava);
                          chkAsp = (CheckBox) findViewById(R.id.chkAsp);
                          chkCpp = (CheckBox) findViewById(R.id.chkCpp);
              btnDisplay = (Button) findViewById(R.id.btnDisplay);
              btnDisplay.setOnClickListener(new OnClickListener() {
                 //Run when button is clicked
                public void onClick(View v) {
                     StringBuffer result = new StringBuffer();
                     result.append("IPhone check : ").append(chkJava.isChecked());
                     result.append("\nAndroid check : ").append(chkAsp.isChecked());
                     result.append("\nWindows Mobile check :").append(chkCpp.isChecke());
                     Toast.makeText(BeanMatrixCheckbox.this, result.toString(),
                                  Toast.LENGTH_LONG).show();
                }

              });
          }
       }


 




Sunday, June 17, 2012

Radio Button Example

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, BeanMatrixDemoActivity!</string>
    <string name="app_name">BeanMatrixDemo</string>
    <string name="radio_male">Male</string>
    <string name="radio_female">Female</string>
    <string name="btn_display">Display</string>
</resources>

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

 </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

File : BeanMatrixDemoActivity.java 

package com.beanmatrix.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class BeanMatrixDemoActivity extends Activity {
    /** Called when the activity is first created. */
         private RadioGroup radioSexGroup;
         private RadioButton radioSexButton;
         private Button btnDisplay;
       @Override
    public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              addListenerOnButton();
    }
     
  public void addListenerOnButton() {
              radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
              btnDisplay = (Button) findViewById(R.id.btnDisplay);
              btnDisplay.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
              // get selected radio button from radioGroup
                          int selectedId = radioSexGroup.getCheckedRadioButtonId();
                             radioSexButton = (RadioButton) findViewById(selectedId);
                           Toast.makeText(BeanMatrixDemoActivity.this,radioSexButton.getText(), Toast.LENGTH_SHORT).show()
               }
               });

        }
}

After Run the application, you will see it as follows,


 and after selecting one of the option, it will look like,


Radio Button in android application

A radio button is a two-state button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it.

Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.


Public Constructors
RadioButton(Context context)
RadioButton(Context context, AttributeSet attrs)
RadioButton(Context context, AttributeSet attrs, int defStyle)


Public Methods
void
onPopulateAccessibilityEvent(AccessibilityEvent event)
void
toggle()