WhatsApp Image 2019-04-02 at 6.38.13 PM (1)

Create A Basic Hello World App Using Button

In this tutorial we will see how to make a Hello world app with android studio. Its a very simple project then what special in it ? Well, we are going to create it using a button. In today’s tutorial we’ll teach you how to use button, explain you how a button works and how can you implement it in your app.

Step 1: open your android studio app.

Step 2: click on a new project -> give any name as you like.

Step 3: choose an empty activity, and then click next.

Step 4: don’t change any other settings such as save location, package name or api level.

Step 5: let the project load the modules and sync itself.

Step 6: copy the following code in main_activity.java file.

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 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:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity"
     android:orientation="vertical"
     android:layout_margin="12dp">
 
 
 
     <TextView
         android:id="@+id/button_click"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="click below"
         android:paddingLeft="12dp"
         android:paddingBottom="12dp"
         android:textAlignment="center"
         />
 
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="active_click"
         android:text="click here" />
 
 
 </LinearLayout>

In the above written code, what I have done is changed the default “android.support.constraint.ConstraintLayout” to LinearLayout for no special reason but because I prefer it to keep things in straight order. I have kept orientation in vertical order .it will help our views and text in linear order or it may appear in horizontal order.

  Then we removed all the text inside the Text view and set the layout_width and height to match parent and added the text “click below”. And we have kept the layout in such a way that they don’t get cramped.

Now here come the important part, we have created an id for text view because when the button will be clicked the it shouldknow where operation should take place so we need to assign an id to it. Then  we create a button view with text  “click here” and give it action to perform called ”active_Part” by calling onclick method.

Step 7: copy the following code in main_activity.java file.

package com.example.bunkers_demo;
 
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 
 public class MainActivity extends AppCompatActivity {
     String click="Hello world";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
     }
 
 
     public void active_click(View view){
 
         display(click);
     }
     public void display(String view) {
         TextView displayThis = (TextView) findViewById(R.id.button_click);
         displayThis.setText(String.valueOf(view));
     }
 
 }

When the button is clicked it calls active_click method and it displays the value of variable present inside “click”. But you need to create a variable of String type of any name (I have kept mine as “click”) and set the value as ” Hello world” but how does it know where to display for that you will create a function called “display” of String type view. It uses the value stored in variable”click”and display the in id button_click using find view by id method in it. It displays the text view value id then we use setText method so that the updated value of string type updates the text in text view outside the app.

Step 8: after following the steps above you app will look like this.

 

So with this we have learn’t what a button is, how it works and how can it be implemented in your app, you can go to android studio and make changes to see how it works and understand better. We have kept it simple so that it can only display a word but we can design it to perform arithmetic calculations. But that’s for different lesson stay tuned and visit our website for more solutions.