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.
Related
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.
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..
I have an Alert Dialog, within the layout for the dialog there is a button that I want to be clickable, however when I use a onclick listener the button still dosn't work.
My Alert Dialog is built up as such.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.distro_editor_popup, (ViewGroup) findViewById(R.id.layout_root));
txt_Name = (EditText) layout.findViewById(R.id.txt_LinuxName);
txt_Image = (EditText) layout.findViewById(R.id.txt_ImageName);
filemanger = (Button) layout.findViewById(R.id.fileselector);
txt_Name.setText(selected_Name);
txt_Image.setText(selected_Image);
final String oldName = selected_Name;
final String oldImage = selected_Image;
filemanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}
});
alert.setView(layout);
alert.setPositiveButton(R.string.dialog_button_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name = txt_Name.getText().toString();
String image = txt_Image.getText().toString();
// Make sure the user entered a name
if (name.equals("")) {
return;
}
if (!oldName.equals(name)) {
// Name was changed so we have to delete the old one from the profiles first!
profiles.remove(oldName);
}
if (!oldImage.equals(image)) {
// Image name has changed so we rename the mounts and config files
file_Rename(oldImage + ".mounts", image + ".mounts");
file_Rename(oldImage + ".config", image + ".config");
}
profiles.put(name, image);
lastSelected = name;
fillSpinner();
savePrefs();
}
});
alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
And then the XML layout for the dialog is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<RelativeLayout android:layout_alignBaseline="#+id/layout_root" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="8sp" android:id="#+id/RelativeLayout1">
<TextView
android:id="#+id/label_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/inputLabel_Name"
android:layout_centerVertical="true"/>
<EditText
android:id="#+id/txt_LinuxName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label_Name"
android:hint="#string/hint_EnterName"
android:textColor="#FF111111" >
<requestFocus></requestFocus>
</EditText>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/layout_root"
android:layout_marginTop="8sp" >
<TextView
android:id="#+id/label_Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/launcher_Label_ImageName" />
<EditText
android:id="#+id/txt_ImageName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/fileselector"
android:layout_toRightOf="#id/label_Image"
android:textColor="#FF111111" >
<requestFocus></requestFocus>
</EditText>
<Button
android:id="#+id/fileselector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txt_ImageName"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="..." />
</RelativeLayout>
You aren't calling dismiss on the negative button:
alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
As for the positive, is your onClick getting fired? Put Log statements in and see if it is.
This is just a guess so it is probably wrong, but when you are setting your onClickListener, try replacing the line fileSelector.setOnClickListener(new OnClickListener) with fileSelector.setOnClickListener(new View.OnClickListener)
I set a swith card with three tab in the main activity,there is a listview in the third tab,click the listview will pop up time options dialog box.
public void onSetWarn(){
LayoutInflater factoryInflater = LayoutInflater.from(MainActivity.this);
final View dialogView = factoryInflater.inflate(R.layout.dialog, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("set")
.setView(dialogView)
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "set success!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
/*
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
*/
dialog.show();
}
where should i put this code
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
I trid several places,but all the NULL pointer errors.
dialog.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="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reminder the time"
/>
<TimePicker
android:id="#+id/TimePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remind way"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ring"
/>
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shake"
/>
</LinearLayout>
</LinearLayout>
You should use dialogView.findViewById(R.id.TimePicker1) instead of this.findViewById(R.id.TimePicker1).That is because TimePicker1 exists in the view tree of dialogView (You load it from dialog.xml by LayoutInflator)
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();