Build your first Android App in Android Studio

Build your first Android App in Android Studio

Build your first Android App in Android Studio:

Hello beginners, Welcome to Approid Technologies coding world.

Today I am going to show you how you can start building your first Android App! 🙂

Coding is fun thing. Play with android tools and you will find it really awesome. Here are some tools for creating a development environment. I prefer Android studio as a best choice!

So Download Android Studio here. Once you have downloaded, follow this easy tutorial to install it correctly on you windows.

So we have all setup now! Great 🙂 Lets have some fun now!

 Step 1. Open your Android Studio & follow the screen shots.

After successfully creating your project in android studio, you will have to understand the application structure, Basically we have two areas

1- Design

2- Development

For Designing your app go to MyfirstApp/app/src/main/res/ location, you will see some xml format files inside your layout directory, One is called activity_main.xml is your application’s launching page.

For Developing your app go to MyfirstApp/app/src/main/java/ location & here you will find your java classes to add some functionality in you UI.

Go to activity_main inside MyfirstApp/app/src/main/res/layout & put these lines of code in it.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="222dp" />
</RelativeLayout>

This view has One button in it saying “Hello World” and has an id of named “button”.

Now go to MyfirstApp/app/src/main/java/app.myfirstapp.com.myfirstapp/MainActivity.java & put following lines of code in it.

package app.myfirstapp.com.myfirstapp;
 
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
 
public class MainActivity extends ActionBarActivity {
    Button btnHelloWorld;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnHelloWorld=(Button)findViewById(R.id.button);
        btnHelloWorld.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Hello World from my First App ;)",Toast.LENGTH_LONG).show();
            }
        });
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}

In this set of code we are actually connecting the button from its id & add a click listener which instantiate when we click on a button and show a toast message!

That’s it we have done all for our first android app. Now Just click on the run button & add emulator or run it on your real android device, when you click on the button “Hello World” it will show a toast message!

Cheers you have successfully develop your first Android Application. 🙂

Comment below for any query.

Leave a Reply