Pages

Thursday, September 8, 2016

Android Example-Basic Calculator and Change Button TextColor

activity_main.xml
<LinearLayout 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"
    android:columnCount="2"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"

            android:ems="9"/>
        <EditText
            android:id="@+id/edittext2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"

            android:ems="10"
            />
    </LinearLayout>


    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"

        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <Button
                android:id="@+id/plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"/>

            <Button
                android:id="@+id/sub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"


                android:text="-" />

            <Button
                android:id="@+id/mul"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"


                android:text="X" />

            <Button
                android:id="@+id/divide"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:text="/" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <Button
                android:id="@+id/remender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="%"/>

            <Button
                android:id="@+id/clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"


                android:text="C" />

            <Button
                android:id="@+id/calculate"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"


                android:text="Calculate"
                android:textAlignment="center" />

        </LinearLayout>

        <Button
            android:id="@+id/color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text="ChangeColor"
            android:textAlignment="center" />






    </LinearLayout>

</LinearLayout>
MainActivity.java
package in.kantapp.example5;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    private EditText disp,editText2;
    private TextView res;
    private Button add,mul,divide,sub,clear,calc,change,rem;
    Integer num1,num2,result;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        disp= (EditText) findViewById(R.id.textView1);
        res= (TextView) findViewById(R.id.textView2);
        editText2= (EditText) findViewById(R.id.edittext2);

        add= (Button) findViewById(R.id.plus);
        mul= (Button) findViewById(R.id.mul);
        divide= (Button) findViewById(R.id.divide);
        sub= (Button) findViewById(R.id.sub);
        clear= (Button) findViewById(R.id.clear);
        calc= (Button) findViewById(R.id.calculate);
        change= (Button) findViewById(R.id.color);

        rem= (Button) findViewById(R.id.remender);
        rem.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                int num11 = Integer.parseInt(String.valueOf(disp.getText()));

                int num22 = Integer.parseInt(String.valueOf(editText2.getText()));

                if (num22==0)
                {
                    Toast.makeText(MainActivity.this, "Please Enter Other Number", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    result=num11%num22;

                    res.setText(String.valueOf(result));
                }

            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = Integer.parseInt(String.valueOf(disp.getText()));

                num2 = Integer.parseInt(String.valueOf(editText2.getText()));

                result=num1+num2;

                res.setText(String.valueOf(result));
            }
        });
        mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = Integer.parseInt(String.valueOf(disp.getText()));

                num2 = Integer.parseInt(String.valueOf(editText2.getText()));

                result=num1*num2;

                res.setText(String.valueOf(result));
            }
        });
        divide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                num1 = Integer.parseInt(String.valueOf(disp.getText()));

                num2 = Integer.parseInt(String.valueOf(editText2.getText()));


                if (num2==0)
                {
                    Toast.makeText(MainActivity.this, "Please Enter Other Number", Toast.LENGTH_SHORT).show();
                }

                else
                {
                    result=num1/num2;

                    res.setText(String.valueOf(result));
                }

            }
        });
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1 = Integer.parseInt(String.valueOf(disp.getText()));

                num2 = Integer.parseInt(String.valueOf(editText2.getText()));

                result=num1-num2;

                res.setText(String.valueOf(result));
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                disp.setText("");
                editText2.setText("");
            }
        });

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int rand = (int) ((Math.random() * 10) % 3);
                Button b1 = (Button) findViewById(R.id.plus);
                Button b2 = (Button) findViewById(R.id.mul);
                Button b3 = (Button) findViewById(R.id.divide);
                Button b4 = (Button) findViewById(R.id.sub);
                Button b5= (Button) findViewById(R.id.remender);
                Button b6 = (Button) findViewById(R.id.clear);
                Button b7 = (Button) findViewById(R.id.calculate);
                Button b8 = (Button) findViewById(R.id.color);

                switch (rand)
                {
                    case 0:
                        b1.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b2.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b3.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b4.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b5.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b6.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b7.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        b8.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
                        break;
                    case 1:
                        b1.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b2.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b3.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b4.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b5.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b6.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b7.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        b8.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                        break;
                    case 2:
                        b1.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b2.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b3.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b4.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b5.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b6.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b7.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        b8.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                        break;

                    default:
                        b1.setTextColor(getResources().getColor(android.R.color.black));
                        b2.setTextColor(getResources().getColor(android.R.color.black));
                        b3.setTextColor(getResources().getColor(android.R.color.black));
                        b4.setTextColor(getResources().getColor(android.R.color.black));
                        b5.setTextColor(getResources().getColor(android.R.color.black));
                        b6.setTextColor(getResources().getColor(android.R.color.black));
                        b7.setTextColor(getResources().getColor(android.R.color.black));
                        b8.setTextColor(getResources().getColor(android.R.color.black));
                        break;
                }
            }
        });

    }


}

No comments:

Post a Comment