Tuesday, 20 January 2015

CheckBox Example1




string.xml

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

    <string name="app_name">Checkbox</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="Facebook">Facebook</string>
    <string name="Twitter">Twitter</string>
    <string name="Google">Google+</string>
   
   

</resources>

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="48dp"
        android:text="@string/Facebook" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_below="@+id/checkBox2"
        android:text="@string/Google" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:text="@string/Twitter" />
   
</RelativeLayout>


MainActivity.java

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

public class MainActivity extends Activity {
       CheckBox facebook, twitter, google;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              facebook = (CheckBox) findViewById(R.id.checkBox1);
              twitter = (CheckBox) findViewById(R.id.checkBox2);
              google = (CheckBox) findViewById(R.id.checkBox3);
              facebook.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                     @Override
                     public void onCheckedChanged(CompoundButton buttonView,
                                  boolean isChecked) {
                           // TODO Auto-generated method stub
                           Toast.makeText(getApplicationContext(), "facebook is cheked",
                                         Toast.LENGTH_LONG).show();
                     }
              });
              twitter.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                   @Override
                     public void onCheckedChanged(CompoundButton buttonView,
                                  boolean isChecked) {
                           // TODO Auto-generated method stub
                           Toast.makeText(getApplicationContext(), "twitter is cheked",
                                         Toast.LENGTH_LONG).show();
                     }
              });
              google.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                     @Override
                     public void onCheckedChanged(CompoundButton buttonView,
                                  boolean isChecked) {
                           // TODO Auto-generated method stub
                           Toast.makeText(getApplicationContext(), "Google+ is cheked",
                                         Toast.LENGTH_LONG).show();
                     }
              });
       }
     
}



No comments:

Post a Comment