Simple Android App
Simple Android App
Creating an Android app with three activities that capture and link user details
1. Setting Up Your Project
• Open Android Studio and create a new project.
• Select an "Empty Views Activity" template and name your project.
2. Resources
Let's create the required XML files for strings, colors, dimens, and styles. These files will help
you manage resources efficiently.
strings.xml
Define all the string resources your app will use:
<resources>
<string name="app_name">UserDetailsApp</string>
<string name="name_prompt">Enter Your Name:</string>
<string name="email_prompt">Enter Your Email:</string>
<string name="phone_prompt">Enter Your Phone Number:</string>
<string name="summary_title">Summary of Details</string>
<string name="name_label">Name:</string>
<string name="email_label">Email:</string>
<string name="phone_label">Phone:</string>
<string name="next_button">Next</string>
</resources>
colors.xml
Define all the colors used in your app:
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="colorText">#FFFFFF</color>
</resources>
dimens.xml
Define all the dimensions used in your app:
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="text_size">18sp</dimen>
<dimen name="margin_top">20dp</dimen>
<dimen name="margin_large">50dp</dimen>
</resources>
styles.xml
Define the styles and themes for your app:
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@color/colorText</item>
</style>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Name:"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvName"
android:layout_margin="20dp"
android:hint="Name" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_below="@id/etName"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
DetailsActivity Layout (activity_details.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Email:" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Phone Number:" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
</LinearLayout>
SummaryActivity Layout (activity_summary.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/tvSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Summary of Details"
android:textSize="18sp"
android:layout_marginBottom="30dp"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvSummary"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvName"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvEmail" />
</RelativeLayout>
3. Linking Activities
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.etName);
btnNext = findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
}
}
DetailsActivity.java
public class DetailsActivity extends AppCompatActivity {
private EditText etEmail, etPhone;
private Button btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
etEmail = findViewById(R.id.etEmail);
etPhone = findViewById(R.id.etPhone);
btnNext = findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etEmail.getText().toString();
String phone = etPhone.getText().toString();
Intent intent = new Intent(DetailsActivity.this, SummaryActivity.class);
intent.putExtra("name", getIntent().getStringExtra("name"));
intent.putExtra("email", email);
intent.putExtra("phone", phone);
startActivity(intent);
}
});
}
}
SummaryActivity.java
java
public class SummaryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summary);
tvName = findViewById(R.id.tvName);
tvEmail = findViewById(R.id.tvEmail);
tvPhone = findViewById(R.id.tvPhone);