Part 8 - Introducing Color and Themes (Source Code)

 java Part : 


package first.learn.edittextproject;

import androidx.appcompat.app.AppCompatActivity;

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 android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

EditText etTwo,
etOne;

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

@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);


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

String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

// if (!isIntegerCharacter(a)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// return;
// }

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}


// if (!isIntegerCharacter(b)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// 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) {

String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

// if (!isIntegerCharacter(a)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// return;
// }

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}


// if (!isIntegerCharacter(b)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// 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) {
String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

// if (!isIntegerCharacter(a)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// return;
// }

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}


// if (!isIntegerCharacter(b)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// 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) {

String a = getTextFromEditText(etOne);

if (a == null) {

return;
}

// if (!isIntegerCharacter(a)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// return;
// }

String b = getTextFromEditText(etTwo);

if (b == null) {
return;
}


// if (!isIntegerCharacter(b)) {
// Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
// return;
// }

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

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

}
});

}

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="+"/>

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

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


<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"/>

</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>



colors : 

<?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>

</resources>


themes : 

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EditTextProject" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>



<style name="my_app_theme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/my_color_red</item>
<item name="colorPrimaryVariant">@color/teal_200</item>
<item name="colorOnPrimary">@color/white</item>

<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">@android:color/holo_green_light</item>
<!-- Customize your theme here. -->
</style>


</resources>


Manifests : 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.EditTextProject"
tools:targetApi="31">



<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/my_app_theme"
>

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>


</activity>



</application>

</manifest>



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