I create a custom view which showing the simple game
I set the custom view in the MainActivity
setContentView(new CustomView())
In this custom view, only have few ball and the timer
When a ball touch another ball. The timer will stop and show the result.
I don't know how to show the result in a better way. So i tried to create a dialog to show the result.
This code is write in the CustomView class
if (ballIsTouch) {
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
alertDialog.setTitle("Result");
alertDialog.setMessage(point);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
However, the page is freezed. The dialog haven't show.
create() - is a final step -when you use builder pattern to create dialog u first set in chain all variables and then create dialog - and show so it should be
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
alertDialogBuilder.setTitle("Result");
alertDialogBuilder.setMessage(point);
alertDialogBuilder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
or
alertDialogBuilder.setTitle("Result")
.setMessage(point)
.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ensure you getContext() is valid (should be activity, service context)**
in activity use this or Myactivity.this in interface/callback in fragment getActivity()
**see Dialog creation context
Related
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
TextView titleView = new TextView(this);
EditText editTextView = new EditText(this);
titleView.setText("PASSWORD");
editTextView.setHint("password");
editTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
titleView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
dialogBuilder.setCustomTitle(titleView);
dialogBuilder.setView(editTextView);
dialogBuilder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface DialogInterface, int i) {
String editTextInput = editTextView.getText().toString();
}
});
dialogBuilder.setNegativeButton("dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
This is my alert dialog . When ,dialog shows, keyboard gets up and editText has focus , i tried to force hidding keyboard different ways but without success so far . I looking forward to show this diakog while keeping keyboard down .
I am new to Android Studio programming. My program has a recyclerview with 3 Textviews, and one image. I would like an alert dialog to appear based on where the user clicks on the screen, for instance if the user clicks on the image the dialog says, "You clicked on image". If the user clicks on the text, it would say "You clicked on the text".
My onclick listener is as follows: itemView.setOnClickListener(this);
#Override
public void onClick(View view) {
int index = getAdapterPosition();
if (playerNames.equals(index)) {
showDialogPlayerNames();
}
if (numberPlayers.equals(index)){
showDialogPlayerNumber();
}
if (ScorePlayers.equals(index)){
showDialogPlayerScore();
}
if (picturePlayers.equals(index)) {
showDialogPlayerImage();
}
}
}
Here is my alert dialog code:
void showDialogPlayerNames() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Click Information");
alertDialog.setIcon(R.drawable.ic_baseline_info_24);
alertDialog.setMessage("You clicked Player Name");
alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.create().show();
}
Thank you
You need to add parameters in your methods
showDialogPlayerNames("Something text here");
void showDialogPlayerNames(String myText) {
alertDialog.setMessage(myText);
}
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
so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog(); that I created but I can't dismiss it using alertdialog.dismiss(); inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?
The method:
private void showCustomDialog() {
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
I am calling the meathod and using it as follows:
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCustomDialog();
alertbutton1.getId();
alertbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("500");
function();
//I want to dismiss the alertdialog here.
}
});
alertbutton2.getId();
alertbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("1000");
function();
//I want to dismiss the alertdialog here.
}
});
and so on.
I figured out a solution for you using AlertDialog.
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
}
});
Then the method MUST be a private or public AlertDialog:
private AlertDialog createDialog() {
LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
View v = inflater.inflate(R.layout.dialog_add_new_list, null);
Button okButton = v.findViewById(R.id.confirm_button);
Button cancelButton = v.findViewById(R.id.cancel_button);
final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
.setView(v)
.show();
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p1) {
dialog.dismiss();
}
});
return dialog;
}
P.S. Replace the ids I used to create this sample with yours.
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);