• Have Any Question?
  • contact@eitworld.com

Check Box Show Message





In this application we have used three checkboxes and one button, when we check the checkboxes then we have to check all the checkboxes if any of checkbox is not checked then we will get a message on the screen that shows the “select all checkboxes”; and if we select all the checkboxes then we will see the message “congo u r indian”. It is like a validation application because in some cases we have to select all the things in the application using the check box. The xml file is shown below:

 

<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” >

<TextView
android:id=”@+id/tv”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Check All Chekboxes” />

<CheckBox
android:id=”@+id/chk1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/tv”
android:layout_marginTop=”64dp”
android:text=”India” />

<CheckBox
android:id=”@+id/Chk2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/chk1″
android:layout_below=”@+id/chk1″
android:layout_marginTop=”24dp”
android:text=”Hindustan” />

<CheckBox
android:id=”@+id/Chk3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/Chk2″
android:layout_centerVertical=”true”
android:text=”Aryavart” />

<Button
android:id=”@+id/btnSubmit”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/Chk3″
android:layout_below=”@+id/Chk3″
android:layout_marginTop=”42dp”
android:text=”Submit” />

<TextView
android:id=”@+id/tvMessgae”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/chk1″
android:layout_alignParentRight=”true”
android:layout_below=”@+id/tv”
android:layout_marginTop=”20dp”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

 

In this layout file, we have used the relative layout and we have used the three checkboxes by using the <CheckBox> tag and used a button to perform the action. The java file is shown below:

package com.example.checkradiobutton;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView message;
CheckBox chk1,chk2,chk3;
Button submit;

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

message = (TextView)findViewById(R.id.tvMessgae);
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.Chk2);
chk3 = (CheckBox) findViewById(R.id.Chk3);
submit = (Button) findViewById(R.id.btnSubmit);

submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

if(chk1.isChecked()==false)
{
chk1.setTextColor(Color.RED);
}
if(!chk2.isChecked())
{
chk2.setTextColor(Color.RED);
}
if(!chk3.isChecked())
{
chk3.setTextColor(Color.RED);
}

if(chk1.isChecked() && chk2.isChecked() && chk3.isChecked())
{
chk1.setTextColor(Color.BLACK);
chk2.setTextColor(Color.BLACK);
chk3.setTextColor(Color.BLACK);
message.setText(“Congo u r Indian”);
}
else
{
message.setText(“Select all the checkboxes”);
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

In this java file we have used the following packages:

check-box-show-message After that we have a class named MainActivity which extends the Activity class shown as follow:

check-box-show-message Now we have to declare the instance of the Button, checkbox and TextView which are as follows:

check-box-show-message Here message is of type TextView that is used to show the message on the screen, chk1, chk2, chk3 are type of the CheckBox which takes the user input as checked box and after that submit is the Button type on which we have to perform the action inside the onCreate() method.

Now we have to register all the controls defined in the xml file inside the onCreate() method as follows:

check-box-show-message Now we have to perform the action on the submit button by using the setOnClickListener() method as follows:

check-box-show-message Inside this method, first of all we have to check that if any of checkbox is not checked then the text color of the checkbox will change into the red color. Here we used the setTextColor() method which takes one parameter that is color name with the class Color means Color.<COLOR_NAME>.; after that we have checked if all the checkboxes are checked then “Congo u r indian” message will be shown on the screen and if all the checkboxes are not checked then else part will be executed and the “Select all the checkboxes” message will be shown on the message.

The output is as follows:

check-box-show-message When all checkboxes are not checked

check-box-show-message when all checkboxes are checked



Real Time Web Analytics