Thursday 14 February 2013

Action Task - 2

Demo:

1)action_task2.xml:


<?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:background="@color/color_white"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Shopping Kart"
        android:textColor="@color/color_black"
        android:textSize="20dp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Samsung Y duos"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="10000"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Samsung Ace duos"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="12000"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Samusung Grand"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="22000"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Akash Tablet"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2500"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Samsung Tablet"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="27000"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cellkon Tablet"
            android:textColor="@color/color_black" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6500"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Display Total" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/color_black" />

</LinearLayout>


2)Activity:


package com.venky.gvrtasks;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class ActionTask2Activity extends Activity {
      CheckBox cb1, cb2, cb3, cb4, cb5, cb6;

      TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7;

      int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0;

      int res = 0, preResult = 0;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.action_task2);

            cb1 = (CheckBox) findViewById(R.id.checkBox1);
            cb2 = (CheckBox) findViewById(R.id.checkBox2);
            cb3 = (CheckBox) findViewById(R.id.checkBox3);
            cb4 = (CheckBox) findViewById(R.id.checkBox4);
            cb5 = (CheckBox) findViewById(R.id.checkBox5);
            cb6 = (CheckBox) findViewById(R.id.checkBox6);

            tv1 = (TextView) findViewById(R.id.textView1);
            tv2 = (TextView) findViewById(R.id.textView2);
            tv3 = (TextView) findViewById(R.id.textView3);
            tv4 = (TextView) findViewById(R.id.textView4);
            tv5 = (TextView) findViewById(R.id.textView5);
            tv6 = (TextView) findViewById(R.id.textView6);

            tv7 = (TextView) findViewById(R.id.textView7);

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

            btn.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        if (cb1.isChecked()) {

                              val1 = Integer.parseInt(tv1.getText().toString());
                        } else
                              val1 = 0;

                        if (cb2.isChecked()) {
                              val2 = Integer.parseInt(tv2.getText().toString());
                        } else
                              val2 = 0;

                        if (cb3.isChecked()) {
                              val3 = Integer.parseInt(tv3.getText().toString());
                        } else
                              val3 = 0;

                        if (cb4.isChecked()) {
                              val4 = Integer.parseInt(tv4.getText().toString());
                        } else
                              val4 = 0;

                        if (cb5.isChecked()) {
                              val5 = Integer.parseInt(tv5.getText().toString());
                        } else
                              val5 = 0;

                        if (cb6.isChecked()) {
                              val6 = Integer.parseInt(tv6.getText().toString());
                        } else
                              val6 = 0;

                        res = val1 + val2 + val3 + val4 + val5 + val6;

                        tv7.setText("Total Amount " + res);

                  }
            });
      }
}



3)Add this Statements in strings.xml file:


<color name="color_black">#000000</color>
<color name="color_white">#ffffff</color>

No comments:

Post a Comment