By reading this article you will get to know about Checkbox Control which is used for taking data from the user in multiple ways.
1. It has two states either checked or unchecked.
2. It has so many application such as computers or Phones, tablet.
3. I am using a single button and three checkboxes which are declared in Activity_Main.xml.
4. As we know the controls are controlled by the java code which is used in MainActivity.java.
5. In java file, the method buttonChecked() is used to invoke when Activity is created.
6. Methods introduced are onClick, setOnclickListener and followed by view.onClickListener.
7. StringBuilder class is used to build the modified String with append method. It is used to get the message on the screen with the help of TextView and Toast.
8. isChecked method is used to verify, that either it has used click on following checkboxes or not.
9. Declare menu is inside the directory res\menu. If it is not given there then you can create it manually.
Attributes
Each attribute is inherited from android.Widget.TextView
o android: autoText: when this attribute is set then it specifies TextView in a Textual input method and check the common spelling error.
o android:drawableBottom: This is drawable elements which are specified at the bottom.
o android:drawableRight: This is drawable elements which are specified in the right
o android:editable: Attribute provides the editable TextView input.
AndroidManifest.xml
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsample.msclient009.checkbox">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mapsample.msclient009.checkbox.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
<CheckBox
android:id="@+id/color1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Red"
/>
<CheckBox
android:id="@+id/color2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Green" />
<CheckBox
android:id="@+id/color3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blue"
/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="itemSelected"
/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="34dp"
/>
</LinearLayout>
Main_Activity.java
package com.mapsample.msclient009.checkbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
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 r,g,b;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
buttonChecked ();
}
public void buttonChecked()
{ r =(CheckBox)findViewById ( R.id.color1 );
g =(CheckBox)findViewById ( R.id.color2 );
b =(CheckBox)findViewById ( R.id.color3 );
txt=(TextView)findViewById (R.id.tv );
btn =(Button)findViewById (R.id.btn );
btn.setOnClickListener ( new View.OnClickListener ( ) {
@Override
public void onClick(View v) {
int count = 0;
StringBuilder builder = new StringBuilder ( );
builder.append ( "<<<---Select Item--->>>" );
if(r.isChecked ()) {
builder.append ( "Red" );
count++;
}
if(g.isChecked ())
{builder.append ( "Green" );
count++;
}
if(b.isChecked ())
{ builder.append ("Blue" );
count++;
}
Toast.makeText ( getApplicationContext (),count+"Item name is \n"+builder.toString (),Toast.LENGTH_LONG ).show ();
txt.setText ( count+"colors\n"+builder.toString ()+"\nis Selected" );
}
} );
}
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater ().inflate (R.menu.menu_activity,menu );
return true;
}
}
Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="red">red</item>
<item android:title="green">Green</item>
<item android:title="blue">Blue</item>
</menu>
Leave Comment