I have accept and reject button, both to dismiss the dialog box. I also have a condition where the dialog will be removed if there is no action by the user, but it is redrawn after being removed. Upon thorough investigation I found out the dialog box was redrawn right after it is removed. This is my code:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setView(dialoglayout);
builder.setTitle("REQUEST");
builder.setMessage("Would you like to accept?");
//builder.show();
dia = builder.create();
dia.show();
spRequest.play(spSoundId, 1, 1, 1, 15, 1);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dia.dismiss();
}
}, 7000); //the alert will play for 7 seconds and stop
//dia.show();
Button accept = (Button) dialoglayout.findViewById(R.id.bAcceptRequest);
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//mDialog.cancel();
dia.dismiss();
spRequest.release();
}
});
Button reject = (Button) dialoglayout.findViewById(R.id.bDeclineRequest);
reject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//mDialog.cancel();
Log.e("caution", "dismiss reached");
dia.dismiss();
spRequest.release();
}
});
And this is my custom dialog xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/custom_dialog_background">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/bAcceptRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/acceptrequest"
android:background="#drawable/blue_button"
/>
<Button
android:id="#+id/bDeclineRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rejectrequest"
android:background="#drawable/blue_button"
/>
</LinearLayout>
Can someone explain to why the dialog is being redrawn and how to stop that behavior?
Related
I am creating an Alert Dialog on clicking an imageView .Code is given below:
imgEditStatus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileActivity.this);
inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.dialog_edit_status, null);
builder.setView(dialogView);
dialog = builder.create();
dialog.show();
final EditText edtStatus = (EditText) dialogView.findViewById(R.id.editStatus);
Button btnOK = (Button) dialogView.findViewById(R.id.btnOK);
// btnOK.getBackground().setColorFilter(getResources().getColor(R.color.orange_color)), PorterDuff.Mode.SRC_ATOP);
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Toast.makeText(getApplicationContext(), "OK clicked", Toast.LENGTH_SHORT).show();
String statusText = edtStatus.getText().toString().trim();
new UpdateStatusTask().execute(statusText);
}
});
Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});
dialog_edit_status.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/editStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin20"
android:hint="Enter your status here"
android:paddingBottom="#dimen/padding10"
android:padding="#dimen/padding10"
android:textColor="#color/black_color"
android:textColorHint="#color/gray_color"
android:layout_marginBottom="#dimen/margin10"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin10">
<Button
android:id="#+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/margin50"
android:text="OK"
android:background="#color/orange_color"
android:textColor="#color/white_color"/>
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin50"
android:text="Cancel"
android:textColor="#color/white_color"/>
</RelativeLayout>
</LinearLayout>
I am trying to set orange color in the background of OK button.But its size got changed. I tried to use the following code :
btnOK.getBackground().setColorFilter(getResources().getColor(R.color.orange_color)), PorterDuff.Mode.SRC_ATOP);
it is giving me error :setColorFilter() in Drawable can not be applied to (int) . Please help me to fix the issue
If you want to change the background color but retain other styles, then below might help.
btnOK.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
try this:
btnOK.setBackgroundColor(getResources().getColor(R.color.orange_color));
btnOK.setBackgroundResource(R.color.orange_color);
I am absolute beginner to Android. Now I am having a problem with setting the width of default AlertDialog with custom view in Android. It is not resizing the width of the alert dialog. What is wrong with my code ?
This is the view layout of my alert dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textColor="#color/white"
app:backgroundTint="#color/green"
android:layout_gravity="center_horizontal"
android:id="#+id/btn_row_option_done"
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:textColor="#color/white"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="#color/lightBlue"
android:layout_gravity="center_horizontal"
android:text="Edit"
android:id="#+id/btn_row_option_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:textColor="#color/white"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="#color/red"
android:layout_gravity="center_horizontal"
android:text="Delete"
android:id="#+id/btn_row_option_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="#color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="#+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
As you can see, I set the width of the linearLayout to wrap_content.
This is how I am opening the alert dialog in my Java code
public void showOptionDialog(final int id)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.row_option_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
Boolean isTaskDone = dbHelper.isTaskDone(id);
Button doneBtn = (Button)view.findViewById(R.id.btn_row_option_done);
if(isTaskDone==false)
{
doneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbHelper.markAsDone(id);
refreshListView();
alertDialog.cancel();
Toast.makeText(getActivity().getBaseContext(),"Marked as done",Toast.LENGTH_SHORT).show();
}
});
}
else{
ViewGroup viewGroup = (ViewGroup)doneBtn.getParent();
viewGroup.removeView(doneBtn);
}
Button editBtn = (Button)view.findViewById(R.id.btn_row_option_edit);
if(isTaskDone==false)
{
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity activity = (MainActivity) getActivity();
activity.replaceEditTaskFragment(id);
alertDialog.cancel();
}
});
}
else{
ViewGroup viewGroup = (ViewGroup)editBtn.getParent();
viewGroup.removeView(editBtn);
}
Button deleteBtn = (Button)view.findViewById(R.id.btn_row_option_delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbHelper.deleteTask(id);
items.remove(optionFocusedItemIndex);
adapter.notifyDataSetChanged();
Toast.makeText(getActivity().getBaseContext(),"Task deleted",Toast.LENGTH_SHORT).show();
alertDialog.cancel();
updateListEmptyText();
}
});
Button cancelBtn = (Button)view.findViewById(R.id.btn_row_option_cancel);
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.cancel();
}
});
alertDialog.setView(view);
alertDialog.show();
}
But when the alert dialog is opened, its width is not resized and still showing the default size like in screenshot.
This is the screenshot.
As you can see, width is nearly full of screen even I did set to wrap_content. I also set something like 300px. It is not working. How can I achieve this?
First make other layout for container and inside it make this linear layout with wrap content. After that you can make your alpha background of the container almost to 0 to be transparent. The dialog inside with the buttons is on the inner layout. For custom dialog you cant use this AlertDialog, make your own activity which behavior is like a dialog.
set required size here
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300px"
android:layout_height="match_parent">......</LinearLayout>
or
alertDialog.show();
alertDialog.getWindow().setLayout(300, 300);
Change this line
alertDialog.setView(view);
To
alertDialog.setContentView(view);
Here view is the instance of your custom layout.
I have a button that calls a dialog. From that button i have 8 buttons: 1,2,3,4,5,6,7, and cancel. These buttons will be used to change the text of the button. The thing is that it doesn't do anything if i set the text inside the dialog.
buttonDefineHits = (Button) rowView.findViewById(R.id.button_define_hits);
buttonDefineHits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
setDialogSetHits();
}
});
.
private void setDialogSetHits(){
final Dialog dialogConfirmPlayers = new Dialog (activity);
dialogConfirmPlayers.setCancelable(false);
dialogConfirmPlayers.setContentView(R.layout.dialog_set_hits);
Button button1Hit = (Button) dialogConfirmPlayers.findViewById(R.id.button_1_hit);
button1Hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog 1-7 i x para definir los holes
buttonDefineHits.setText("1");
dialogConfirmPlayers.cancel();
}
});
dialogConfirmPlayers.show();
}
You can set the Text of a Button that is defined in a activity from the dialogBox. I guess Aniruddha is wrong in his comment. I mean yes the user cannot have a "Iteration" with the Activity's UI elements as long as a Dialog is shown over it, But programatically you can change the Text property of the Button in your activity. To confirm, this is what I tried:
Created a Dialog on the click event of ImageView.
From the Dialog Button's Click listener, I changed the Text of a editText in the Activity.
Similarly, you should also be able to set the text of the button from the dialog button's click listener.
I think you should remove the 7 buttons from your dialog, and for testing purpose just have one button on it. Then handle the click event on this button and try n set the Activity button's Text. This should work like charm.
Then later you can integrate your 7-buttons.
Here is the working example, you need to modify it accordingly
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv1"
android:layout_below="#+id/tv1"
android:layout_marginTop="44dp"
android:text="Button" />
</RelativeLayout>
dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
TextView t;
Button bMain;
Dialog d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.tv1);
bMain = (Button) findViewById(R.id.buttonMain);
bMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d = new Dialog(MainActivity.this);
d.setTitle("Hello Android..!");
d.setContentView(R.layout.dialog_view);
Button bOK = (Button) d.findViewById(R.id.button1);
Button bCancel = (Button) d.findViewById(R.id.button2);
d.show();
bOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("OK");
bMain.setText("Changed the text");
d.cancel();
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have an Text View on which when user click will open a alert dialog. The dialog consists of Edit Text and a button. In Edit text when user enters it's height and clicks on "OK" button,the entered height will gets displayed to the Edit Text.
MainActivity.java
et_weightAndHeight = (EditText) findViewById(R.id.et_weightAndHeight);
et_weightAndHeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(AccountActivity.this);
View promptView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountActivity.this);
alertDialog.setView(promptView);
final EditText input = (EditText)findViewById(R.id.editTextDialogUserInput);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
et_weightAndHeight.setText(input.getText().toString());
}
});
AlertDialog aD = alertDialog.create();
aD.show();
}
});
}
}
prompts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Weight in lbs : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
Without proper explanation and logcat, I am assuming you are getting NullPointerException because you haven't initialized the TextView inside the dialog properly.
Change,
final TextView input =(TextView)findViewById(R.id.editTextDialogUserInput);
to
final TextView input =(TextView)promptView.findViewById(R.id.editTextDialogUserInput);
This question already has answers here:
How to prevent a dialog from closing when a button is clicked
(21 answers)
Closed 3 years ago.
The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.
Thanks..
You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:
/*
* Prepare the alert with a Builder.
*/
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setNegativeButton("Button", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
});
this.alert = b.create();
/*
* Add an OnShowListener to change the OnClickListener on the
* first time the alert is shown. Calling getButton() before
* the alert is shown will return null. Then use a regular
* View.OnClickListener for the button, which will not
* dismiss the AlertDialog after it has been called.
*/
this.alertReady = false;
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
if (alertReady == false) {
Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
alertReady = true;
}
}
});
Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#
Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="180dip"
android:digits="1234567890"
android:maxLength="4"
android:password="true"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/Accept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accept"/>
</LinearLayout>
</LinearLayout>
Then when you want it to pop up:
final Dialog dialog = new Dialog(RealizarPago.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("PIN number:");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.Accept);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(password_wrong){
// showToast
} else{
dialog.dismiss();
// other stuff to do
}
}
});
dialog.show();
You can set an OnClickListener as follows to keep the dialog open:
public class MyDialog extends AlertDialog {
public MyDialog(Context context) {
super(context);
setMessage("Hello");
setButton(AlertDialog.BUTTON_POSITIVE, "Ok", (new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// this will never be called
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ok) {
// do something
dismiss();
} else {
Toast.makeText(getContext(), "when you see this message, the dialog should stay open", Toast.LENGTH_SHORT).show();
}
}
});
}
}
You can just continue using the dialog you already have, just put an if clause in the onClick() saying
if(pin_check_method){ //pin_check_method should be a boolean returned method
//close the Dialog, then continue
}
else{
//dont put the dialog.dismiss() in here, put instead
Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show();
}
Now, to use this code, simply invoke text.setText(""); and put in the text you want here
common error is that when you type in:
TextView text = (TextView) findViewById(R.id.dialog);
you miss that it needs to actually be
dialog.findViewById
and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/text"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"/>
<Button android:text="Continue"
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/text">
</Button>
</RelativeLayout>
Try this:
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Do something
}
});
}
});
alertDialog.show();
Source: Prevent Alertdialog from closing after button click
Hope This Helps! Good Luck!
Same problem for me in a FragmentDialog. Here's my criminal/elegant solution:
Remove all buttons from the dialog (positive,negative,neutral). Add your buttons from the xml.eg.:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/cancel"
android:layout_gravity="left"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_ok"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/ok"
android:layout_gravity="right"
/>
</LinearLayout>
And then in your code handle it with:
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
if (wannaClose)
dismiss();
else
//do stuff without closing!
}
});
where view is the view assigned to the dialog!