Showing posts with label OnClick Listener. Show all posts
Showing posts with label OnClick Listener. Show all posts

Sunday, August 19, 2012

Android - Radio Button Example


Below is example of RadioButton, it is used to select option from list of options.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Layout file (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:orientation="vertical" >

    <RadioGroup 
        android:id="@+id/rdogroupsex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RadioButton 
            android:id="@+id/rdomale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdomale"
            />
        <RadioButton 
            android:id="@+id/rdofemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdofemale"
            />
 
    </RadioGroup>
<Button
   android:id="@+id/btnok"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="@string/btnok"
   />

</LinearLayout>

String file(XML file):

<resources>

    <string name="app_name">RadioButtonExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_radio_button_example">Radio Button Example</string>
<string name="rdomale">Male</string>
<string name="rdofemale">Female</string>
<string name="btnok">OK</string>
</resources>

Code base file (.JAVA):

package com.example.radiobuttonexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 RadioButtonExample extends Activity {

private RadioGroup rdoGroup;
private RadioButton rdoMale, rdoFemale;
private Button btnOk;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button_example);
        
        rdoGroup = (RadioGroup) findViewById(R.id.rdogroupsex);
        rdoFemale = (RadioButton) findViewById(R.id.rdofemale);
        rdoMale = (RadioButton) findViewById(R.id.rdomale);
        btnOk = (Button) findViewById(R.id.btnok);
        
        onButtonOkClick();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_radio_button_example, menu);
        return true;
    }
    
    
    public void onButtonOkClick(){
    btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if ( rdoGroup.getCheckedRadioButtonId() == R.id.rdomale){
Toast.makeText(getApplicationContext(), "Male Selected", Toast.LENGTH_LONG).show();
}
if (rdoGroup.getCheckedRadioButtonId() == R.id.rdofemale){
Toast.makeText(getApplicationContext(), "Female Selected", Toast.LENGTH_LONG).show();
}
}
});
    }
}

Note: By clicking on "Ok" button, it will show selected option.


Android - EditText Example


Below is example of EditText, it can be used to enter text. Example demonstrates usage of other attributes and methods.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are resources are used to develop android app like image files etc. but for this example, we are using three basis components like code base file, layout file and string file.

Source of Layout File(.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:orientation="vertical" >

<EditText 
   android:id="@+id/etText"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="@string/ettext"
   />
    <Button
        android:id="@+id/btnselectall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnselectall"
        />
    
    <Button
        android:id="@+id/btnsetselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnsetselection"
        />
    <Button
        android:id="@+id/btnsettext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnsettext"
        />
    <Button
        android:id="@+id/btnclose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnclose"
        />
</LinearLayout >



Source of Strings file(.XML):

<resources>

    <string name="app_name">EditTextExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_edit_text_example">Edit Text Example</string>
<string name="ettext">Enter Text</string>
<string name="btnselectall">Select All</string>
<string name="btnsetselection">Set Selection</string>
<string name="btnsettext">Set Text</string>
<string name="btnclose">Close</string>
</resources>


Source code file (.JAVA):

package com.example.edittextexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditTextExample extends Activity {

private Button btnSelectAll, btnClose, btnSetSelection, btnSetText;
private EditText etText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text_example);
        
    btnSelectAll = (Button) findViewById(R.id.btnselectall);
    etText = (EditText) findViewById(R.id.etText);
    btnClose = (Button) findViewById(R.id.btnclose);
    btnSetSelection = (Button) findViewById(R.id.btnsetselection);
    btnSetText = (Button) findViewById(R.id.btnsettext);
   
    etText.setSingleLine(); //allows to be text in single line if it is not there then longer text will be in next lines. you can use "setHorizontallyScrolling" method as well
        addOnOkClickListener();
        addOnCloseListener();
        addOnSetSelectionListener();
        addOnClickSetTextListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_edit_text_example, menu);
        return true;
    }
    
    public void addOnOkClickListener (){

   
    btnSelectAll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.selectAll(); //select all text of EditText
}
});
    }
    
    public void addOnCloseListener(){
    btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
    }
    
    public void addOnSetSelectionListener(){
    btnSetSelection.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setSelection(0,4); //method is used to select specific character from EditText, start and end range is required, start = 0 means first character. and "End" parameter allows how many characters to be selected.
}
});
    }
    
    public void addOnClickSetTextListener(){
    btnSetText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setText("Hello World!!!"); //setText method is used to set Text to EditText
}
});
    }
}


Note:
  • "Select All"  selects text entered in text field
  • "Set Selection" selects first 4 characters of entered text
  • "Set Text" sets value like "Hello World!!!" to edit text 
  • "Close" will close the application