Android Alert Dialog
Android Development |
Android Alert Dialog :-
⇛ layout > .xml 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"
android:gravity="center"
tools:context=".First">
<TextView
android:id="@+id/Idtextview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="If you press back button in your mobile then exit"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"/>
</LinearLayout>
<?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"
android:gravity="center"
tools:context=".First">
<TextView
android:id="@+id/Idtextview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="If you press back button in your mobile then exit"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"/>
</LinearLayout>
⇛ java file :
package com.example.dell.alertdialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TabHost;import android.widget.Toast;public class First extends AppCompatActivity {private long onbackPressed;private Toast backToast;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);}//Override this method press Ctrl+O .@Overridepublic void onBackPressed() {// here 2000 means 2 second.if (onbackPressed+2000 > System.currentTimeMillis()){backToast.cancel();super.onBackPressed();return;}else {backToast = Toast.makeText(getApplication(),"Press back again to exit",Toast.LENGTH_SHORT);backToast.show();}onbackPressed = System.currentTimeMillis();}}
No comments