How to turn off an app with a button in android - android-studio

i try to implement a way to turn off an app with a button. If i push the button, the app seems to being closed, but if i look which applications a currently running, my app is still active.
Here ist my code:
turnOffApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finishAffinity();
System.exit(0);
}
});

Related

Cannot create dialog inside a fragment

I am trying to create a dialog inside a fragment.when I am trying to press on the button and enter the dialog the app collaspe.
I guess the code is not right can you please help me with that?
Here is my code:
private void openDialog(){
Dialog dialog=new Dialog(getContext());
//AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater=this.getLayoutInflater();
View custom_dialog=getActivity().getLayoutInflater().inflate(R.layout.geo_dialog,null);
dialog.setContentView(custom_dialog);
// add_geofence_radius= custom_dialog.findViewById(R.id.radius_size);
save_btn=custom_dialog.findViewById(R.id.save_btn);
cancel_btn=custom_dialog.findViewById(R.id.cancel_btn);
/* save_btn.setOnClickListener(new View.OnClickListener() {
#Override
}
});
*/
/*cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
*/
// dialog.setTitle("hello");
dialog.show();
}
The best way to show dialog in android is to use "DialogFragments" since they are aware of the lifecycle of the view it is attached on (ie. fragments/activities).
Here is an examples provided in Android docs:
public class PurchaseConfirmationDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
.create();
}
public static String TAG = "PurchaseConfirmationDialog";
}
To show the dialog use:
new PurchaseConfirmationDialogFragment().show(
getChildFragmentManager(), PurchaseConfirmationDialog.TAG);
For more reference on dialogFragments, checkout : Create a DialogFragment

Android Studio - AlertDialog button listener, and view of ProgressBar

This is the code to test the thread and how it works! (It's not really connecting just to look like one) I wanted to add a progress bar when the alert dialog is gone by pressing the yes button, but it doesn't work. I set the Visibility to gone before, and visible to after. What seems to be the problem? I done casting right, and there's nothing wrong elsewhere. The text doesn't change also..... please help :)
private void connectRequest() {
AlertDialog.Builder builder = new AlertDialog.Builder(ThreadActivity2.this);
AlertDialog dialog = builder.setMessage("Do you request Remote Access?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
accessTv.setText("Requesting Remote Access ...");
accessPgb.setVisibility(View.VISIBLE);
try {
Thread.sleep(5700);
} catch (InterruptedException e) {
e.printStackTrace();
}
accessTv.setText("Server Access Successful!");
accessPgb.setVisibility(View.GONE);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
Try using java.util.concurrent.TimeUnit:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or
TimeUnit.MINUTES.sleep(1);

I want to make Edit text clickable so that when i click on it, it should show a text like one android studio

this is my code I want to add a text whenever I click on edit text.
`jet1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
jet1.setText("One");
}
});`
To make an editText clickable
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// do things you want
}
});

Unexplained delay between ad onAdClosed and finish

After clicking back button, dialog is opened asking if user want to save his work before returning to menu activity (finishing current activity).
If there is ad loaded, it will show and the activity is finished on onAdClosed.
But there is unexplained delay between closing ad and returning to the menu. If the ad is displayed for some time, the delay disappear. When the ad is not loaded so it will not be shown, finishing is smooth too.
mInterstitialBack.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
finish();
}
});
builderSingle.setNegativeButton(getString(R.string.noSave),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (mInterstitialBack.isLoaded()) {
mInterstitialBack.show();
} else {
finish();
}
}
});

How to put some delay in calling an activity after clicking button on an another activity

I am developing a simple app. On one of my activity, there is button which calls the another activity. The another activity starts immediately, but I want to put some delay before opening the next activity after clicking the button.
Handler and Timer methods are not working. I have no idea, What to do???
public class thumb extends ActionBarActivity {
ImageButton show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thumb);
show=(ImageButton)findViewById(R.id.imageButton);
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),pop.class);
startActivityForResult(intent, 0);
}
});
}
}
Have you tried Thread.sleep(1000);? This will stop the execution by 1000 milliseconds.

Resources