Button on Custom Dialog Not Responding to Click Events - android

I created a custom dialog that extends Dialog. One button on that the dialog is an “OK” button which the user is expected to press when finished entering information in other fields. I cannot get any listeners set to that button to fire.
public class HeightDialog extends Dialog {
private Button okButton;
…
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heightdialog);
this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);
this.okButton.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
// Does not fire
HeightDialog.this.dismiss();
return;
}
});
this.okButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// Does not fire
HeightDialog.this.dismiss();
return true;
}
});
this.okButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Does not fire
HeightDialog.this.dismiss();
return true;
}
});
…
}
I also attempted an implementation where the Dialog class implemented the listeners(http://www.androidcompetencycenter.com/2009/01/android-basics-dialogs-and-floating-activities/) instead of using inner classes(http://about-android.blogspot.com/2010/02/create-custom-dialog.html):
Still no luck.
public class HeightDialog extends Dialog implements View.OnClickListener {
private Button okButton;
…
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heightdialog);
this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);
this.okButton.setOnClickListener(this);
public void onClick(View view){
HeightDialog.this.dismiss();
return;
}
…
}
I have set breakpoints inside each of the listeners in both versions of the implementation, and the debugger never stops execution. I have attempted to use inner classes for the listeners which did not solve the problem.
Any clues?
Thanks

I found a solution here:
Handling buttons in custom dialogs
It works in my case.

dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener()
{
// Perform button logic
}

Why I am not sure why following the two examples mentioned in my post did not work, I figured out how to get it to work. I had to move the attachment of my listener to the button in the dialog's onStart() method from the dialog's onCreate() method.
It appear this is related to me also overriding the onStart() method in my custom dialog:
public void onStart() {
super.onStart();
setContentView(R.layout.heightdialog);
...
}
That code must have "zeroed" out my listeners which were in the onCreate() method.

In order to intercept button clicks HeightDialog must implement View.OnClickListener
public class HeightDialog extends Dialog implements View.OnClickListener
{
}

Related

Is there a way to reset the OnClickListener to its default Android implementation?

I have an editText to which I have set an OnClickListener, which is set to open a Dialog. But I have an option in the Dialog to let the user enter data into the editText by manually typing. I tried calling setOnClickListener(null), but it makes the editText unresponsive.
As of yet I have tried a lot of things, but the only thing that works is recreating the activity by calling recreate(), but I'd rather the user not know that I'm recreating the Activity.
How do I reset the editText to behave normally like an Android editText works? (like opening the keyboard and entering data on tapping it)
Change
editText.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
//do as u wish
}
}
);
to
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do as you wish
}
}
});
You can use a boolean member variable to keep track of when to allow user input and when to show the dialog.
private boolean mShouldAllowInput = false;
Then in your custom click listener you could do something like:
private View.OnClickListener editClickListener = new View.OnClickLIstener() {
#Override
public void onClick(View v) {
if(!mShouldAllowInput) {
showDialog();
mShouldAllowInput = true;
}
}
}
Now you need a way to revert back the value of the boolean member variable back to false. You can reset in the DialogInterface.OnClickListener as per your business logic.
To reset the OnClickListener of an EditText I tried:
View.OnClickListener defaultOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
v.requestFocus();
}
};
...
editText.setOnClickListener(defaultOnClickListener);
and it seems to work fine!

Access a button from another class in android

I know there are similar questions asked before in SO, but sorry to say that, none of them are serving my purpose.
I have a button in an activity class, and I want to give its functionality in another class.
Below is my HomeActivity code:
// Tile Button
tileButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TileButton tileView = new TileButton();
tileView.tile();
}
});
And here is TileButton.java class code:
public class TileButton {
HomeActivity homeActivity = new HomeActivity();
View view = homeActivity.hometabView;
public void tile(){
if(view.isShown()){
view.setVisibility(View.INVISIBLE);
}else{
view.setVisibility(View.VISIBLE);
}
}
}
Now when I press the tile button, a Null Pointer Exception is thrown. Below is the LogCat entry.
10-04 10:32:07.833: E/AndroidRuntime(5330): java.lang.NullPointerException
How do I solve this problem? Please help
Change:
public class TileButton {
public void tile(View view){
if(view.isShown()){
view.setVisibility(View.INVISIBLE);
}else{
view.setVisibility(View.VISIBLE);
}
}
}
// Tile Button
tileButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TileButton tileView = new TileButton();
tileView.tile(v);// you can pass any view from here
}
});
If you want to have same operation in both the activities, Create a public method in one of the activity and just call the method onClick of both buttons. But you cannot control the visibility of an activity which is not even on screen.

