Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make CheckBox in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 393
    Comment on it

    Here I have created Checkbox function in android. In android CheckBox is a type of two state button either checked or unchecked. We can use checkbox to activate/deactivate the specific action. In the below code example, I have described how to make checkbox in android.

    Step(1)- activity_main.xml

    <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"  
        tools:context=".MainActivity" >  
    
        <CheckBox  
            android:id="@+id/checkBox1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:text="Civic" />  
    
        <CheckBox  
            android:id="@+id/checkBox2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_toRightOf="@+id/checkBox1"  
            android:text="Honda"/>  
    
        <CheckBox  
            android:id="@+id/checkBox3"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_toRightOf="@+id/checkBox2"  
            android:text="Amaze />  
    
        <Button  
            android:id="@+id/button1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_below="@+id/checkBox2"  
            android:layout_marginTop="32dp"  
            android:layout_toLeftOf="@+id/checkBox3"  
            android:text="Order" />  
    
    </RelativeLayout>  
    

    Step(2)- MainActivity.java class

      public class MainActivity extends Activity {  
        CheckBox Civic,Honda,Amaze;  
        Button buttonOrder;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            addListenerOnButtonClick();  
        }  
    public void addListenerOnButtonClick(){  
        //Getting instance of CheckBoxes and Button from the activty_main.xml file  
        Civic=(CheckBox)findViewById(R.id.checkBox1);  
         Honda=(CheckBox)findViewById(R.id.checkBox2);  
        Amaze=(CheckBox)findViewById(R.id.checkBox3);  
        buttonOrder=(Button)findViewById(R.id.button1);  
    
        //Applying the Listener on the Button click  
        buttonOrder.setOnClickListener(new OnClickListener(){  
    
            @Override  
            public void onClick(View view) {  
                int totalamount=0;  
                StringBuilder result=new StringBuilder();  
                result.append("Selected Items:");  
                if(Civic.isChecked()){  
                    result.append("\nCivic 100000Rs");  
                    totalamount+=100;  
                }  
                if(Honda.isChecked()){  
                    result.append("\nHonda 500000Rs");  
                    totalamount+=50;  
                }  
                if(Amaze.isChecked()){  
                    result.append("\nAmaze 120000Rs");  
                    totalamount+=120;  
                }  
                result.append("\nTotal: "+totalamount+"Rs");  
                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
            }  
    
        });  
    }  
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            // Inflate the menu; this adds items to the action bar if it is present.  
            getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
    
    }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: