Saturday, 21 November 2015

Form Controls in Android(EditText,CheckBox ,RadioButton,Button etc)

06:21


Simple code for addition of   two   integer numbers   using   controls follow below directory example:



Step 1: write code for   calculator.java   (main activity):
package calculator.test.om.com.calculator;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class calculator extends AppCompatActivity {

    Integer
n1,n2,n3;
    EditText
no1,no2;
    TextView
t;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_calculator);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.
fab);
        fab.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                Snackbar.make(view,
"Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction(
"Action", null).show();
            }
        });

        Button a=(Button) findViewById(R.id.
add);
        a.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
               
t=(TextView)findViewById(R.id.result);

               
no1=(EditText)findViewById(R.id.n1);
               
no2=(EditText)findViewById(R.id.n2);
               
n1=Integer.parseInt(no1.getText().toString());
               
n2=Integer.parseInt(no2.getText().toString());
               
n3= n1+n2;
               
t.setText("Result is "+n3);
            }
        });
    }

   
@Override
   
public boolean onCreateOptionsMenu(Menu menu) {
       
// Inflate the menu; this adds items to the action bar if it is present.
       
getMenuInflater().inflate(R.menu.menu_calculator, menu);
       
return true;
    }

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       
int id = item.getItemId();

       
//noinspection SimplifiableIfStatement
       
if (id == R.id.action_settings) {
           
return true;
        }

       
return super.onOptionsItemSelected(item);
    }
}

step 2:  then write code for    activity_calculator.xml  :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".calculator">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_calculator" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>





Step   3   : content_calculator.xml  :
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_calculator" tools:context=".calculator">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="enter no 1"

        android:id="@+id/no1"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:layout_marginRight="204dp"

        android:layout_marginEnd="204dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="enter no 2"

        android:id="@+id/no2"

        android:layout_below="@+id/no1"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="43dp" />



    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/n1"

        android:layout_alignTop="@+id/no1"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:inputType="number"

        android:text="10"

        android:layout_toRightOf="@+id/add"

        android:layout_toEndOf="@+id/add" />



    <EditText

        android:inputType="number"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/n2"

        android:layout_alignBottom="@+id/no2"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:text="20"

        android:layout_alignLeft="@+id/n1"

        android:layout_alignStart="@+id/n1" />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="add"

        android:id="@+id/add"

        android:layout_below="@+id/no2"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="62dp" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="result"

        android:id="@+id/result"

        android:layout_below="@+id/add"

        android:layout_toRightOf="@+id/no2"

        android:layout_toEndOf="@+id/no2"

        android:layout_marginTop="89dp" />

</RelativeLayout>




Output:




----------------------------Advance example---------------------------
(1)write code for  MainActivity.java:

package gcmquickstart.com.samples.android.play.gcm.myapplication;

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

public class MainActivity extends Activity {

   
public String str ="";
    Character
op = 'q';
   
int i,num,numtemp;
    EditText
showResult;
   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
showResult = (EditText)findViewById(R.id.result_id);


    }
   
public void btn1Clicked(View v){
        insert(
1);

    }

   
public void btn2Clicked(View v){
        insert(
2);

    }
   
public void btn3Clicked(View v){
        insert(
3);

    }
   
public void btn4Clicked(View v){
        insert(
4);

    }
   
public void btn5Clicked(View v){
        insert(
5);

    }
   
public void btn6Clicked(View v){
        insert(
6);
    }
   
public void btn7Clicked(View v){
        insert(
7);

    }
   
public void btn8Clicked(View v){
        insert(
8);

    }
   
public void btn9Clicked(View v){
        insert(
9);

    }
   
public void btnplusClicked(View v){
        perform();
       
op = '+';

    }

   
public void btnminusClicked(View v){
        perform();
       
op = '-';

    }
   
public void btndivideClicked(View v){
        perform();
       
op = '/';

    }
   
public void btnmultiClicked(View v){
        perform();
       
op = '*';

    }
   
public void btnequalClicked(View v){
        calculate();

    }

   
public void btnclearClicked(View v){
        reset();
    }
   
private void reset() {
       
// TODO Auto-generated method stub
       
str ="";
       
op ='q';
       
num = 0;
       
numtemp = 0;
       
showResult.setText("");
    }
   
private void insert(int j) {
       
// TODO Auto-generated method stub
       
str = str+Integer.toString(j);
       
num = Integer.valueOf(str).intValue();
       
showResult.setText(str);


    }
   
private void perform() {
       
// TODO Auto-generated method stub
       
str = "";
       
numtemp = num;
    }
   