Android OnClickListener not firing for button on separate Layout

I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);

Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

Is it possible to show multiple Dialogs one over another? Is there something like Dialog Z-Level?
I am using DialogFragment where user chooses elements, when he comfirms his choice, it is saved to database and sent on server. if the save action fails I would like to inform user with ... another dialog is it possible? And will it not clear off my first dialog?
Thanks in advance.
Indeed, it's possible to show multiple dialog Fragments one inside another one. The z-order depends on the order they are created.
In the code below there is an example of a FragmentActivity with the behavior that you require.
public class MyActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
//...
}
public void onSave(View view) {
Intent intent = getIntent();
this.setResult(RESULT_OK, intent);
finish();
}
public void onCancel(View view) {
finish();
}
public void SelectWeekDay(View view) {
DialogFragment selectWeekDayFragment = new SelectWeekDayFragment();
selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
}
public class SelectWeekDayFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.week_day_dialog, container, true);
Button saveButton = (Button) view.findViewById(R.id.button_save);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox checkboxMonday = (CheckBox) getDialog().findViewById(R.id.checkBox_monday);
if (!checkboxMonday.isChecked()) {
DialogFragment saveErrorFragment = new SaveErrorFragment();
saveErrorFragment.show(getSupportFragmentManager(), "SaveErrorFragment");
}
else {
SaveToDb(); //Perform actions to store on db or what you wish
dismiss();
}
}
});
Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
}
public class SaveErrorFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage("You must select Monday").setPositiveButton("Ok", null).create();
}
}
}
My advice is to use a custom layout with a ViewFlipper inside your dialog so you can easily switch between a progress-bar or whatever different layouts you want to show. If you want to show multiple Dialogs my guess is that the z-order depends on the order they were created the latest beeing shown on top.
You usually can, however, just be a little careful. Use the dialog's lifecycle to your advantage to avoid side-effects. For example: you can do a check on a function like onStop() to see if the child dialog is open, and if so, close it.
Ideally, cutting down on the amount of layers of dialogs you have is ideal, as long as it's sane (for example: doing it ends up being hundreds of lines of code more)

Android: From a popup window, how do I call a method in the object that initiated the popup window?

I have an object "A"
"A" initiates the display of a popup window
That popup window has a button within it.
I want that button in the popup window to initiate a call to a method in "A".
I want to initiate the call from the line of code below that says:
"// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared"
Generally speaking how can I call a method in the object that declares the popup window, from that popup window? this seems like it would be soo easy, but I am soo Newbie with this OO stuff.
If this explanation is confusing I will be happy to embellish.
public EventsOverlay A = new EventsOverlay(a, b))
class EventsOverlay extends ItemizedOverlay<OverlayItem> {
private PrePopupPanel panel=new PrePopupPanel( R.layout.preview1);
#Override
protected boolean onTap(int index) {
panel.show(true);
return true;
}
...
} end the EventsOverly class
class PrePopupPanel {
View popup;
boolean isVisible=false;
PrePopupPanel(int layout) {
ViewGroup parent=(ViewGroup)mapView.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
ImageButton infobtn = (ImageButton)popup.findViewById(R.id.button1);
infobtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
hide();
// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared
}
});
... other methods like show(), hide() etc... copied from someone else
Your class is inside the other one, you may have access to all member method of your parent.
Use an Interface to do this.
-- class A should implement the interface
-- Pass this to PrePopupPanel.
-- call the method through the interface object.
Interface
package com.demo.interface;
public interface ICallHandler {
public void show(String show);
}
Class
class EventsOverlay extends ItemizedOverlay<OverlayItem> implements ICallHandler{
private PrePopupPanel panel=new PrePopupPanel( R.layout.preview1,this);
#Override
protected boolean onTap(int index) {
panel.show(true);
return true;
}
PrePopupPanel
Class PrePopupPanel {
View popup;
boolean isVisible=false;
ICallHandler mHandler;
PrePopupPanel(int layout, ICallHandler callHandler) {
mHandler = callHandler;
ViewGroup parent=(ViewGroup)mapView.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
ImageButton infobtn = (ImageButton)popup.findViewById(R.id.button1);
infobtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
hide();
// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared
mHandler.show();
}
});
Use Android Dialog methods.
In onCreateDialog you can pass a reference to your custom object and trigger any methods you want when user clicks a button. Then anywhere in your activity code call showDialog()

Categories

Resources