Wednesday, 26 August 2015

Checkbox1 in Android

activity_main.xml
----------------------
<LinearLayout 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:orientation="vertical"

    tools:context=".MainActivity">



    <CheckBox

        android:layout_width="fill_parent"

        android:id="@+id/hellocheckbox"

        android:text="CheckBox"

        android:layout_height="wrap_content" />



</LinearLayout>
MainActivity.Java
----------------------------
package com.android.anil.checkbox1;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    CheckBox cb;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        cb=(CheckBox)findViewById(R.id.hellocheckbox);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(buttonView.isChecked()){

                    cb.setText("Checked");

           Toast.makeText(getApplicationContext(),"Checked",5000).show();

                }else{

                    cb.setText("UnChecked");

           Toast.makeText(getApplicationContext(),"UnChecked",5000).show();

                }

            }

        });

    }

}
OutPut is:
---------------

No comments:

Post a Comment