Libgdx - How to close Dialog and continue operations on current screen? - android

I have a problem with closing the Dialog. I can close it, but I have to press the button more than once. I have two buttons and one that is used for closing the application works fine, but the second one for closing the dialog doesn't work properly.
The button: button("No", "continue");
I'm using remove() method in overriden method result(Object object) when a parameter object is equal to "continue".
#Override
protected void result(Object object) {
if (object.equals("exit")){
Gdx.app.exit();
}
else if (object.equals("continue")){
remove();
}
}
I have also tried to use hide(null); but I got the same result.

This is my working dialog code for "Are you sure you want to quit?"
Dialog dialog = new Dialog("Exit", Assets.skin1) {
#Override
protected void result(Object object) {
if ((Boolean) object) {
Gdx.app.exit();
}else{
//not necessary but if dialog not hide, call hide() here
//hide();
}
}
};
dialog.text("Are you sure you want to quit?");
dialog.button("Yes", true).button("No", false);
dialog.getContentTable().pad(20);
dialog.getTitleTable().pad(20);
dialog.padTop(60); // set padding on top of the dialog title
dialog.setModal(true);
dialog.setMovable(false);
dialog.setResizable(false);
dialog.show(stage);
"No" button will close the dialog.

Related

Android bar chart: undo selection

There's some way to undo a selection on Mikephil charting? I have an application which opens an activity when I selects one value in a bar chart. That works fine, however, when I returns to activity that contains the chart, the selection remains. So, when I selects again, the selection is cleared and the activity does not open. What I want is ever I select a value on bar chart the function "onValueSelected" be executed. How can I do that?
That is the code fragment which calls assyncronously an activity when a value is selected.
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if(e.getVal() == 0);
else {
GetClientesCadastradosDiaAsync task = new GetClientesCadastradosDiaAsync();
task.execute();
}
}
#Override
public void onNothingSelected() {
// do nothing
}
});
At the end of your onValueSelected() method, call:
chart.highlightValues(null);
Now, this will only remove the highlighting. If you select that same bar again, onNothingSelected() will be called.
Therefore, in onNothingSelected(), again call onValueSelected(). You will have to pass the parameters here, but looks like you will only need the Entry parameter, and for the other 2 you can pass null.

show/hide the content by clicking button

Hi I want to show/hide the content by clicking button. I am having a problem to hide the content by clicking button.
Here is my code for hiding the content
private boolean visible;
protected Button SearchButton;
private void Toggle(){
if(visible=false){
DishButton.setVisibility(View.INVISIBLE);
SpoonButton.setVisibility(View.INVISIBLE);
cupButton.setVisibility(View.INVISIBLE);
FridgeButton.setVisibility(View.INVISIBLE);
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}
if(visible=false)
is not gonna work!
Use if(visible==false).
Note that you can use View.GONE to hide the content and free the empty space.
From your comments and question it seems like
You have not put any listener to your button.
You have written = in place of ==.
You have user View.INVISILE which will permanently hide the element it will not come back. SO use View.GONE
You have some logic flaw in case of handling visible/invisible.
You have not initialized visible boolean with true because as first time you are showing all the buttons so it should be true.
So possible solution is
In your onCreate() method add
visible=true;
SearchButton.setOnclickListener(new OnClickListener()
{
public void onClick(View v)
{
Toggle();
}
});
And make the Toggle method look like
private void Toggle(){
if(visible==true){
DishButton.setVisibility(View.GONE);
SpoonButton.setVisibility(View.GONE);
cupButton.setVisibility(View.GONE);
FridgeButton.setVisibility(View.GONE);
visible=false;
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}

Android Layout button partially appears

So,
I have an activity with a layout, and in this layout I only have one button.
when clicking this button, the activity sets the visibility of the button to invisible, and launches a popup window.
I implemented a simple onDismiss function in this popup, which sets the button to visible
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
});
the problem is that sometimes, not very often, after the popup is dismissed, the button is shown, but only the top part of it, something like 1/5 of the button.
I suspected that the button became visible before the popup dismissed completely, and a sort of a clash happened between them, but on the other hand I made some checks and the popup window and the button are able to be shown at the same time without a problem, so a "layout clash" cannot be the reaon, right?
You can add delay and run this method on a handler.
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
}, 1000);
};
});
I would suggest making the button variable non static and instead call a method of your activity from your listener and in this method set the button visibility. Having the button as a static variable could mean that although it is non null the button isn't added to the activities view at the point when you call to set is visibility.

Attaching listener to dialog

I need to be notified when there are changes on the screen. Currently I am using
this.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Log.d("TAG", "GLOBAL LAYOUT");
}
});
But it doesn't work when dialog(custom, alert, progress, etc.) is shown or dismissed. I understand that dialogs are shown on another overlay so listener isn't attached to them. How can I get my desired functionality?
You probably need a dialog.setOnDismissListener or dialog.setOnCancelListener
They'll be called when something related to dialog dismissal happens or is being canceled.

DialogPreference shouldn't close if no option selected

I have a DialogPreference and I want to avoid the user from closing it when pressing "OK", "Cancel", etc.
How should I do that?
EDIT:
I tried to reach the OK button to disable when the dialog is created. But I couldn't make it :(
The solution is quite easy. Overwrite showDialog and set your own click listener to the buttons you want to intercept.
#Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
Button pos = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
pos.setOnClickListener(...);
}
In your click listener you can do the validation you want.
A tweak could be to create a custom dialog where you define your own buttons (OK and Close).
public class YourClass implements OnClickListener {
private Button DialogButton;
private Dialog dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainLayout);
/* Your code... */
DialogButton = (Button) findViewById(R.id.DialogButtonId);
DialogButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.DialogButtonId:
LayoutInflater inflater = LayoutInflater.from(YourClass.this);
final View inflay = inflater.inflate(R.layout.DialogLayout, (ViewGroup) findViewById(R.id.RootIdOfDialogLayout));
TextView YourTextView = (TextView) inflay.findViewById(R.id.TextViewId);
Button cancel = (Button) inflay.findViewById(R.id.CancelButtonId);
cancel.setOnClickListener(YourClass.this);
Button ok = (Button) inflay.findViewById(R.id.OkButtonId);
ok.setOnClickListener(YourClass.this);
dialog = new Dialog(YourClass.this);
dialog.setContentView(inflay);
dialog.setTitle(getString(R.string.TitleStringId));
dialog.show();
break;
case R.id.CancelButtonId:
/* Checking if the user selected an option if true call dialog.dismiss() */
break;
case R.id.OkButtonId:
/* Here handle your preferences (e.g. putString(String key, String value)) */
/* Checking if the user selected an option if true call dialog.dismiss() */
break;
}
}
}
Check out http://developer.android.com/reference/android/content/SharedPreferences.Editor.html in order to handle your preference in onClick. I didn't test this code just wrote it to show you how you could solve it!
The dialog stays open until you call dialog.dismiss();. In that case you'll have to create your drop-down-menu, polls or what ever you want to display in your layout file. After pressing ok or cancel you should check if the user made a choice, and parse that choice into your preferences. (check link above)
Rgds
Layne
You could try opening it again.
Why would you want to prevent users to close the dialog? Users should be able to have 'full' control of their device.
You can see the source code of DialogPreferences here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/preference/DialogPreference.java
And then, copy most of it to your code, modifying the code as needed.
How about overriding the onDismiss() method and implementing a canExit() method with the validations you want to occcur? E.g. :
public class MyDialogPref extends DialogPreference {
#override public void onDismiss(DialogInterface dialog) {
if (canExit()) {
super.onDismiss(dialog);
}
}
...
}
A good UI should have a default selection/option already selected (the previously user-entered options or a program default).
Presenting a dialog asking for a change in options without any indication of what you already have is bad UI design.
This way if the user clicks Cancel, nothing changes and they saw what the option selected was. If they make no change and click OK then nothing really changes either.
Software is supposed to make doing specific tasks easier, not force the user to process the apps logic themselves.

Categories

Resources