I have button on (CustomDilaog activity) when clicked show custom dialog with password edittext and OK button and cancel button , if you put correct password it open another activity (Text activity), till now every things work fine ,
i have question with two parts.
Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss
Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .
(CustomDilaog activity):
public class CustomDilaog extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(getString(R.string.introclusion)));
button = (Button) findViewById(R.id.button1);
// add button listener
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
Typeface font = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
text.setTypeface(font);
text.setText("write password :");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
Typeface font1 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
dialogButton.setTypeface(font1);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText password = (EditText) dialog.findViewById(R.id.password);
////
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
Intent intent = new Intent(CustomDilaog.this,Text.class);
startActivity(intent);
}
else{
// get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_toast);
text.setText("Wrong password");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
}
});
Button dialogButtonCancell = (Button) dialog.findViewById(R.id.cancel);
Typeface font11 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
dialogButtonCancell.setTypeface(font11);
dialogButtonCancell.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
call dialog.dismiss() at the beginning of dialogButton click listener:
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
EditText password = (EditText) dialog.findViewById(R.id.password);
if( password.getText().toString().length() > 0 ) {
...
}
});
Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss
Simply call finish() in your CustomDialogActivity when you start the new Activity. It looks like your Intent is calling this same Activity though so I'm a little confused on that.
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
Intent intent = new Intent(CustomDilaog.this,Text.class);
startActivity(intent);
Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .
Add an else statement to your first if and inside it put dialog.dismiss()
Related
Context: There is a custom Listview and each list item has a button in it. When you click the button an alertDialog appears with an edit text and submit button. This only happens on the first click, on subsequent clicks a Toast will simply appear with the number of times it has been clicked thus far.
When you click the submit button a toast will appear displaying the text that was entered into the editText and the number of times they have clicked on it which will presumably always be 1 since this can only happen on the first click.
Problem: The timesClicked counter is not working properly if the user so much as clicks on the editText before clicking submit. It is restting to 0 I guess. However if the user does not click on the editText then the program works normally. 0_o I'm at a loss.
Attempts at solving: I simplified the code down quite a bit to try and pinpoint the problem and this is where I am stuck. Originally I was inflating a view that only had an edit text and then I was just using builder.setPositiveButtton. I thought implementing the buttons directly in the view would fix it but that doesn't seem to be the case. I have been stuck on this for awhile. Any help would be great
Here is a video of the bug happening
private class OnSubtractClickListener implements View.OnClickListener {
final int id; //id of list item that was clicked
int timesClicked;
Toast toast;
public OnSubtractClickListener(int id, View view) {
super();
this.id = id;
timesClicked = 0;
}
#Override
public void onClick(View view) {
if (timesClicked != 0) {
toast.setText(Integer.toString(timesClicked));
toast.show();
}
else{
toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT);
final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null);
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setView(dialogView);
builder.setTitle("Subtract cigar?");
builder.setIcon(R.mipmap.monkey_launcher);
final AlertDialog dialog = builder.create();
Button yesButton = (Button)dialogView.findViewById(R.id.dialog_notes_yes_button);
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText);
String userInput = editText.getText().toString();
String timesClickedString = Integer.toString(++timesClicked);
toast.setText(timesClickedString + ": " + userInput);
toast.show();
dialog.dismiss();
}
});
dialog.show(); //new
}
}
}
You can make class that extend Dialog.
example:
public class CustomDialog extends Dialog {
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams();
lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
lpWindow.dimAmount = 0.8f;
getWindow().setAttributes(lpWindow);
setContentView(R.layout.activity_custom_dialog);
editText = (EditText) findViewById(R.id.editText);
}
}
You can use this dialog..
mCustomDialog = new CustomDialog();
mCustomDialog.show();
You can make the layout as you wish.
======================================================================
You can use AlertDialog.Builder.setPositiveButton.
site : setPositiveButton
example...
toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT);
final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null);
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setView(dialogView);
builder.setTitle("Subtract cigar?");
builder.setIcon(R.mipmap.monkey_launcher);
builder.setPositiveButton("text", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText);
String userInput = editText.getText().toString();
String timesClickedString = Integer.toString(++timesClicked);
toast.setText(timesClickedString + ": " + userInput);
toast.show();
}
});
final AlertDialog dialog = builder.create();
dialog.show(); //new
I found the solution. Basically what was happening was that when the Keyboard appeared it would cause the listview to adjust the size recreating the whole listview with recycled/old versions of the list items from before the dialog appeared -effectively undoing any changes made to the ListView items by the dialog.
In your listview XML add this:
android:descendantFocusability="beforeDescendants"
In Mainfest.xml:
<activity android:name= ".yourActivity"
android:windowSoftInputMode="adjustPan"/>
I have this button to show a dialog which in turn has an EditText for the users to write something and then tap the done button to save it otherwise another button to close the dialog. The problem here is that I am not able to fetch that String that the user wrote. It returns me a null sting instead and causes the app to crash. Here's the code :
I call the dialog up on clicking on a button:
//Write Review Dialog Box
writeReviewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("HAPPENED", "This Onclick is working");
try{
// Create custom dialog object
final Dialog dialog = new Dialog(GetReviewActivity.this);
// Include dialog's xml file
dialog.setContentView(R.layout.write_review_activity);
// Set dialog title
dialog.setTitle("Your review is valuable");
final EditText etWR = (EditText)findViewById(R.id.etWR);
Button writeButton = (Button) dialog.findViewById(R.id.buttonWR);
Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks);
dialog.show();
//Done Button
writeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("HAPPENED: ","writeButton Block Is WORKING!");
Activity activity = GetReviewActivity.this;
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
String Review = etWR.getText().toString();
StoreAReview(Review);
}
});
}
});
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
});
final EditText etWR = (EditText)findViewById(R.id.etWR); is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. so you have to inflate EditText from your dialog's view:
Try
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
instead of
final EditText etWR = (EditText)findViewById(R.id.etWR);
Been through this issue before. I tried initiating EditText with Dialog reference but this didn't work
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
Solution:
This is how I solved it:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View dialogWR = getActivity().getLayoutInflater().inflate(R.layout.write_review_activity, null);
etWR = (EditText) dialogWR.findViewById(R.id.etWR);
}
You should initialize EditText
EditText etWR = (EditText)dailog.findViewById(R.id.etWR);
becoz your EditText is coming from dailog. So
U just miss the dialog.findViewById(R.id.etWR)
change this line
final EditText etWR = (EditText)findViewById(R.id.etWR);
to this one
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
then, add click event code
Why doesn't dialog dismiss on the first click (but shows Toast) ?
On the second click it dismisses (Toast is shown again).
private void networkDialog(){
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
Try this way .Let me inform .I hope it will help you.
private void networkDialog()
{
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(this);
dialog.show();
}
Then Use onClick switch Statement
public void onClick(View view)
{
switch (view.getId())
{
case R.id.dialogButLeft:
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
dialog.dismiss();
break;
}
}
A bit late but a colleague had the same issue and referred to this, are you absolutely sure that you're not creating two dialogs by calling networkDialog() twice?
Add some unique text to the dialog that will be visible to you when it's displayed like System.currentTimeMillis(), that way you can see if it's called twice because the text is different.
Or add logging / run in debug
Make your Button also final like this:
private void networkDialog(){
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
final Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
it's working for me in my app like this:
// Initialize variables
final Dialog passwordDialog = new Dialog(BPMActivity.this,R.style.CustomDialogStyle);
passwordDialog.setContentView(R.layout.password_view);
final Button btnCancel=(Button) passwordDialog.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
passwordDialog.dismiss();
}
});
passwordDialog.show();
I don't know if this is still relevant to the OP. But I've been banging my head against the wall for quite some time trying to figure this one out. It appears to happen in later (5-6+) Android versions and did not occur on a 4.4.2 device which I have. The solution I've found is to setFocusableInTouchMode of the Button to false:
button.setFocusableInTouchMode(false)
This answer gave me the idea:
I have to click the button twice for it to work
First of all, I have searched in forum for "How to get text from edittext in other layout", and tried many other ways, then I decided to use LayoutInflater but it did not help.
The implementation of LayoutInflater (I follow this guide)
LayoutInflater factory = getLayoutInflater();
View regisText = factory.inflate(R.layout.regis, null);
EditText user = (EditText) regisText.findViewById(R.id.edt1);
String usr = user.getText().toString();
My parent layout is named activity_input.xml having 3 buttons (I do not set text button). I put only button Id for easy view.
<Button
android:id="#+id/btn1">
</Button>
<Button
android:id="#+id/btn2">
</Button>
<Button
android:id="#+id/btn3">
</Button>
After I click a button, one dialog appears to select value (1, 2 or 3) to set the button display text (1, 2 or 3) in activity_input.xml.
final Dialog dialog1 = new Dialog(user_input.this);
dialog1.setTitle("Select a Number");
dialog1.setContentView(R.layout.game_dialog);
dialog1.show();
button1 = (Button) dialog1.findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
p1_btn.setText("1");
dialog1.dismiss();
}
});
The onClick works perfect.
But my problem is that I want to get the text of the button in parent layout again, I try to implement Layoutinflater but it does not work, here is the code.
LayoutInflater f1 = getLayoutInflater();
View v1 = f1.inflate(R.layout.activity_input, null);
Button btn1 = (Button) v1.findViewById(R.id.b1);
String t1 = btn1.getText().toString();
I check to make sure that the String t1 is retrieve successfully by print the string but it shows nothing
Hope you guide help me as soon as possible!
Thanks you very much.
You can make a static variable and store the result there, then access it from your dialog. So in your ActivityInput.java
public class ActivityInput extends Activity {
public static String input = "";
//code
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
input = "1";
p1_btn.setText(input);
dialog1.dismiss();
}
});
}
Now in your other java class ActivityOutput.java:
public class ActivityOutput extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//code
String result = ActivityInput.input;
//use the result string somewhere to display it in your app
}
}
Alright, so I would like to have a custom dialog, but I cannot figure out for the life of me how to make it appear when the function is called.
public void addHomework() {
final Dialog alert = new Dialog(this);
alert.setTitle("Add Homework");
alert.setContentView(R.layout.homework_item_entry);
Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);
add_button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ClassHomeworkList.this, "Adding homework", Toast.LENGTH_SHORT).show();
}
});
cancel_button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
What could I do?
I know this is an old thread, but even after reading the Android docs it also was not obvious to me how to display a custom dialog using the standard Dialog class. Basically you can call:
this.showDialog(MANAGE_PASSWORD); // MANAGE_PASSWORD static final int
from your activity. Then instantiate the custom dialog in the onCreateDialog method:
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case MANAGE_PASSWORD:
dialog= getInstancePasswordDialog();
break;
case DIALOG_ABOUT:
// do the work to define the About Dialog
dialog= getInstanceAlertDialog(); // called "the first time"
break;
default:
dialog = null;
}
return dialog;
}
The code to instantiate the dialog is in getInstancePasswordDialog(). Here is the code sample.
I think you have the problem that your two buttons cannot be found by their ID's like this (as you are trying to find them in your main activity, but they are in the layout for the dialog)
Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);
But instead need to do:
Button add_button = (Button) alert.findViewById(R.id.add_homework_button);
Button cancel_button = (Button) alert.findViewById(R.id.cancel_homework_button);
Have you read the following document: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog ?
You should override your Activity's onCreateDialog(int) method as described there and then use showDialog(int)
LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.dialog, null);
//the id is your root layout
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
alert.setContentView(layout);