Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Tuesday, 15 June 2021

Login with Retrofit in android Studio with SourceCode | Login and Registration form in android using JSON example

 Build.gradle File:-  
 
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.6.0'
 
 Activity-main 
<?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:orientation="vertical"
android:background="@color/colorWhite"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:text="Login to continue"
android:layout_marginTop="100dp"
android:textSize="24sp"
android:layout_gravity="center_horizontal"
android:textColor="@color/colorPrimaryDark"
android:layout_height="wrap_content" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:hint="Username"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:inputType="text"
android:id="@+id/edUsername"
android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:hint="Password"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:inputType="textPassword"
android:id="@+id/edPassword"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_margin="30dp"
android:text="Login"
android:id="@+id/btnLogin"
android:background="@color/colorPrimary"
android:textColor="@color/colorWhite"
android:layout_height="wrap_content" />
</LinearLayout
 
 Java File :-
 
1. Api Client 
 
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

private static Retrofit getRetrofit(){

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();

Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://api.irawen.net/") //Change server URL
.client(okHttpClient)
.build();

return retrofit;
}


public static UserService getUserService(){
UserService userService = getRetrofit().create(UserService.class);

return userService;
}

}

2. Login Request :-
 
public class LoginRequest {

private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

3. Login response :- 
 
public class LoginResponse {


private int user_id;
private String email;
private String username;

public int getUser_id() {
return user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}
 
4. Main Activity :-
 import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;

import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {


TextInputEditText username, password;
Button btnLogin;

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

username = findViewById(R.id.edUsername);
password = findViewById(R.id.edPassword);
btnLogin = findViewById(R.id.btnLogin);

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(TextUtils.isEmpty(username.getText().toString()) || TextUtils.isEmpty(password.getText().toString())){
Toast.makeText(MainActivity.this,"Username / Password Required", Toast.LENGTH_LONG).show();
}else{
//proceed to login
login();
}

}
});
}


public void login(){
LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(username.getText().toString());
loginRequest.setPassword(password.getText().toString());

Call<LoginResponse> loginResponseCall = ApiClient.getUserService().userLogin(loginRequest);
loginResponseCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

if(response.isSuccessful()){
Toast.makeText(MainActivity.this,"Login Successful", Toast.LENGTH_LONG).show();
LoginResponse loginResponse = response.body();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

startActivity(new Intent(MainActivity.this,DashboardActivity.class).putExtra("data",loginResponse.getUsername()));
}
},700);

}else{
Toast.makeText(MainActivity.this,"Login Failed", Toast.LENGTH_LONG).show();

}

}

@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(MainActivity.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();

}
});


}

}

5. User Service :-
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface UserService {


@POST("authenticate/")
Call<LoginResponse> userLogin(@Body LoginRequest loginRequest);


}

Wednesday, 10 February 2021

Calculator in android studio with Source Code | Calculator in Android studio | Clcoding

 Activity Main :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 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"
android:background="#000"
tools:context=".MainActivity">


<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="1"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="2"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="3"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="4"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="5"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="6"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="7"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="8"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="9"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button0"
android:layout_width="185dp"
android:layout_height="80dp"
android:background="@drawable/new_but"
android:paddingRight="95sp"
android:text="0"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.061"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.957" />

<Button
android:id="@+id/button_equal"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="="
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<Button
android:id="@+id/button_multi"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="×"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="/"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.987"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="+"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button_sub"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="-"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="AC"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="%"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="+/-"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.349"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_dot"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="."
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.671"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<TextView
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="68sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.252" />

<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />

</androidx.constraintlayout.widget.ConstraintLayout>
 
Main Activity :


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b0;
private Button b_equal;
private Button b_multi;
private Button b_divide;
private Button b_add;
private Button b_sub;
private Button b_clear;
private Button b_dot;
private Button b_para1;
private Button b_para2;
private TextView t1;
private TextView t2;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQU = '=';
private final char EXTRA = '@';
private final char MODULUS = '%';
private char ACTION;
private double val1 = Double.NaN;
private double val2;

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

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "1");
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "2");
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "3");
}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "4");
}
});

b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "5");
}
});

b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "6");
}
});

b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "7");
}
});

b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "8");
}
});

b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "9");
}
});

b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "0");
}
});

b_dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exceedLength();
t1.setText(t1.getText().toString() + ".");
}
});

b_para1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MODULUS;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "%");
} else {
t2.setText((int) val1 + "%");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = ADDITION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "+");
} else {
t2.setText((int) val1 + "+");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = SUBTRACTION;
operation();
if (t1.getText().length() > 0)
if (!ifReallyDecimal()) {
t2.setText(val1 + "-");
} else {
t2.setText((int) val1 + "-");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MULTIPLICATION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "×");
} else {
t2.setText((int) val1 + "×");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = DIVISION;
operation();
if (ifReallyDecimal()) {
t2.setText((int) val1 + "/");
} else {
t2.setText(val1 + "/");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_para2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!t2.getText().toString().isEmpty() || !t1.getText().toString().isEmpty()) {
val1 = Double.parseDouble(t1.getText().toString());
ACTION = EXTRA;
t2.setText("-" + t1.getText().toString());
t1.setText("");
} else {
t2.setText("Error");
}
}
});

b_equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
operation();
ACTION = EQU;
if (!ifReallyDecimal()) {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf(val1));
} else {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf((int) val1));
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
CharSequence name = t1.getText().toString();
t1.setText(name.subSequence(0, name.length() - 1));
} else {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
}
}
});

// Empty text views on long click.
b_clear.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
return true;
}
});
}

private void viewSetup() {
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 = findViewById(R.id.button5);
b6 = findViewById(R.id.button6);
b7 = findViewById(R.id.button7);
b8 = findViewById(R.id.button8);
b9 = findViewById(R.id.button9);
b0 = findViewById(R.id.button0);
b_equal = findViewById(R.id.button_equal);
b_multi = findViewById(R.id.button_multi);
b_divide = findViewById(R.id.button_divide);
b_add = findViewById(R.id.button_add);
b_sub = findViewById(R.id.button_sub);
b_clear = findViewById(R.id.button_clear);
b_dot = findViewById(R.id.button_dot);
b_para1 = findViewById(R.id.button_para1);
b_para2 = findViewById(R.id.button_para2);
t1 = findViewById(R.id.input);
t2 = findViewById(R.id.output);
}

private void operation() {
if (!Double.isNaN(val1)) {
if (t2.getText().toString().charAt(0) == '-') {
val1 = (-1) * val1;
}
val2 = Double.parseDouble(t1.getText().toString());

switch (ACTION) {
case ADDITION:
val1 = val1 + val2;
break;
case SUBTRACTION:
val1 = val1 - val2;
break;
case MULTIPLICATION:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case EXTRA:
val1 = (-1) * val1;
break;
case MODULUS:
val1 = val1 % val2;
break;
case EQU:
break;
}
} else {
val1 = Double.parseDouble(t1.getText().toString());
}
}

// Remove error message that is already written there.
private void ifErrorOnOutput() {
if (t2.getText().toString().equals("Error")) {
t2.setText("");
}
}

// Whether value if a double or not
private boolean ifReallyDecimal() {
return val1 == (int) val1;
}

private void noOperation() {
String inputExpression = t2.getText().toString();
if (!inputExpression.isEmpty() && !inputExpression.equals("Error")) {
if (inputExpression.contains("-")) {
inputExpression = inputExpression.replace("-", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("+")) {
inputExpression = inputExpression.replace("+", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("/")) {
inputExpression = inputExpression.replace("/", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("%")) {
inputExpression = inputExpression.replace("%", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("×")) {
inputExpression = inputExpression.replace("×", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
}
}

// Make text small if too many digits.
private void exceedLength() {
if (t1.getText().toString().length() > 10) {
t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}

Popular Posts

Categories

Android (23) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (12) C++ (81) Course (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (38) IS (25) Java (87) Leet Code (4) PHP (20) Projects (10) Python (249) R (69) Selenium Webdriver (2) Software (14) SQL (27)