Pages

Wednesday, May 4, 2016

Android Checkbox Basics and Example



MainActivity.java
package kantapp.androidcheckboxlike;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    CheckBox hollywood,bollywood,southmovie,other;
    Button submit;
    TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hollywood=(CheckBox)findViewById(R.id.hwood);
        bollywood=(CheckBox)findViewById(R.id.bwood);
        southmovie=(CheckBox)findViewById(R.id.s_movie);
        other=(CheckBox)findViewById(R.id.o_movie);


        hollywood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked())
                {
                    Toast.makeText(MainActivity.this, "Hollywood is Selected", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Hollywood is not Selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        bollywood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked())
                {
                    Toast.makeText(MainActivity.this, "bollywood is Selected", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "bollywood is not Selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        southmovie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked())
                {
                    Toast.makeText(MainActivity.this, "southmovie is Selected", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "southmovie is not Selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked())
                {
                    Toast.makeText(MainActivity.this, "other is Selected", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "other is not Selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        result=(TextView)findViewById(R.id.result);
        submit=(Button)findViewById(R.id.fav_movie);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                StringBuffer stringBuffer=new StringBuffer();

                stringBuffer.append("You Like \n\n Hollywood : ").append(hollywood.isChecked());
                stringBuffer.append("\n Bollywood : ").append(bollywood.isChecked());
                stringBuffer.append("\n South Movie : ").append(southmovie.isChecked());
                stringBuffer.append("\n Othe Movie : ").append(other.isChecked());

                result.setText(stringBuffer.toString());

            }
        });


    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
    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="kantapp.androidcheckboxlike.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="What is your Fav Movie"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox
        android:id="@+id/hwood"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Hollywood" />

    <CheckBox
        android:id="@+id/bwood"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Bollywood" />

    <CheckBox
        android:id="@+id/s_movie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="South Movie" />

    <CheckBox
        android:id="@+id/o_movie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Other Movie" />

    <Button
        android:id="@+id/fav_movie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>


Download Complete Project :https://userscloud.com/ykutu2iekuj3

Download Only Source Code:https://userscloud.com/ggaj5ntasacj

No comments:

Post a Comment