Introduction: HelloWorld With Exit Button AndroidStudio

This tutorial will teach you on how to build Android App that display Hello World text and Exit button to exit from the activity.

Step 1: Create New Project

Open Android Studio and create new project. Name your new project as HelloWorld and add Empty Activity.

Step 2: Edit the Activity_main.xml

Add a new text view and button (as shown below) inside the res>layout>activity_main.xml.

<TextView        
	android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:foregroundGravity="center"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView" />
<Button
android:id="@+id/btn_logout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:text="Exit" android:textColor="@color/black"/>

Since we using colors for the text, add a new resource in the color.xml. Go to res>values>colors.xml and add the following code

<resources>

<color name="black">#0d0c0c>

<resources/>

Step 3: Now, Edit the MainActivity.java

Add the following code to the OnCreate() method in the MainActivity.java

We add OnClickListener function to the button so whenever user clicked on the button a dialog will popup with a warning "Do you want to exit?". Two options will be provided to the user, "Yes ..." to exit and "Not ..." to close the dialog and go back to main.

Button btnlogout = (Button) findViewById(R.id.btn_logout);

btnlogout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Exit");
        builder.setMessage("Do you want to exit ??");
        builder.setPositiveButton("Yes. Exit now!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 

                finish();

            }
        });
	    builder.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 

               dialogInterface.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }
});

Step 4: Finish!

Now, you may run the app.

Good Luck!

The full code is here:https://github.com/mLynnhope/HelloWorld

Comments

author
DIY+Hacks+and+How+Tos made it!(author)2017-04-16

It is amazing just how many different ways that I have seen Hello World be implemented.

About This Instructable

87views

0favorites

License:

Bio: To continue learning to gain more knowledge in life and use the best of my knowledge to give contribution to the people.
More by MLynn93:HelloWorld With Exit Button AndroidStudio
Add instructable to: