I'm trying to create an AlertDialog to show an introduction message in my application, with a "Don't show this again" CheckBox below it.
It works well when the message of the AlertDialog is short, but when is too long (requiring scrolling) the CheckBox is no longer shown. It gets "pushed out" by the TextView.
The XML for the custom view (dont_show_again.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Don't show this again">
</CheckBox>
</LinearLayout>
And the code to display the AlertDialog
String longMessage = getString(R.string.long_message);
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setMessage(longMessage)
.setView(checkboxLayout)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
Any ideas on how to solve this? Perhaps someone has an example of a working AlertDialog with a "Don't show this again" CheckBox?
Don't set the dialog box message, instead include it in the layout XML as textView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="3"
android:scrollbars="vertical"/>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't show this again">
</CheckBox>
</LinearLayout>
Use the XML layout from the answer earlier.
String longMessage = getString(R.string.long_message);
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);
CheckBox cb = (CheckBox)checkboxLayout.findViewById(R.id.checkBox);
TextView tv = (TextView)checkboxLayout.findViewById(R.id.message);
tv.setText(longMessage);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setView(checkboxLayout)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
Related
I am creating a About us screen as a popup/dialog in android. I want to add a button (OK or CANCEL) to this dialog. How can I do that ?
This is my layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="#+id/popup"
android:layout_height="wrap_content"
android:background="#E3C39D"
android:orientation="vertical"
android:padding="0dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About Us.."
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:textSize="20dp"
android:textColor="#ffffff"
style="#style/TextShadow"/>
<View
android:id="#+id/SplitLine_hor1"
android:layout_width="match_parent"
android:layout_height= "1dp"
android:background="#000" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:text="Hello ! I want to put a button below with label 'OK' and Click on this OK button the popup should be close. Thank you !" />
</LinearLayout>
and below is the function for Dialog box
public void AboutUsDialog(){
final AlertDialog.Builder alert;
alert = new AlertDialog.Builder(this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.activity_about_us, null);
alert.setView(dialogView);
alert.show();
alert.setPositiveButton("OK",null);
//alert.setInverseBackgroundForced(true);
}
I am using alert.setPositiveButton("OK",null); or alert.setInverseBackgroundForced(true);. But I did not get any button displayed in dialog.
Now Dialog gets OFF when I touch anywhere on the screen. I want to close popup through OK button only.
Thanks in advance!
Output
Your button is not coming because it should be like this
alert.setPositiveButton("OK",null);
alert.setView(dialogView);
alert.show();
Instead of
alert.setView(dialogView);
alert.show();
alert.setPositiveButton("OK",null);
Refer this :
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Try and let me know
Below is my code to add buttons.
public void AboutUsDialog() {
final AlertDialog.Builder alert;
alert = new AlertDialog.Builder(this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.activity_about_us, null);
alert.setPositiveButton("OK", null); //This is my Solution to this question(adding OK button)
alert.setCancelable(false);
alert.setInverseBackgroundForced(true);
alert.setView(dialogView);
alert.show();
//alert.setInverseBackgroundForced(true);
}
I made an alert box that have a title, a description for every option and three button. Now I saw that I need another button! But I saw that exist only negative, neutral and positive button. This is my code:
alertDialog = new AlertDialog.Builder(Home.this);
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));
alertDialog.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do a thing
}
});
alertDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do a thing
}
});
alertDialog.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do a thing
}
});
alertDialog.show();
And alert_dialog_layout.xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title"
android:gravity="center"
android:textSize="20sp"
android:textColor="#color/white"
android:background="#drawable/button_blue" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/padding_and_margin"
android:text="#string/text4"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/padding_and_margin"
android:gravity="center"
android:text="#string/text5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/padding_and_margin"
android:gravity="center"
android:text="#string/text6"
android:layout_marginBottom="#dimen/padding_and_margin" />
</LinearLayout>
I want another button because I have 4 option. Is possible to use alert button or I have change the view?
Is possible to change the layout of the 3 (or 4) button that I automatically made?
Thanks for answer
Inflate the AlertDialog with your own custom view (alert_dialog_layout.xml) and place the buttons in your alert_dialog_layout.xml
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.alert_dialog_layout, (ViewGroup) findViewById(R.id.root));
Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();
You have to create custom layout.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
Create your own layout and Inflate it to get the View and follow the link for the rest.
Basically what I want to do is closing Spinner's Dialog Box and the Customized Dialog I've created when clicked outside of those boxes. Anyone knows how to handle these?
dialog.xml, My Spinner's xml, MakeandShowDialogBox function below :
dialog.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" >
<TableLayout
android:id="#+id/scheduleTable"
android:layout_height="wrap_content"
android:layout_width="match_parent"
></TableLayout>
</LinearLayout>
Spinner XML
<Spinner
android:id="#+id/spinList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/strDepartmentNames"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:spinnerMode="dialog"
android:paddingTop="15dp"
android:paddingBottom="15dp" />
makeAndShowDialogBox
private void makeAndShowDialogBox() {
AlertDialog.Builder myDialogBox = new AlertDialog.Builder(this);
final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = layoutInflater.inflate(R.layout.dialog, null);
// Set three option buttons
myDialogBox.setPositiveButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
myDialogBox.setView(dialogView);
myDialogBox.create();
myDialogBox.show();
}
you just add this option in your dialog
myDialogBox.setCancelable(true);
Did you try to call this method setCanceledOnTouchOutside(true).
myDialogBox.setCanceledOnTouchOutside(true);
Afaik It should work.
I am trying to design AlertDialog, but I can't get rid of the black surrounding (As indicated by the red line), and the orange surrounding (As indicated by the blue line).
This is my xml code:
<?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"
android:background="#android:color/darker_gray" >
<TextView
android:src="#drawable/save_as"
android:id="#+id/label_save_as"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/save_as" />
<EditText
android:id="#+id/filename"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/filename" />
</LinearLayout>
This is the code:
AlertDialog.Builder saveAsBuilder = new AlertDialog.Builder(this);
LayoutInflater saveAsInflater = this.getLayoutInflater();
saveAsBuilder.setView(saveAsInflater.inflate(R.layout.saveas, null))
.setNeutralButton(R.string.saveAsImg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do something
}
})
.setPositiveButton(R.string.saveAsSkt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
d = saveAsBuilder.create();
d.setCanceledOnTouchOutside(true);
d.show();
How can I get rid of those?
BTW , I am using API 10.
Once try to change like this and try..
AlertDialog.Builder saveAsBuilder = new AlertDialog.Builder(this,android.R.style.Theme_Translucent);
setting the theme for the dialog to remove the black spaces
with using dialog..
Dialog dialog=new Dialog(getApplicationContext());
dialog.setContentView(R.layout.yourlayout);
for this you have to create two buttons in layout and implement listeners for that..
Does anyone know how to save the text that the user will input after a dialog Box pops up?
I'm first inflating a view for the dialog Box, then letting the user input a string and displaying it somehow. Here's my code for the Dialog Box inflating. My question is How do I save the text that someone inputs into my field from the AlertDialog Box. My XML is at the bottom
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater mInflater = LayoutInflater.from(this);
final View textEntryView = mInflater.inflate(
R.layout.alert_text_entry, null);
return new AlertDialog.Builder(AlarmList.this)
.setIcon(R.drawable.alert_icon)
.setTitle("Enter your name")
.setView(textEntryView)
.setPositiveButton("accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNegativeButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
}
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="12dp"
android:textSize="16sp" >
</TextView>
<EditText android:id="#+id/alarm_name_text"
android:layout_height="3dp"
android:text=""
android:layout_below="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_alignLeft="#+id/rowTextView"
android:layout_alignRight="#+id/rowTextView">
</EditText>
<CheckBox android:id="#+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
As mentioned in EditText docs, you can call getText() to get the text from the EditText view.