Wednesday, 20 June 2012

Creating Question and Answer with Checkbox

Checkbox:-  


Check boxes are used when you want to use want to select one or more option from a set of alternative 

Select the Item in Checkbox :-



In this example. 


(1) There four 11 Player,3 Umpire, 2 Umpire, 31 Ball and 1 Bat.


(2) There is only Two alternate is right i.e. 11 player and 2 Umpire.


(3)Whatever you choose the option it will show in the TextView.


(4)There is three  image Ques.png,valid.png,wrong.png.


(5)These images change according to the chooses question.




 
LAYOUT OF THE PROGRAM




We make it by Graphical Method:By default in Graphical Method it show Icon and Text in pop down menu.We can can select Show only Icon.


Graphical view




By drag and drop you make easily layout of the program once you make, the main.xml file auto generated.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ques"

         />

    <CheckBox
        android:id="@+id/op1id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op1"
/>

    <CheckBox
        android:id="@+id/op2id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op2" />

    <CheckBox
        android:id="@+id/op3id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op3"
/>

    <CheckBox
        android:id="@+id/op4id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op4"
/>


    <TextView
        android:id="@+id/anstextid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/ans"

        />
   
    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ques"
        android:layout_gravity="center"
/>



    <Button
        android:id="@+id/refid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ref" android:layout_gravity="center"
/>

</LinearLayout> 

String.xml
Android Resources(default)

Explanation:
Here is 
Hello and app_name is default String
One is Text String that is ques
Four Check box Label
One is "Your Answer" String resources where ans shows
One Refresh Button Label


Now the Java Source Code


Java Source Code:-


package abh.checkbox.com;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class CheckboxquestionActivity extends Activity implements CompoundButton.OnCheckedChangeListener  {
    /** Called when the activity is first created. */
    CheckBox opt1,opt2,opt3,opt4;
    TextView tv;
    ImageView iv;
    Button refbut;
    String str="";
    int flag1=0,flag2=0,flag3=0,flag4=0;
   
    static int num=0;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        opt1=(CheckBox)findViewById(R.id.op1id);
        opt2=(CheckBox)findViewById(R.id.op2id);
        opt3=(CheckBox)findViewById(R.id.op3id);
        opt4=(CheckBox)findViewById(R.id.op4id);
        tv = (TextView)findViewById(R.id.anstextid);
        iv=(ImageView) findViewById(R.id.image1);
        refbut=(Button)findViewById(R.id.refid);
       
       
       
       
        opt1.setOnCheckedChangeListener(this);
        opt2.setOnCheckedChangeListener(this);
        opt3.setOnCheckedChangeListener(this);
        opt4.setOnCheckedChangeListener(this);
       
        refbut.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                str="";
                tv.setText(str);
                iv.setImageResource(R.drawable.ques);
                opt1.setChecked(false);
                opt2.setChecked(false);
                opt3.setChecked(false);
                opt4.setChecked(false);
               
            }
        });

       
       
       
       
    }
   

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       
       
        if(opt1.isChecked() && flag1==0 )
        {
             str=str+opt1.getText();
             tv.setText(str);
             iv.setImageResource(R.drawable.valid);
             flag1=1;
             num++;
              
        }
        if(opt2.isChecked() && flag2==0)
        {
            str=str+opt2.getText();
             tv.setText(str);
             iv.setImageResource(R.drawable.wrong);
             flag2=1;
             num++;
        }
        if(opt3.isChecked() && flag3==0)
        {
             str=str+opt3.getText();
             tv.setText(str);
             iv.setImageResource(R.drawable.valid);
             flag3=1;
             num++;
        }
        if(opt4.isChecked() && flag4==0)
        {
             str=str+opt2.getText();
             tv.setText(str);
             iv.setImageResource(R.drawable.wrong);
             flag4=1;
             num++;
        }
        if(num<2)
        {
            Context context = getApplicationContext();

            Toast toast = Toast.makeText(context, "Please Choose Two option", Toast.LENGTH_SHORT);
            toast.show();
        }
       
        }
   
       
    }



Expanation:- 
We define  OnCheckedChangeListener but we need to wire it to UI element before it come to action.There is two line of code that connect to checkbox to listen the checkbox click event
(1) Declaration of checkbox in main.xml file
 


  <CheckBox
        android:id="@+id/op1id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op1"
/>

    <CheckBox
        android:id="@+id/op2id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op2" />

    <CheckBox
        android:id="@+id/op3id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op3"
/>

    <CheckBox
        android:id="@+id/op4id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/op4"
/>



(2)  opt1,opt2,opt3,opt4 is checkbox variable is connected to setOnCheckedChangeListener(this) by two line of code for each checkbox


         opt1=(CheckBox)findViewById(R.id.op1id);
        opt2=(CheckBox)findViewById(R.id.op2id);
        opt3=(CheckBox)findViewById(R.id.op3id);
        opt4=(CheckBox)findViewById(R.id.op4id);


       
opt1.setOnCheckedChangeListener(this);
        opt2.setOnCheckedChangeListener(this);
        opt3.setOnCheckedChangeListener(this);
        opt4.setOnCheckedChangeListener(this)


public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
 It is method which takes two parameter CompoundButton and boolean.  checkedId is Id of the checkbox which is clicked.


      Every Activity class is subclass of Context Class.Generally Toast is used for the debugging purpose the  Toast is class generally used to show the message in the screen it takes three arguments Context reference, mmessage to display and the Toast.LENGTH_SHORT. 


 
Context context = getApplicationContext();

            Toast toast = Toast.makeText(context, "Please Choose Two option", Toast.LENGTH_SHORT);
            toast.show();





toast.show() function is the function which is used to display the message.

1 comment: