postheadericon Android Checkbox setOnCheckedChangeListener Example



MainActivity.java

package bipin.rupadiya.checkboxexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;



public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CheckBox eng = (CheckBox) findViewById(R.id.chkEnglish);
        CheckBox hin = (CheckBox) findViewById(R.id.chkHindi);
        CheckBox guj = (CheckBox) findViewById(R.id.chkGujarati);
     
        eng.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
     
        hin.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
     
        guj.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
     
    }

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



activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:text="Select Language"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox
        android:id="@+id/chkEnglish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="40dp"
        android:text="English" />

    <CheckBox
        android:id="@+id/chkHindi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/chkEnglish"
        android:layout_below="@+id/chkEnglish"
        android:layout_marginTop="22dp"
        android:text="Hindi" />

    <CheckBox
        android:id="@+id/chkGujarati"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/chkHindi"
        android:layout_below="@+id/chkHindi"
        android:layout_marginTop="30dp"
        android:text="Gujarati" />

</RelativeLayout>



3 comments:

Ezequiel Jarvis & Martin Otonello said...

dwd

Unknown said...

I want to connect MCQ to different checkboxes Lessonwise. Please assist if you can.
mail: studypoint4all@gmail.com

Real-Time Teaching said...

Informative blog and knowledgeable content. Keep sharing more blogs with us.
UI Development Course in Hyderabad
Uipath Training in Hyderabad
Python Course in Hyderabad
Mean Stack Development Course in Hyderabad

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.