Part 9 : Working with Colors and Color Algorithm.

Java Part : 

package first.learn.edittextproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

EditText etTwo,
etOne;

TextView tvResult, txtNewResult;
Button btnPlus, btnMinus, btnDevide, btnMulitiply;

int grey, sky, red, blue, yellow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etTwo = (EditText) findViewById(R.id.etTwo);
etOne = (EditText) findViewById(R.id.etOne);

tvResult = (TextView) findViewById(R.id.tvResult);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button)findViewById(R.id.btnMinus);
btnDevide = (Button)findViewById(R.id.btnDevide);
btnMulitiply = (Button)findViewById(R.id.btnMulitiply);
txtNewResult = (TextView)findViewById(R.id.txtNewResult);

/*
yellow: -10752
blue: -16776961
grey: -13291215
sky: -13611010
red: -2818048
*/

yellow = ContextCompat.getColor(MainActivity.this, R.color.yellow);
// btnMinus.setBackgroundColor(-10752);

blue = Color.BLUE;
// btnDevide.setBackgroundColor(-16776961);


grey = Color.parseColor("#353131");
// btnPlus.setBackgroundColor(-13291215);


sky = 0xFF304FFE;
// btnMinus.setBackgroundColor(-2818048);

red = Color.rgb(213,0,0);
// btnMulitiply.setBackgroundColor(-2818048);


System.out.println("yellow: "+yellow+"\nblue: "+blue+"\ngrey: "+grey
+"\nsky: "+sky+"\nred: "+red);


btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//changeColorBtnColor(btnPlus);
changeColorBtnPlus ();
String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}

float c = stringToFloat(a);
float d = stringToFloat(b);

float e = resultAddFloat(c, d);

String formatted = new DecimalFormat("#.####").format(e);
tvResult.setText("" + formatted);
txtNewResult.setText("" + formatted);


}
});

btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


// changeColorBtnColor(btnMinus);
changeColorBtnMinus();

String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}

float c = stringToFloat(a);
float d = stringToFloat(b);


minus(c, d, tvResult);
minus(c, d, txtNewResult);


}
});

btnDevide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//changeColorBtnColor(btnDevide);
changeColorBtnDevide();
String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}

float c = stringToFloat(a);
float d = stringToFloat(b);

float p = divide( c, d );

System.out.println("Divide Section: before Formatted : "+ p );

String formatted = new DecimalFormat("#.####").format(p);

System.out.println("Divide Section: after Formatted : "+ formatted );

tvResult.setText(""+formatted );
txtNewResult.setText(""+formatted);

}
});

btnMulitiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// changeColorBtnColor(btnMulitiply);

changeColorBtnMultiply();

String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}

float c = stringToFloat(a);
float d = stringToFloat(b);

multiply (c, d, tvResult );
multiply (c, d, txtNewResult );

}
});

}

void changeColorBtnColor(Button btn){

btnPlus.setBackgroundColor(grey);
btnMinus.setBackgroundColor(grey);
btnDevide.setBackgroundColor(grey);
btnMulitiply.setBackgroundColor(grey);

btn.setBackgroundColor(red);
}

void changeColorBtnPlus (){
btnPlus.setBackgroundColor(red);
btnMinus.setBackgroundColor(grey);
btnDevide.setBackgroundColor(grey);
btnMulitiply.setBackgroundColor(grey);
}




void changeColorBtnMinus (){

btnPlus.setBackgroundColor(grey);
btnMinus.setBackgroundColor(red);
btnDevide.setBackgroundColor(grey);
btnMulitiply.setBackgroundColor(grey);
}



void changeColorBtnDevide (){

btnPlus.setBackgroundColor(grey);
btnMinus.setBackgroundColor(grey);
btnDevide.setBackgroundColor(red);
btnMulitiply.setBackgroundColor(grey);
}


void changeColorBtnMultiply (){

btnPlus.setBackgroundColor(grey);
btnMinus.setBackgroundColor(grey);
btnDevide.setBackgroundColor(grey);
btnMulitiply.setBackgroundColor(red);
}

void multiply (float a , float b, TextView txtResult ){

float c = a*b ;
String formatted = new DecimalFormat("#.####").format(c);

txtResult.setText(""+formatted );

}


float divide (float n, float m ){

float x = n/m;

return x ;
}



void minus (float a , float b, TextView txtResult ){

float c = a - b;

String formatted = new DecimalFormat("#.####").format(c);

txtResult.setText(""+formatted);

}

boolean isIntegerCharacter(String s) {

int c = -1;

try {
c = Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

float stringToFloat(String s) {
return Float.parseFloat(s);
}


float resultAddFloat(float a, float b) {
return a + b;
}


String getTextFromEditText(EditText editText) {
String s = editText.getText().toString();

if (TextUtils.isEmpty(s)) {
editText.setError("Empty");
return null;
} else {
editText.setError(null);
return s;
}

}


}


Xml Part : 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp"

>



<EditText
android:hint="number One...."
android:layout_marginTop="25dp"
android:id="@+id/etOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>

<EditText
android:hint="number two..."
android:layout_marginTop="25dp"
android:id="@+id/etTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>


<TextView
android:textSize="25dp"
android:textColor="@color/purple_700"
android:layout_marginTop="20dp"
android:id="@+id/tvResult"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result..."
/>


<LinearLayout
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">

<Button
android:layout_weight="1"
android:id="@+id/btnPlus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="+"
android:backgroundTint="@color/grey"/>

<Button
android:layout_marginLeft="2dp"
android:id="@+id/btnMinus"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="-"
android:backgroundTint="@color/grey"/>

<Button
android:layout_marginLeft="2dp"
android:id="@+id/btnDevide"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="/"
android:backgroundTint="@color/grey"/>


<Button
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:id="@+id/btnMulitiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="x"
android:backgroundTint="@color/grey"/>

</LinearLayout>



<LinearLayout
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:weightSum="4">

<TextView
android:background="@color/my_color_red"
android:layout_width="0dp"
android:layout_weight="3"
android:text="Final Resul"
android:gravity="center"
android:textColor="@color/white"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/txtNewResult"
android:background="@color/my_color_green"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/white"
android:text="value"
android:layout_height="match_parent"/>

</LinearLayout>










</LinearLayout>



Color.xml : 


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>


<color name="my_color_red">#B81059</color>

<color name="my_color_green">#06A120</color>


<color name="yellow">#FFD600</color>

<color name="sky">#304FFE</color>

<color name="blue">#070F40</color>

<color name="grey">#353131</color>

<color name="red">#D50000</color>


</resources>




 

No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts