FIRST YOU SEE XML CODE;
XML CODE:
Copy this code and paste it on your android studio and set a layout margin and start making your first app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="30sp" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="30sp" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="30sp" />
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="30sp" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="30sp" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:textSize="30sp" />
<Button
android:id="@+id/button_subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height
JAVE CODE;
Copy the JAVA Code and paste on your android studio to make your first app.
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView result;
Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0,
buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
double valueOne, valueTwo;
boolean add, subtract, multiply, divide;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
button1 = findViewById(R.id.button_1);
button2 = findViewById(R.id.button_2);
button3 = findViewById(R.id.button_3);
button4 = findViewById(R.id.button_4);
button5 = findViewById(R.id.button_5);
button6 = findViewById(R.id.button_6);
button7 = findViewById(R.id.button_7);
button8 = findViewById(R.id.button_8);
button9 = findViewById(R.id.button_9);
button0 = findViewById(R.id.button_0);
buttonAdd = findViewById(R.id.button_add);
buttonSubtract = findViewById(R.id.button_subtract);
buttonMultiply = findViewById(R.id.button_multiply);
buttonDivide = findViewById(R.id.button_divide);
buttonEquals = findViewById(R.id.button_equals);
buttonClear = findViewById(R.id.button_clear);
// Set onClick listeners for all the buttons
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText(result.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText(result.getText() + "2");
}
});
// And so on for all the buttons...
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
valueOne = Double.parseDouble(result.getText() + "");
add = true;
result.setText(null);
}
});
buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
valueOne = Double.parseDouble
PASTE THIS CODE AND BUILD YOUR FIRST APP
0 Comments