How To Add Changing Number Of EditText Objects in AlertDialog - java

I'm Developing a game so I want to get player name using an AlertDialog. But I don't Know certain number of players, it's Variable between 2 to 16!
I've added an spinner to ask about the NumberOfPlayers and a Button to Show AlertDialog then I tried to add certain number of EditText Using for loop. It doesn't have error but when I run application on phone, I just have Ok and Cancel Buttons. I couldn't resolve problem and become appreciate if someone help me.
This is My AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux==NumberOfPlayers;aux++) {
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();

Just change your code with below one and you will get dynamic edittext in alert dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux<NumberOfPlayers;aux++) {
input[aux] = new EditText(MainActivity.this);
input[aux].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Let me highlight problems in your code are:
you are using "aux==NumberOfPlayers" in "for loop" which is wrong. it should be "aux < NumberOfPlayers"
you are not initializing edittext in "for loop" such as "input[aux] = new EditText(MainActivity.this);"
you are not giving height and width for both linear layout as well edittext after initializing such as ".setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));"

Related

android: input dialog not working [duplicate]

This question already has answers here:
How to display Toast in Android?
(22 answers)
Closed 5 years ago.
public void showEditPassword() {
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.dialog_editpassword, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText txtOldPass, txtNewPass, txtConfirmPass;
txtOldPass = (EditText) promptsView.findViewById(R.id.txtOldPassword);
txtNewPass = (EditText) promptsView.findViewById(R.id.txtNewPassword);
txtConfirmPass = (EditText) promptsView.findViewById(R.id.txtConfirmPassword);
// set and show dialog edit password
alertDialogBuilder.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do the saving here
saveNewPassword(currentPassword, txtOldPass.getText().toString(),
txtNewPass.getText().toString(), txtConfirmPass.getText().toString());
//recreate();
Toast.makeText(MainActivity.this, "Save Password clicked", Toast.LENGTH_LONG);
}
})
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I want to call a input dialog and capture the save button action when click. I have tried putting a Toast message when save button was clicked in the dialog but nothing' happened. Thanks.
You do not call show() on your Toast. That's why it looks like nothing is happening

NumberPicker inside AlertDialog java

I'm trying to get a numberpicker inside an alertdialog but it returns me the following error:
12-22 20:37:09.640 16893-16893/com.example.asus.mingausfashionmoda
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
here
at com.example.asus.mingausfashionmoda.estoque$2$2.onItemClick(estoque.java:134)
This is where I create the AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(estoque.this);
builder.setTitle("ADICIONAR OU REMOVER ROUPA");
NumberPicker np = (NumberPicker) findViewById(R.id.nPicker);
np.setMinValue(0);
np.setMaxValue(5);
builder.setView(np);
builder.setPositiveButton("ADICIONAR", new DialogInterface.OnClickListener() {#Override
public void onClick(DialogInterface dialog, int which) {
adicionarR(objectIdTXTtoS);
}
});
builder.setNegativeButton("REMOVER", new DialogInterface.OnClickListener() {#Override
public void onClick(DialogInterface dialog, int which) {
removerR(objectIdTXTtoS);
}
});
AlertDialog dialog = builder.create();
dialog.show();
This is what I have on (estoque.java.134):
dialog.show();
What am I doing wrong?

Assign value to spinner from number picker

I'm creating a fragment that contains number pickers and cancels okay buttons. Whenever I click the first spinner in the main activity, the spinner will pop out the fragment that contains number pickers. I select the number pickers values using getValue() method, but how to pass this value back to the spinner in main activity. I try to use getposition() method from spinner, but it won't works, because I didn't use setAdapter() method assign values to my spinner, I just want my spinner show the only one value retrieved from the fragment number pickers.
OnCreate function
s = (Spinner) findViewById(R.id.spinner1);
s.setOnTouchListener(onSpinnerTouchListener);
Below is my onSpinnerTouchListener:
LayoutInflater li = LayoutInflater.from(TaxCalc.this);
View promptView = li.inflate(R.layout.frag_numpicker, null);
AlertDialog.Builder builder = new AlertDialog.Builder(TaxCalc.this);
builder.setView(promptView);
builder.setTitle("Choose Amount of People");
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int value = mNumberPicker.getValue();
});
builder.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
mNumberPicker = (NumberPicker) promptView.findViewById(R.id.number_picker);
array_spinner = new String[30];
for (i = 0; i < 30 ; i++){
array_spinner[i] = Integer.toString(i+1);
}
mNumberPicker.setMaxValue(array_spinner.length-1);
mNumberPicker.setMinValue(0);
mNumberPicker.setWrapSelectorWheel(false);
mNumberPicker.setDisplayedValues(array_spinner);
alertDialog = builder.create();
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);

