Adding multiple edit texts to an alert dialog [duplicate] - java

This question already has answers here:
Multiple EditText objects in AlertDialog
(4 answers)
Closed 8 years ago.
I've implemented an alert dialog containing one edit text for name but I need to store other info such a quantity location using the same alert dialog.
I tried to add another edit text by simply declaring another but this didn't populate the dialog with another edit text.
Does anyone now how you can add extra edit text object to an alert dialog?
This is how I've implemented an alert dialog for containing one edit text:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Generic Info");
alert.setMessage("Ship Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
//need to add two more edit text fields for extra input.
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
return true;

Try this
Create res/layout/custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 1"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 2"
android:id="#+id/editText2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 3"
android:id="#+id/editText3"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 4"
android:id="#+id/editText4" />
</LinearLayout>
Then in your Activity that you want AlertDialog to show use:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Custom view with 4 EditTexts");
builder.setMessage("AlertDialog");
builder.setView(R.layout.custom_view);
//In case it gives you an error for setView(View) try
builder.setView(inflater.inflate(R.layout.custom_view, null));
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return builder.create();
And this will give you the following result:

Create a Relative Layout
add as many edittext as you want
and then set relative layout in alert.setView(relativeLayout);
can not write whole code for you
but see here custom layout , this will surely help you .

Related

How to align EditText hint and input with AlertDialog message?

In my Android app I have an AlertDialog with an EditText to input the name of the user. The AlertDialog contains a title and a message, and the EditText contains a hint. All are left aligned, which is good, but they are not aligned with each other.
Here is my current code for the AlertDialog:
final EditText nameText = new EditText(this);
nameText.setHint(dialogHint);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(dialogTitle)
.setMessage(dialogMessage)
.setView(nameText)
.setPositiveButton(textPositiveButton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Get name from player
String highScoreName = String.valueOf(nameText.getText());
// Do something with highScoreName
}
})
.setNegativeButton(textNegativeButton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.create();
dialog.show();
This is how it looks:
And this is how I want it to look:
Is there a way to achieve this? Searching for answers online only led me to questions about central aligning the AlertDialog message or aligning separate AlertDialog and EditText views. Any help is appreciated.
Well, technically, you can add margins to your EditText programmatically, for example, like described here. Note, that you can request layoutParams of your EditText only after dialog.show() method call. I highly don't recommend to follow this way and I'm not sure about its reliability.
So the best option, like was mentioned in the comments, is custom dialog with your own fully customisable layout.
Create layout file. For example, custom_dialog.xml. And create your dialog view.
<?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="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message" />
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint" />
</LinearLayout>
Then insert it in the dialog.
View customDialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText editText = customDialogView.findViewById(R.id.edit_text);
AlertDialog alertDialog = new AlertDialog.Builder(requireContext())
.setView(customDialogView)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
editText.getText();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.create();
alertDialog.show();
More information in the docs.

How do I get the edit text value in text view from an alert box in android java?

I want to get some integer value from the user he/she click on the an alert box will show and asks for enter the total budget. When he/she puts the total budget in the alert box field then it'll be show in text view in the same activity.
Do refer to topic regarding custom dialog box:
How to create a Custom Dialog box in android?
You can create a custom dialog box that have a custom layout
R.layout.budget_dialog:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="#+id/edit_budget"
android:hint="Enter budget"
android:layout_below="#+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="#drawable/btn_flat_red_selector"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
And then obtain the value by:
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.budget_dialog);
final EditText edtBudget = dialog.findViewById(R.id.edit_budget);
Button dialogButton = dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get budget string value
String budget = edtBudget.getText().toString();
//Do something with your value
dialog.dismiss();
}
});
dialog.show();
}
Each dialog has a positive button, and that positive button has a callback where you can get the text from edit text, like Edittext.getText and display it for textview like TextView.setText.
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// your work here
textview.setText = Edittext.getText
dialog.dismiss();
}
});
alertDialog.show();

How to set fontfamily in AlertDialog edittext?

