I am having a great difficulty on these part. I cannot call my dialog when I put my class inside Tab Group. Is there anyway I could do this?
This is my dialog creator
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return createDialog();
default:
return null;
}
}
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case 1:
// Clear the input box.
EditText text = (EditText) dialog.findViewById(TEXT_ID);
text.setText(textme);
text.setKeyListener(null);
break;
}
}
private Dialog createDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(SpecialofMonth.this);
final EditText input = new EditText(this);
input.setId(TEXT_ID);
builder.setView(input);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
return builder.create();
}
Calling it on button click
View.OnClickListener handlesReadme = new View.OnClickListener() {
public void onClick(View v) {
showDialog(1);
}
};
This one works when it is outside the tab group. But in vice versa or when it is inside, it is not working..
Can you help me out?
in side your TAB Group create like this
AlertDialog.Builder builder = new AlertDialog.Builder(SpecialofMonth.this.getParent());
Related
I am building an app in Android studio and basically I want a window to popup when a user clicks the add button. I used the setOnClickListener but when I run the app, nothing happens. Could there possibly something wrong with my code?
Here's my MainActivity code
public class MainActivity extends AppCompatActivity {
Button addBtn;
ListView itemListView;
DatePickerDialog.OnDateSetListener dateSetListener;
String dateString = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = (Button)findViewById(R.id.addBtn);
itemListView = (ListView)findViewById(R.id.itemListView);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.activity_popup_window, null);
EditText itemName = (EditText)view.findViewById(R.id.itemName);
Button expirationDateBtn = (Button)view.findViewById(R.id.expirationDateBtn);
builder.setView(view)
.setTitle("Add Item")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (itemName.getText().toString().isEmpty() || dateString == null) {
Toast.makeText(MainActivity.this,
"Item name or expiration date is missing",
Toast.LENGTH_LONG).show();
}
else{
//do action
}
}
});
//when clicked on Expiration Date Btn
//display date on button
AlertDialogBuilder doesn't create and show a new AlertDialog implicitly. It only prepares the dialog before explicitly calling create() (or you can directly call show() if you need to display your dialog in the moment it gets built).
Your code misses the following lines at the end of onClick():
AlertDialog dialog = builder.create();
dialog.show();
or just:
builder.show();
Iam creating a dialog with following code, who creates multiple choice check box.. But I don't know how to create their id's to add click event , I m new to android please help me..:
private void showDailog() {
final String[] items = {" Blue", " Red", " Black", " White", " Pink"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select any theme you want : ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Your logic when OK button is clicked
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
}
});
dialog = builder.create();
dialog.show();
}
Instead of Alert Dialog create a simple dialog with your custom layout like this
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_lauout);
dialog.show();
Button button = (CheckBox) dialog.findViewById(R.id.button);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I have the following code for an AlertDialog window in which I want the user to input a number(that I'm storing in int m_Text). I have 2 problems: I can't see the numbers that I'm typing and if I press enter without any numbers it will crash. How can I solve them ? (the method pressMe() is executed when I press a button)
public void pressMe(){
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER );
AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setMessage("Enter number:")
.setView(input)
.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_Text = Integer.parseInt(input.getText().toString());
Log.d(TAG," Number : "+m_Text");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setTitle("Test")
.setIcon(R.drawable.ic_launcher)
.create();
myAlert.show();
}
The answer why your application is crashed is here:
Integer.parseInt(input.getText().toString());
When input text is empty you try to parse "" on Integer and you get NumberFormatException.
you have to handle this situation for example like in code below:
final String str = input.getText().toString().trim();
m_Text = str.length() == 0 ? 0 : Integer.parseInt(str);
or
final String str = input.getText().toString().trim();
if(str.length() != 0){
m_Text = Integer.parseInt(str);
}
I don't understand your first problem. If you explain it to me, I help you.
Update
When I tried your code I have result like below:
Read this document
http://developer.android.com/guide/topics/ui/dialogs.html
if you want to use any types of dialog in your application.
1)Create a class that will extend DialogFragment class.
2)Overide onCreateDialog() method and inside that write your code for both Positive button and negative button
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
3)Call that Fragment from HostActivity using this code
DialogFragment newFragment = new YourFragmentClass();
newFragment.show(getSupportFragmentManager(), "missiles");
if you want a CustomLayout then
1)create a XML file in res->customLayout.xml and inside that place the view you want(In your case draw a Single EditText on that layout),do not add Button for Positive or Negative Button they are already add by DialogFragment.
2)Inside onCreateDialog() of dialogFragment do something like this
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final EditText et_text=(EditText)getActivity().findViewById(R.id.editTextId);
builder.setView(inflater.inflate(R.layout.custom_layout, null))
// Add action buttons
.setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String mob=et_text.getText().toString();
//Do What you want to do with EditText
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Coding for Negative Button Here
}
});
return builder.create();
}
you can Pass EventBack to Hosting Activity.
Read reference document
Hope this will help you
The topic explains what i'm after... I can't retrieve the EditText from my custom view in android. All I get is a Nullpointer Exception. :/
I've marked where the problems are in the code with comments.
The ID:s are correct and my XML layout is a simple RelativeLayout containing two EditText attributes.
Obviously I'm missing something trivial here, but I've now stared at the code for almost 2 hours without solving this, so I thought I'll give SO a try instead.
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
EDIT:
After suggestions the following code is a working one! Thanks again!
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) f.findViewById(R.id.login_username);
passWord = (EditText) f.findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
Thanks in advance!
Alex
Cast the Dialog as a View:
View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);
Then replace:
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
with
uName = (EditText) v_iew.findViewById(R.id.login_username);
passWord = (EditText) v_iew.findViewById(R.id.login_password);
(Can't vote so creating new answer: Took me hours to figure this out; adding the qualifier finally fixed it - like you figure out already)
Doesnt work:
final Dialog dialog = new Dialog(this);
...
keyInput = (EditText) findViewById(R.id.key_input);
Works:
final Dialog dialog = new Dialog(this);
...
keyInput = (EditText) dialog.findViewById(R.id.key_input);
In the case of an EditText you should implement TextWatcher It is generally a very bad idea to use editText.getText()
Here is a very simple example code for Custom Dialog containing an EditText as part of the layout. There is also a button that needs to be on the same layout which when clicked will show you the text which you just entered. Have fun!
final String inputString = null;
final Dialog dialog = new Dialog(YourActivityName.this);
dialog.setContentView(R.layout.custom_dialog_layout);
EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text);
Button done = (Button) dialog.findViewById(R.id.done);
dialog.show();
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
inputString = s.toString();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT);
dialog.dismiss();
}
});
I am trying to write an AlertDialog with 3 buttons. I want the middle, Neutral Button to be disabled if a certain condition is not met.
Code
int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("You have Encountered a Monster");
alertbox.setPositiveButton("Fight!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
createMonster();
fight();
}
});
alertbox.setNeutralButton("Try to Outwit",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
// This should not be static
// createTrivia();
trivia();
}
});
// Return to Last Saved CheckPoint
alertbox.setNegativeButton("Run Away!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
runAway();
}
});
// show the alert box
alertbox.show();
// Intellect Check
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
button.setEnabled(false);
}
}
The line:
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
Gives error:
Cannot cast from AlertDialog.Builder to AlertDialog
How do I fix this?
You can't call getButton() on the AlertDialog.Builder. It has to be called on the resulting AlertDialog after creation. In other words
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
//...All your code to set up the buttons initially
AlertDialog dialog = alertbox.create();
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
button.setEnabled(false);
}
The builder is just a class to make constructing the dialog easier...it isn't the actual dialog itself.
HTH
Better solution in my opinion:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
if(**some condition**)
{
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
if (button != null) {
button.setEnabled(false);
}
}
}
});
The trick is that you need to use the AlertDialog object retuned by AlertDialog.Builder.show() method. No need to call AlertDialog.Builder.create().
Example:
AlertDialog dialog = alertbox.show();
if(monsterint > playerint) {
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
button.setEnabled(false);
}