Simple app
Sun, 03 Nov 2024
MainActivity.java
& activity_main.xml
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="12dp">
<TextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintVertical_bias="0.1"
android:gravity="center"
android:padding="4dp"
android:textSize="26sp"
android:textStyle="bold"
android:fontFamily="cursive"
app:layout_constraintBottom_toTopOf="@+id/buttonPassData"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is dummy data" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/buttonPassData"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvMessage">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_your_name" />
</com.google.android.material.textfield.TextInputLayout>
<android.widget.Button
android:id="@+id/buttonAddPrefix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="@drawable/button_ripple"
android:text="@string/add_prefix"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
<android.widget.Button
android:id="@+id/buttonPassData"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent="0.7"
app:layout_constraintVertical_bias="0.9"
android:background="@drawable/button_ripple"
android:backgroundTint="@color/dark_red"
android:text="@string/pass_data"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Preview:
Initial MainActivity.java
package com.lazymind.myfirstapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
If we run our app, we will see an initial design. But what about the functionality we want?
Till now, we have connected the whole design. But we need to access each component individually to read text, set text etc.
We connect each component by using findViewById(int)
which finds the corresponding object in XML using the id
.
Complete code:
package com.lazymind.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
private EditText editTextName;
private Button buttonAddPrefix;
private Button buttonPassData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMessage = findViewById(R.id.tvMessage);
editTextName = findViewById(R.id.editTextName);
buttonAddPrefix = findViewById(R.id.buttonAddPrefix);
buttonPassData = findViewById(R.id.buttonPassData);
setClickListener();
}
private void setClickListener(){
// shorter way
buttonAddPrefix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = editTextName.getText().toString();
tvMessage.setText( "Hello "+text );
}
});
// Actually what happens internally
ClickHolder clickHolder = new ClickHolder();
buttonPassData.setOnClickListener(clickHolder);
}
class ClickHolder implements View.OnClickListener{
@Override
public void onClick(View view) {
String text = tvMessage.getText().toString();
// Code for passing the data
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data",text);
startActivity(intent);
}
}
}
activity_second.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:padding="4dp"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is dummy data" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity.java
package com.lazymind.myfirstapp;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
private TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tvMessage = findViewById(R.id.tvMessage);
// Receiving the data. Remember "data" was the key while passing
String data = getIntent().getStringExtra("data");
tvMessage.setText(data);
}
}
AndroidManifest.xml
contains two activity. Second one is added automatically bt ide.This is a comment 1.
This is a comment 2.