JAVA FILE
package com.example.viraj.blog_7_checkbox;
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;
public class MainActivity extends AppCompatActivity {
private CheckBox c1,c2,c3;
Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox)findViewById(R.id.id_cb1);
c2=(CheckBox)findViewById(R.id.id_cb2);
c3=(CheckBox)findViewById(R.id.id_cb3);
t1=(TextView)findViewById(R.id.id_txt) ;
b1=(Button)findViewById(R.id.id_button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//STRING BHI LENA HO TO STRING BUILDER LE
StringBuilder sbr=new StringBuilder();
//text ko jodne ke liye
sbr.append(c1.getText().toString()+"STATUS IS" +c1.isChecked()+"\n");
sbr.append(c2.getText().toString()+"STATUS IS" +c2.isChecked()+"\n");
sbr.append(c3.getText().toString()+"STATUS IS" +c3.isChecked()+"\n");
t1.setText(sbr);
}
});
}
}
XML FILE
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.viraj.blog_7_checkbox.MainActivity">
<CheckBox
android:id="@+id/id_cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="47dp"
android:text="APPLE"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/id_cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="68dp"
android:text="MANGO"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_cb1" />
<CheckBox
android:id="@+id/id_cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="76dp"
android:text="BANANA"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_cb2" />
<Button
android:id="@+id/id_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="31dp"
app:layout_constraintTop_toBottomOf="@+id/id_cb3"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="45dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_button" />
</android.support.constraint.ConstraintLayout>
========================================================================
package com.example.viraj.checkbox;
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.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
CheckBox dog,cat,horse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.id_button);
dog=(CheckBox)findViewById(R.id.id_dog);
cat=(CheckBox)findViewById(R.id.id_cat);
horse=(CheckBox)findViewById(R.id.id_horse);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dog.isChecked() && cat.isChecked())
{
Toast.makeText(getApplicationContext(),"its dog and Cat",Toast.LENGTH_LONG).show();
}
else if (dog.isChecked() )
{
Toast.makeText(getApplicationContext(),"its dog ",Toast.LENGTH_LONG).show();
}
else if ( cat.isChecked())
{
Toast.makeText(getApplicationContext(),"its Cat",Toast.LENGTH_LONG).show();
}
}
});
}
}
==================================================================================================================================================
package com.example.viraj.checkbox;
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.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
CheckBox dog,cat,horse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.id_button);
dog=(CheckBox)findViewById(R.id.id_dog);
cat=(CheckBox)findViewById(R.id.id_cat);
horse=(CheckBox)findViewById(R.id.id_horse);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dog.isChecked() && cat.isChecked())
{
Toast.makeText(getApplicationContext(),"its dog and Cat",Toast.LENGTH_LONG).show();
}
else if (dog.isChecked() )
{
Toast.makeText(getApplicationContext(),"its dog ",Toast.LENGTH_LONG).show();
}
else if ( cat.isChecked())
{
Toast.makeText(getApplicationContext(),"its Cat",Toast.LENGTH_LONG).show();
}
}
});
}
}
==================================================================================================================================================
0 comments:
Post a Comment