Don't Miss

Checkbox in android

By on January 10, 2018

Activity.java

 

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

public class ExampleActivity extends AppCompatActivity {

private CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5;
private Button btnview;

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

checkBox1 = (CheckBox)findViewById(R.id.checkBox);
checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
checkBox3 = (CheckBox)findViewById(R.id.checkBox3);
checkBox4 = (CheckBox)findViewById(R.id.checkBox4);
checkBox5 = (CheckBox)findViewById(R.id.checkBox5);

btnview = (Button)findViewById(R.id.button);

btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String country1=””,country2=””,country3=””,country4=””,country5=””;
if(checkBox1.isChecked())
{
country1 = checkBox1.getText().toString();
}

if(checkBox2.isChecked())
{
country2 = checkBox2.getText().toString();
}
if(checkBox3.isChecked())
{
country3 = checkBox3.getText().toString();
}
if(checkBox4.isChecked())
{
country4 = checkBox4.getText().toString();
}
if(checkBox1.isChecked())
{
country5 = checkBox5.getText().toString();
}
Toast.makeText(ExampleActivity.this,
country1+”\n”+country2+”\n”+country3+”\n”+country4+”\n”+country5+”\n”, Toast.LENGTH_SHORT).show();

}
});

}

}

 

activity_main.xml

 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>

<CheckBox
android:id=”@+id/checkBox”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”41dp”
android:layout_marginStart=”41dp”
android:layout_marginTop=”42dp”
android:text=”America” />

<CheckBox
android:id=”@+id/checkBox2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentEnd=”true”
android:layout_alignParentRight=”true”
android:layout_alignTop=”@+id/checkBox”
android:layout_marginEnd=”57dp”
android:layout_marginRight=”57dp”
android:text=”England” />

<CheckBox
android:id=”@+id/checkBox3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/checkBox”
android:layout_alignStart=”@+id/checkBox”
android:layout_below=”@+id/checkBox”
android:layout_marginTop=”43dp”
android:text=”France” />

<CheckBox
android:id=”@+id/checkBox4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/checkBox2″
android:layout_alignStart=”@+id/checkBox2″
android:layout_alignTop=”@+id/checkBox3″
android:layout_marginLeft=”11dp”
android:layout_marginStart=”11dp”
android:text=”Itlay” />

<CheckBox
android:id=”@+id/checkBox5″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/checkBox3″
android:layout_alignStart=”@+id/checkBox3″
android:layout_below=”@+id/checkBox3″
android:layout_marginTop=”35dp”
android:text=”Germany” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/checkBox5″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”28dp”
android:text=”View Checked” />
</RelativeLayout>

 

About CodingBazz

Leave a Reply

Your email address will not be published. Required fields are marked *