Nested LinerLayout in Custom Dialog not showing up

Im trying to create a custom dialog box which contains main LinearLayout which has 2 nested LinearLayout inside it which contains ImageView and TextView.
But the problem is the None of the LinearLayout's are showing up.I'm not even getting any errors and Application is not crashing.
Thank You.
Below is the code segment related to this problem:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
ll_main = new LinearLayout(AddFreebie.this);
ll_main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_main.setGravity(Gravity.CENTER);
ll_main.setOrientation(LinearLayout.VERTICAL);
ll_bpic = new LinearLayout(AddFreebie.this);
ll_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_bpic.setGravity(Gravity.CENTER);
ll_bpic.setOrientation(LinearLayout.HORIZONTAL);
iv_bpic = new ImageView(AddFreebie.this);
iv_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_bpic = new TextView(AddFreebie.this);
tv_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_bpic.setText("Browse phone for pictures..");
ll_bpic.addView(iv_bpic);
ll_bpic.addView(tv_bpic);
ll_cpic = new LinearLayout(AddFreebie.this);
ll_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_cpic.setGravity(Gravity.CENTER);
ll_cpic.setOrientation(LinearLayout.HORIZONTAL);
iv_cpic = new ImageView(AddFreebie.this);
iv_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_cpic = new TextView(AddFreebie.this);
tv_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_cpic.setText("Take pictures using camera.");
ll_cpic.addView(iv_cpic);
ll_cpic.addView(tv_cpic);
ll_main.addView(ll_bpic);
ll_main.addView(ll_cpic);
builder.setView(ll_main);
builder.setCancelable(true);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// MyActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
I could be incorrect, But I dont think an AlertDialog is what you really want to use in this case. If you read the Developer pages on AlertDialogs, they are really to be used for small 0, 1, 2, or 3 button alert windows, and arent really meant to have their own truly custom interfaces. Here are some links on AlertDialogs
http://www.helloandroid.com/tutorials/how-display-alertdialog-your-android-application
To do what you want to do I think you either want to use a Diaglog, or a PopupWindow
Link to creating custom dialogs:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
Link to creating a custom popover window (I have used these in the past):
http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

How to create the above custom dialog in android?

Can someone tell me how to create the above dialog view similar/exactly to the link [here][1], whereby the focus of the problem is to create the view in the centre of the picture?
I have done some research and it made me wonder should i be using a custom xml to create a custom dialog view or should i be using alertdialog to create the exact view programmability shown above? And even if alertdialog is possible how am i going to accommodate with so many textview messages shown in the middle of the dialog picture given alertdialog limitation? Eg: "builder.setMessage("This is the alert's body");" If you know what i mean!!
Can someone tell me the easiest way to get the exact same view because i'm kinna doing the same app and new to android.. Thanks :)
The best approach will be custom dialog. As it will be helpful creating all those background colours and the effect. I am sure the link you have posted is using custom dialog as well,
cheers
link that might help:
[1] http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
[2] http://androidideasblog.blogspot.com/2010/02/creating-custom-dialog-in-android.html
/// In your code implementations just add this code when you create dialog....after having this just have all TextView arranged in your layout and add that layout id to this below code good luck
//Dialog box creator
private Dialog constructYourDialog()
{
//Preparing views
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.***your_xml_name***, (ViewGroup) findViewById(R.id.***Yout view id***));
//Building dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setPositiveButton("Show Videos", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Video Click");
dialog.dismiss();
});
builder.setNegativeButton("E-Mail", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","E-mail Click");
dialog.dismiss();
}
});
builder.setNeutralButton("Show Map", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Map Click");
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
return alert;
}
Try following code
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert !");
builder.setMessage("your text here");
builder.setPositiveButton("Show Video", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
connect = false;
}
});
builder.setNegativeButton("Show Map", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.setNeutralButton("Show Both", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.show();
UPDATE: To show custom title create a layout and inflate it using below code
LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
Remove following line from above code
builder.setTitle("Alert !");
and then set using
builder.setCustomTitle(layout)

Categories