I don't know how to set fontfamily for alertdialog editext.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Project name");
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(10, 10, 10, 10);
final EditText editText = new EditText(MainActivity.this);
editText.setHint("Enter your project name");//here how to change text fontfamily
linearLayout.addView(editText);
builder.setView(linearLayout);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String text = editText.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
I tried with TypeFace but it's not working. if you help me then it would be great and great and i will give you one big upvote.
try to these two line and check it working or not.
Typeface typeface=Typeface.create("font-family",Typeface.BOLD_ITALIC);
editText.setTypeface(typeface);
Try using
editText.setTypeface(
Typeface.create("serif", Typeface.NORMAL)
);
Make custom layout file for the dialog
<?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="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Project Name"
android:fontFamily="#fonts/myFont" />
</LinearLayout>

EditText.getText() returning empty string

So this is another problem that has been brought up a million times, but I'm still doing something wrong. Using EditText.getText() is returning an empty string.
I'm doing this in a small custom dialog I've made. I'm building it with the AlertDialog Builder, which might be causing the issue? I really don't know at this point.
Some things I've tried/notes on what I know about the issue:
I'm checking for text in the OK button's click listener, so I'm not trying to get a value before there would be one, which was a common error I saw.
I have ID's set for the EditText objects in my XML and the debugger seems to show that I'm referencing them properly.
I've tried defining the EditText objects outside of the onCreateDialog method and that didn't change things (though I am curious which is better practice).
Using EditText.setText() before getText() will allow it to return the argument used in setText(), but it doesn't seem to be fetching a value input by the user.
Here is my custom Dialog Fragment:
public class GPSLocationDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.gps_dialog, null);
final EditText latitudeText = (EditText) view.findViewById(R.id.latitude);
final EditText longitudeText = (EditText) view.findViewById(R.id.longitude);
// Define the dialog
builder.setView(inflater.inflate(R.layout.gps_dialog, null))
.setMessage("Manually input a GPS address")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("myTag", "Text: " + latitudeText.getText()); // This prints ""
// These throw errors since they're trying to parse "" as a double
double latitude = Double.parseDouble(latitudeText.getText().toString());
double longitude = Double.parseDouble(longitudeText.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here is how I'm calling the Dialog from my Activity:
GPSLocationDialogFragment gpsDialog = new GPSLocationDialogFragment();
gpsDialog.show(getFragmentManager(), "GPSDialog");
And here is my layout .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/longitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/longitude"
android:inputType="numberSigned|numberDecimal" />
<EditText
android:id="#+id/latitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/latitude"
android:inputType="numberSigned|numberDecimal" />
</LinearLayout>
If more context is necessary I can share it, I tried to simplify to just the relevant code.
Try changing this:
builder.setView(inflater.inflate(R.layout.gps_dialog, null))
to this:
builder.setView(view)
What's happening is that you inflate gps_dialog xml and turn its layout hierarchy into a View. Then you find the EditText's and a assign a reference to them.
However, by calling setView(inflater.inflate(R.layout.gps_dialog, null))
instead of passing the View that you already inflated, and whose child EditText's you have a reference to, to the dialog, what you are doing is inflating a new version of the gps_dialog.xml and passing that to the dialog. The references that you have are to EditText's that aren't on the screen.

Buttons of alert dialog are cut off

I have an alert dialog which is started when someone touches a specific button. The alert dialog contains a view that is defined by a layout xml file. This layout contains several controls like check boxes, edit text fields, ... These controls are all inside a scrool view so that you can scroll through the content, if the whole content doesn't fit on the screen.
The problem is now that when I start this alert dialog, the buttons at the bottom are cut off screen. Altough the scrool bar is working and I can scroll through the content of the alert dialog, the buttons of the alert dialog are sometimes not totally visible.
This means:
Sometimes, all is fine and I can see the buttons of the alert dialog, an sometimes for strange reason, the buttons are cut off. I think it's an issue of the view being to big for the alert dialog and pushing the buttons more down.
For example, the view contains an edit text control. If I enter my name ther, everything is fine. But if I add a new line to this edit text, the buttons start to be cut off a little bit.
What did I wrong? I thought the scroll view would handle the oversize of my view so that the alert dialog fits to the screen.
My app is in portrait mode always.
The code of the view:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bitteeinstellungenwaehlen" />
<EditText
android:id="#+id/edtTeam1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hteam1"
android:text="Christoph" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/edtTeam2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hteam2"
android:text="Lea" />
<EditText
android:id="#+id/edtTeam3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hteam3"
android:text="Ludwig" />
<EditText
android:id="#+id/edtTeam4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hteam4"
android:text="Anja" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/weitereeinstellungen" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/chkModerationMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/moderationsmodus" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/info" />
</LinearLayout>
<CheckBox
android:id="#+id/chkPassOver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/weitergeben" />
<CheckBox
android:id="#+id/chkBlackScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/blankscreen" />
</LinearLayout>
</ScrollView>
And that's how I start the alert dialog:
final AlertDialog.Builder alert = new AlertDialog.Builder(QuizEditor.this);
alert.setTitle(getString(R.string.speichernUndVorschau) + "...");
alert.setMessage("");
LayoutInflater inflater = LayoutInflater.from(QuizEditor.this);
final View layFrage = inflater.inflate(R.layout.layoutquizsettings,null);
alert.setView(layFrage);
final CheckBox chkModeration = (CheckBox) layFrage.findViewById(R.id.chkModerationMode);
final CheckBox chkPassOver = (CheckBox) layFrage.findViewById(R.id.chkPassOver);
final CheckBox chkBlackScreen = (CheckBox) layFrage.findViewById(R.id.chkBlackScreen);
final EditText edtTeam1 = (EditText) layFrage.findViewById(R.id.edtTeam1);
final EditText edtTeam2 = (EditText) layFrage.findViewById(R.id.edtTeam2);
final EditText edtTeam3 = (EditText) layFrage.findViewById(R.id.edtTeam3);
final EditText edtTeam4 = (EditText) layFrage.findViewById(R.id.edtTeam4);
alert.setNeutralButton(getString(R.string.speichernUndVorschau), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton(getString(R.string.abbrechen), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
final AlertDialog dialog2 = alert.create();
dialog2.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
// TODO Auto-generated method stub
Button btnStarten = (Button) dialog2.getButton(AlertDialog.BUTTON_NEUTRAL);
btnStarten.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<String> teams = new ArrayList<String>();
String team1 = edtTeam1.getText().toString().trim();
String team2 = edtTeam2.getText().toString().trim();
String team3 = edtTeam3.getText().toString().trim();
String team4 = edtTeam4.getText().toString().trim();
if(team1.length() > 0) teams.add(team1);
if(team2.length() > 0) teams.add(team2);
if(team3.length() > 0) teams.add(team3);
if(team4.length() > 0) teams.add(team4);
if(teams.size() == 0) {
Toast.makeText(QuizEditor.this, getString(R.string.keinteameingegeben), Toast.LENGTH_SHORT).show();
}
else
{
// Quiz starten
dialog2.dismiss();
Intent myIntent;
if(chkBlackScreen.isChecked()) {
myIntent = new Intent(QuizEditor.this, BlackScreen.class);
}
else // Direkt das Quiz starten
{
myIntent = new Intent(QuizEditor.this, Quiz.class);
}
myIntent.putStringArrayListExtra("teams", teams);
myIntent.putExtra("moderation", chkModeration.isChecked());
myIntent.putExtra("passover", chkPassOver.isChecked());
myIntent.putExtra("filename", filename);
QuizEditor.this.startActivity(myIntent);
}
}
});
}
});
dialog2.show();
// dialog2.getWindow().setLayout(LayoutParams.WRAP_CONTENT, 300);
I am sorry for the German words. In the image you can see the problem.
Unfortunately I was not allowed to upload the screenshots I made...
The question was already solved in the comments, but I will add an answer for completeness and to show that it worked for me, too.
For an AlertDialog with a custom view, don't use the .setMessage line. Removing the following line in my project caused the buttons to stop getting clipped.
.setMessage("This is my message")
Set EditText lines to 1. Also set outer LinearLayout's to fill_parent.
Just make all the EditText property android:singleLine="true" and then try to test.

Categories

Resources