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?
Related
I'm creating a simple DialogFragment with two buttons, which is supposed only to show a Toast when you select any of the two options. This dialog is displayed when you press one certain button on a activity.
For some reason, the code doesn't show any kind of error but when i click the button, the app crashes.
How do I resolve this?
I was first trying to do a custom XML file and java file for the Dialog, but i couldn't make that work either. Google's documentation and other tutorials didn't help either
public class Config extends AppCompatActivity {
Button btncanc;
Button btnreestab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_config);
btncanc = (Button) findViewById(R.id.btncnacelset);
btnreestab = (Button) findViewById(R.id.btnrest);
btncanc.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(),"OperaciĆ³n cancelada",Toast.LENGTH_SHORT);
toast.show();
Config.this.finish();
}
});
btnreestab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Hola");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getApplicationContext(),"hola",Toast.LENGTH_SHORT);
toast.show();
}
});
builder.setNegativeButton("cancela", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getApplicationContext(),"adios",Toast.LENGTH_SHORT);
toast.show();
}
});
AlertDialog dialog = builder.create(); dialog.show();
}
});
}
}
You can copy your style from your context. Make your alert dialog initialization with.
new AlertDialog.Builder(Config.this)
in other way you can define a custom style in style.xml such as
<style name="myDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
...
</style
and initialize alert dialog with
AlertDialog.Builder dialog = new AlertDialog.Builder(getApplicationContext(), R.style.myDialog);
The reason for this crash is the AppCompatActivity has the Theme.AppCompat so you should use this theme.
checkout that crash.
You need to use a Theme.AppCompat theme (or descendant) with this
activity
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));"
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
In my main activity I have an alert dialog which creates a new intent when clicked. When the alert dialog is clicked and the new activity is opened I cannot dismiss the alert dialog even though I have included dialog.cancel(); in my main activity code.
Dialog builder (Main activity)
final Builder alertDialogBuilder = new Builder(this);
alertDialogBuilder.setTitle("Navigation");
alertDialogBuilder.setMessage("Go to the new activity.");
alertDialogBuilder.setNeutralButton("Okay", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "$$ onClick");
dialog.cancel();
Intent newactivity = new Intent(MAINACTIVITY.this,NEWACTIVITY.class);
startActivity(newactivity);
}
});
alertDialogBuilder.show();
After I click on the "Okay" button the dialog closes, but when the new activity is loaded the dialog reappears. The new activity extends the activity that the dialog is created in (Main activity).
Is there a way that I can close the alert dialog from the new extended activity?
try this way:
alertDialogBuilder.setPositiveButton("Okay", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "$$ onClick");
Intent newactivity = new Intent(MAINACTIVITY.this,NEWACTIVITY.class);
startActivity(newactivity);
dialog.cancel();
}
});
alertDialogBuilder.show();
good luck.
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)