postheadericon android Checkbox setOnClickListener Example



MainActivity.java

package bipin.rupadiya.checkboxexample;



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

public class MainActivity extends Activity {

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

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(eng.isChecked())
{
Toast.makeText(getApplicationContext(), "Checkbox English is True", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Checkbox English is False", Toast.LENGTH_LONG).show();
}
}
});
       
     
        hin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(hin.isChecked())
{
Toast.makeText(getApplicationContext(), "Checkbox Hindi is True", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Checkbox Hindi is False", Toast.LENGTH_LONG).show();
}
}
});
     
        guj.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(guj.isChecked())
{
Toast.makeText(getApplicationContext(), "Checkbox Gujarati is True", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Checkbox Gujarati is False", 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>


1 comments:

Anonymous said...

Thanks good tutorial,
I have one question .I am working on checkbox example with listview and textview. when i click on checkbox then i want corresponding text value , but it displays evey time first position row value ..please help me how to solve it.
Thanks in advanced

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.