I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()
Related
After trying so hard to make Home button appear in my ActionBar it finally showed up, but when clicking it nothing happens. What method or OnClick listener should I add to make it functional.
I admit this question has been asked frequently and after going through most of them, am still not satisfied so please moderators take it easy on me, please.
Anyway your help is greatly appreciated.
Here is the code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
//Method to listen on the home button tap and go back to previous activity
}
The button in the upper left of the Action bar is the Up button. If you've already added the android:parentActivityName name to your AndroidManifest, then the system does the correct thing already - navigate you back to that parent activity as per the documentation on the onSupportNavigateUp() method, which is the method that is triggered when that button is clicked.
I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack
When onPause is called, the Dialog is shown and it's background it's transparent so application behind is visible. So if I press Home button while Dialog is shown and then navigate back to application it shows Dialog with black background, if I dismiss Dialog application continuous normally. So how do I make background visible after navigating to application. Black background only happens when I press Home button so application is not visible. I'm guessing that something strange happens in onStop method...
This is how I call my Dialog:
pauseMenu = new PauseMenu();
pauseMenu.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pauseMenu.show();
And onCreate from Dialog class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pause_menu);
}
Your dialog is recreated automatically, so
pauseMenu.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
is not called.
So, try to call this set in your onCreate() after calling super.onCreate()
I tried to make a dialog on startup of my application like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hi Stackoverflow!").create().show();
}
But I want to make it so that if I click a checkbox in the dialog "Don't show again" then the second time I start the application the dialog doesn't appear. How can I do it?
If you just want a Dialog with a Button then you don't really need an AlertDialog. You can simply create a Dialog, create a layout for the Dialog in xml with the CheckBox and Button, then use setContentView() on the Dialog.
To not show the Dialog again simply create a boolean variable and use onCheckedChanged() to set that variable to true if the checkbox is checked. Save that in SharedPreferences and check that value on start up.
Good example of getting started with SharedPreferences
SharedPreferences complete docs
You can use SharedPreferences for this. Add a flag that tells you if the dialog has been shown before (or if the app has been launched before) and determine from there whether to show the dialog or not.
I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?