Tuesday, 20 January 2015

CheckBox Example2



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>
    <string name="CheckedItems">CheckedItems</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" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox3"
        android:layout_below="@+id/checkBox3"
        android:layout_marginTop="31dp"
        android:text="@string/CheckedItems" />
   
</RelativeLayout>

MainActivity.java

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

public class MainActivity extends Activity {
       CheckBox facebook, twitter, google;
       Button button;
       String name;
       @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);
              button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           if (facebook.isChecked()) {
                                  name = "facebook\n";
                           }
                           if (twitter.isChecked()) {
                                  name = name + "twitter\n";
                           }
                           if (google.isChecked()) {
                                  name = name + "google+";
                           }
                           Toast.makeText(getApplicationContext(),
                                         "You have checked:\n" + name, Toast.LENGTH_LONG)
                                         .show();
                     }
              });
       }
}


No comments:

Post a Comment