Friday, 4 July 2025

How to Build a Simple Android App Using XML and Java

How to Build a Simple Android App Using XML and Java

📱 We All Use Mobile Apps Daily…

From WhatsApp to PhonePe and Amazon, mobile apps are part of our everyday life. But have you ever wondered how these apps are made? The answer: tools like Android Studio, and a combination of Java and XML!



In this blog post, I’ll walk you through creating your first simple Android app — a button that changes text when clicked. It’s a great starting point for any beginner.


🧰 Tools You’ll Need:

  • Android Studio (latest version)

  • Basic knowledge of Java and XML


🛠️ Step 1: Set Up a New Project

Open Android Studio and create a new project with:

  • Empty Activity

  • Language: Java

  • Minimum SDK: API 21 or higher


🎨 Step 2: Design the Layout (XML)

Open res/layout/activity_main.xml and write this:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:text="Hello"
        android:textSize="24sp"
        android:layout_marginBottom="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button"
        android:text="Click Me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>


This creates a simple screen with a TextView and a Button.


💻 Step 3: Add Functionality in Java

Open MainActivity.java and add:



public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button button;

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

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setOnClickListener(v -> textView.setText("You clicked!"));
    }
}


This simple code connects the XML UI with Java logic using findViewById and changes the text when the button is clicked.


▶️ Step 4: Run Your App

  • Click Run in Android Studio.

  • Select an emulator or real device.

  • Tap the button and see the message change!


✅ You Just Made Your First Android App!

With just a few lines of code, you’ve learned how XML and Java work together to build a functioning mobile app. This is just the beginning — you can now explore inputs, images, and more advanced logic.


📽️ Bonus:

Want to see this app in action? Check out the YouTube Shorts video I made here:
🔗 Watch on YouTube


🙌 Follow for More!

I’m sharing more beginner-friendly app tutorials and real-time coding tips.
📌 YouTube: @iteacherarmy

Previous Post
Next Post

post written by:

0 comments:

What do you think about this post