Last active
February 10, 2021 09:25
-
-
Save MartinMatta/6589ecffe558d271aa330536bf278599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import androidx.appcompat.app.AppCompatActivity; | |
| import androidx.appcompat.app.AlertDialog; | |
| import android.view.LayoutInflater; | |
| import android.content.Context; | |
| import android.os.Handler; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| public class MainActivity extends AppCompatActivity { | |
| final Context c = this; | |
| boolean doubleBackToExitPressedOnce = false; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| } | |
| @Override | |
| public void onBackPressed() { | |
| if (doubleBackToExitPressedOnce) { | |
| LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c); | |
| View mView = layoutInflaterAndroid.inflate(R.layout.exit_dialog, null); | |
| AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(c); | |
| alertDialogBuilderUserInput.setView(mView); | |
| AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create(); | |
| Button btnCancel = (Button) mView.findViewById(R.id.btnCancel); | |
| Button btnOk = (Button) mView.findViewById(R.id.btnOk); | |
| btnCancel.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| alertDialogAndroid.dismiss(); | |
| } | |
| }); | |
| btnOk.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| finish(); | |
| System.exit(1); | |
| } | |
| }); | |
| alertDialogAndroid.show(); | |
| } | |
| this.doubleBackToExitPressedOnce = true; | |
| new Handler().postDelayed(new Runnable() { | |
| @Override | |
| public void run() { | |
| doubleBackToExitPressedOnce=false; | |
| } | |
| }, 2000); | |
| } | |
| } | |
| /* exit_dialog.xml | |
| <?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:id="@+id/connect_dialog" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| android:padding="16dp" | |
| tools:ignore="MissingClass"> | |
| <ImageView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="center" | |
| android:paddingTop="32dp" | |
| android:paddingBottom="32dp" | |
| android:src="@drawable/exit"/> | |
| <TextView | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:gravity="center" | |
| android:paddingBottom="16dp" | |
| android:textSize="16dp" | |
| android:textStyle="bold" | |
| android:text="Exit text"/> | |
| <TableLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:paddingTop="16dp"> | |
| <TableRow | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:gravity="center"> | |
| <Button | |
| android:id="@+id/btnCancel" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginRight="8dp" | |
| android:scaleY="1.2" | |
| android:text="Cancel" | |
| android:layout_column="1" | |
| app:backgroundTint="@android:color/holo_red_dark"/> | |
| <Button | |
| android:id="@+id/btnOk" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginLeft="8dp" | |
| android:scaleY="1.2" | |
| android:text="ok" | |
| android:layout_column="2" | |
| app:backgroundTint="@android:color/holo_green_light"/> | |
| </TableRow> | |
| </TableLayout> | |
| </LinearLayout> | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment