Thursday, 6 October 2016

CheckBox

ActivityMain.XML :-

<?xml version="1.0" encoding="utf-8"?> 
<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="com.kiranapp.MainActivity">


 <CheckBox 
  android:layout_width="wrap_content"         
  android:layout_height="wrap_content" 
  android:text="Apple" 
  android:id="@+id/checkBox"         
  android:layout_alignParentTop="true" 
  android:layout_toStartOf="@+id/button1" 
  android:layout_marginEnd="34dp" />

 <CheckBox 

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Orange" 
  android:id="@+id/checkBox2" 
  android:layout_alignParentTop="true" 
  android:layout_toEndOf="@+id/button1" 
  android:layout_marginStart="24dp" />
 <Button 
  android:id="@+id/button1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="61dp" 
  android:text="Order" 
  android:layout_alignParentTop="true" 
  android:layout_centerHorizontal="true" />

</RelativeLayout>


MainActivity.java :-

package com.kiranapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {
   CheckBox apple,orange;
   Button Order;
 @Override 
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   addListenerOnButtonClick();
  }
  public void addListenerOnButtonClick(){

   apple=(CheckBox)findViewById(R.id.checkBox);
   orange=(CheckBox)findViewById(R.id.checkBox2);

   Order=(Button)findViewById(R.id.button1);


   Order.setOnClickListener(new OnClickListener(){

 @Override 
   public void onClick(View view) {
    int totalamount=0;
    StringBuilder result=new StringBuilder();
    result.append("Selected Items:");
    if(apple.isChecked()){
    result.append("\nApple 100Rs");
    totalamount+=100;
   }
    if(orange.isChecked()){
    result.append("\nOrange 150Rs");
    totalamount+=150;
   }

   result.append("\nTotal: "+totalamount+"Rs");

   Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
  }

  });
 }
 @Override 
  public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
}

Output :-

CheckBox
CheckBox fig:1
CheckBox

No comments:

Post a Comment