private void calculate() {
       
// TODO Auto-generated method stub
       
if(op == '+')
           
num = numtemp+num;
       
else if(op == '-')
           
num = numtemp-num;
       
else if(op == '/')
           
num = numtemp/num;
       
else if(op == '*')
           
num = numtemp*num;
        
showResult.setText(""+num);
    }

}


now  write code for activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>


Now write code for content_main.xml file :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">





    <EditText



        android:id="@+id/result_id"



        android:layout_width="fill_parent"



        android:layout_height="120dp"



        />



    <Button



        android:id="@+id/Btn7_id"



        android:layout_width="70dp"



        android:layout_height="60dp"



        android:layout_below="@id/result_id"



        android:text="7"



        android:onClick="btn7Clicked"



        />



    <Button



        android:id="@+id/Btn8_id"



        android:layout_width="70dp"



        android:layout_height="60dp"



        android:layout_below="@id/result_id"



        android:layout_toRightOf="@id/Btn7_id"



        android:text="8"



        android:onClick="btn8Clicked"



        />



    <Button



        android:id="@+id/Btn9_id"



        android:layout_width="70dp"



        android:layout_height="60dp"



        android:layout_below="@id/result_id"



        android:layout_toRightOf="@id/Btn8_id"



        android:text="9"



        android:onClick="btn9Clicked"



        />



    <Button



        android:id="@+id/Btnclear_id"



        android:layout_width="90dp"



        android:layout_height="60dp"



        android:layout_below="@id/result_id"



        android:layout_toRightOf="@id/Btn9_id"



        android:text="clear"



        android:onClick="btnclearClicked"



        />



    <Button



        android:id="@+id/Btn4_id"



        android:layout_width="70dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btn7_id"



        android:text="4"



        android:onClick="btn4Clicked"



        /> <Button



    android:id="@+id/Btn5_id"



    android:layout_width="70dp"



    android:layout_height="60dp"



    android:layout_below="@id/Btn8_id"



    android:layout_toRightOf="@id/Btn4_id"



    android:text="5"



    android:onClick="btn5Clicked"



    /> <Button



    android:id="@+id/Btn6_id"



    android:layout_width="70dp"



    android:layout_height="60dp"



    android:layout_below="@id/Btn9_id"



    android:layout_toRightOf="@id/Btn5_id"



    android:text="6"



    android:onClick="btn6Clicked"



    />



    <Button



        android:id="@+id/Btnplus_id"



        android:layout_width="90dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btnclear_id"



        android:layout_toRightOf="@id/Btn6_id"



        android:text="+"



        android:onClick="btnplusClicked"



        />



    <Button



        android:id="@+id/Btn1_id"



        android:layout_width="70dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btn4_id"



        android:text="1"



        android:onClick="btn1Clicked"



        /> <Button



    android:id="@+id/Btn2_id"



    android:layout_width="70dp"



    android:layout_height="60dp"



    android:layout_below="@id/Btn5_id"



    android:layout_toRightOf="@id/Btn1_id"



    android:text="2"



    android:onClick="btn2Clicked"



    /> <Button



    android:id="@+id/Btn3_id"



    android:layout_width="70dp"



    android:layout_height="60dp"



    android:layout_below="@id/Btn6_id"



    android:layout_toRightOf="@id/Btn2_id"



    android:text="3"



    android:onClick="btn3Clicked"



    />



    <Button



        android:id="@+id/Btnminus_id"



        android:layout_width="90dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btnplus_id"



        android:layout_toRightOf="@id/Btn3_id"



        android:text="-"



        android:onClick="btnminusClicked"







        />







    <Button



        android:id="@+id/Btnequal_id"



        android:layout_width="110dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btn1_id"



        android:text="="



        android:onClick="btnequalClicked"



        />



    <Button



        android:id="@+id/Btndivide_id"



        android:layout_width="90dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btn1_id"



        android:layout_toRightOf="@id/Btnequal_id"



        android:text="/"



        android:onClick="btndivideClicked"







        />



    <Button



        android:id="@+id/Btnmulti_id"



        android:layout_width="90dp"



        android:layout_height="60dp"



        android:layout_below="@id/Btnminus_id"



        android:layout_toRightOf="@id/Btndivide_id"



        android:text="*"



        android:onClick="btnmultiClicked"



        />





</RelativeLayout>


 OUPUT:



================================================================================================================================================
Checkbox  control example with OnClickLIstener  see below directory list:




(1)write code for  MainActivity.java :
package checkbox.test.om.com.myapplication;
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.TextView;

public class   MainActivity   extends   Activity   implements  OnClickListener
{
    CheckBox
c1,c2,c3;

    TextView
resp;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button  b=(Button)findViewById(R.id.
button);
       
resp=(TextView)findViewById(R.id.amount);
       
c1=(CheckBox)findViewById(R.id.c1);
       
c2=(CheckBox)findViewById(R.id.c2);
        
c3=(CheckBox)findViewById(R.id.c3);
        b.setOnClickListener(
this);
    }


   
@Override
   
public void onClick(View view)
    {
       
int amt=0;
       
if(c1.isChecked())
        {
          amt=amt+
15;

        }
       
if(c2.isChecked())
        {
            amt=amt+
5;
        }
       
if(c3.isChecked())
        {
          amt=amt+
10;
        }

       
resp.setText("Bill is"+ Integer.toString(amt));
    }
}

(2)write code for Activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)now  write code for  content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <CheckBox

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="pizza $15"

        android:id="@+id/c1"

        android:layout_marginTop="97dp"

        android:checked="false" />



    <CheckBox

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="hot dog $5"

        android:id="@+id/c2"

        android:layout_below="@+id/c1"

        android:checked="false" />



    <CheckBox

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Burger $10"

        android:id="@+id/c3"

        android:layout_below="@+id/c2"

        android:layout_alignLeft="@+id/c2"

        android:layout_alignStart="@+id/c2"

        android:checked="false" />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Calculate Bill"

        android:id="@+id/button"

        android:layout_below="@+id/c3"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="total amount"

        android:id="@+id/amount"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_alignRight="@+id/button"

        android:layout_alignEnd="@+id/button" />





</RelativeLayout>



output:


================================================================================================================================================
Radio button control using onClick  listener:

(1)write  code for  MainActivity.java :

package checkbox.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        RadioButton radioFivestar=(RadioButton)findViewById(R.id.
radioButton1);
        RadioButton radioThreestar=(RadioButton)findViewById(R.id.
radioButton2);
        radioFivestar.setOnClickListener(
radioListener);
        radioThreestar.setOnClickListener(
radioListener);


    }

   
private OnClickListener radioListener =new  OnClickListener() {
       
@Override
       
public void onClick(View view) {
            TextView selectedhotel=(TextView)findViewById(R.id.
textView);
            RadioButton rb=(RadioButton) view;
            selectedhotel.setText(
"the hotel type selected is:" + rb.getText());
        }

    };

   
}


(2)write code for activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>


(3)write  code for  content_main.xml   file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">







    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="hotel type is "

        android:id="@+id/textView"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />



    <RadioGroup android:id="@+id/group_hotel"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_marginTop="34dp"

        android:layout_alignParentTop="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:layout_above="@+id/textView">







        <RadioButton

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Five star"

            android:id="@+id/radioButton1"

            android:layout_marginTop="78dp"

            android:checked="false"

            android:layout_alignParentTop="true"

            android:layout_centerHorizontal="true" />



        <RadioButton

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Three star"

            android:id="@+id/radioButton2"

            android:layout_below="@+id/radioButton1"

            android:layout_alignLeft="@+id/radioButton1"

            android:layout_alignStart="@+id/radioButton1"

            android:checked="false" />







    </RadioGroup>



</RelativeLayout>

output:


========================================================================
Radio   button example using implements  method:

(1)write code for  Main3Activity.java file:

package com.example.day1.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity implements OnClickListener {
    RadioButton
r1,r2;
    TextView
tv;


   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main3);
       
r1=(RadioButton)findViewById(R.id.r1);
       
r2=(RadioButton)findViewById(R.id.r2);
       
r1.setOnClickListener(this);
       
r2.setOnClickListener(this);

    }
public void onClick(View view){
    TextView tv =(TextView)findViewById(R.id.
textView);
    RadioButton rb=(RadioButton) view;
    tv.setText(
"the gender is"+ rb.getText());
}
}

(2)write  code for  activity_main3.xml file:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main3"

    tools:context="com.example.day1.myapplication.Main3Activity">

    <RadioGroup

        android:id="@+id/group"

        android:layout_width="match_parent"

        android:layout_height="match_parent">



    <RadioButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="male"

        android:id="@+id/r1"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="52dp"

        android:checked="false" />



    <RadioButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="female"

        android:id="@+id/r2"

        android:layout_below="@+id/r1"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="48dp"

        android:checked="false" />

    </RadioGroup>





    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="New Text"

        android:id="@+id/textView"

        />

</RelativeLayout>


 output:



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING AT VISSICOMP BY OM SIR. All rights resevered. Designed by Templateism

Back To Top