I have an ImageView on an Activity A, and a button on Activity B.
The ImageView is set to "invisible". I was wondering if i could make the ImageView visible when the button is pressed and keep visible forever (until the user uninstalls the app or resets it).
I found this piece of code that makes the ImageView turn visible:
example.setVisibility(View.VISIBLE);
i know i should use SharedPreferences to make it work, but i tried many times, without success.
Can somebody help me?
Thank you so much.
P.s. What i have to do is to create (or simply make visible) a tick so that the user knows which level he completed. If there's another way, and i know there is, let me know.
It seems you have set the visibility of your ImageView in XML using
android:visibility="invisible"
Instead of that always set the visibility in code using something like -
SharedPreferences sharedPreferences;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(getString(R.string.sp_key),
MODE_PRIVATE);
ImageView example = (ImageView) findViewById(R.id.example_image);
boolean visible = sharedPreferences.getBoolean(R.string.visible, false);
if (visible) {
example.setVisibility(View.VISIBLE);
} else {
example.setVisibility(View.INVISIBLE);
}
}
Then where the user clicks on a checkbox or something to show they want to make your ImageView visible, save this to the SharedPreferences.
Have a look at How to use SharedPreferences in Android to store, fetch and edit values for more details on SharedPreferences example.
I solved. Rnk's method is the key. It returned error because i was using findViewById with an object from another Layout and it returned null point. So i imported the layout where the imageView is, and i solved. THANK YOU.
Related
I have an Android app, that allows the user to dynamically add their own buttons to the layout. I need to make it so that once the app is closed and re-opened, this dynamically added button returns to the layout. Instead of loading the default layout.
Currently, I'm dynamically adding buttons through the ActionBar of the App:
if (id == R.id.add_button)
{
String string = "Adding Button in Progress";
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show( );
Button myButton = new Button(this);
myButton.setText("Button");
RelativeLayout layout = (RelativeLayout)findViewById(R.id.Layout1);
layout.addView(myButton);
//myButton.setVisibility(View.VISIBLE);
return true;
}
This creates the Button fine, however when the app is closed and re-opened, it launches up the default layout.
I've done some research on having the app save and reload the updated layout. It seems that I need to use onSaveInstanceState. Here is what I have so far in terms of trying to save the layout:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the app state here:
savedInstanceState.putAll(savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
And here is what I have in terms of trying to "reload/restore" said layout. Notice I'm not using onRestoreInstanceState, instead I'm doing it through the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
//savedInstanceState.get(savedInstanceState);
}
else
{
//initialize members with default values for a new instance
setContentView(R.layout.activity_main);
}
}
I'm not sure if I'm saving/loading correctly, but any advice on how I can accomplish this task would be greatly appreciated.
Thanks for your time!
P.S. I'm still a fairly new member so I couldn't comment/ask questions on existing threads.
I know there's a lot of information out there on trying to load/save layouts, however in my case I need it to save the Button, and not a string of user text. In other words, its not a fixed value that I need to save. If the user adds n buttons, when the app is exited and relaunched, it should have those same 3 buttons.
Saved Instance State is a key-value pair about your activity. The documentation clearly says that it is destroyed when the app is closed .(pressing back button or if the system itself shutsdown the app).This is only useful when you are navigating within the app or changing orientation.
One solution is to create a Shared Preference of the details your application needs to identify the given structure consisting the dynamic contents. The fetch the values whenever you open the app and code according to it.
Other solutions are to use databases or files to store data about your dynamic content.
just store the String or Boolean.. when ever you try to use them in onRestoreInstanceState then create Button or anything dynamically and use stored String or boolean to set text on them
I have a main activity that loads a PreferenceFragment (its part of an actionBar).
Within the PreferenceFragment I load my preferences from a XML File:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
The Problem is if I change the Tab (remember it is getting managed by an ActionBar), change one of the preferences in a different fragment and coming back to my preferenceFragment its not getting updated. Particularly the problem is a SwitchPreference(true/false) which can be changed from different fragments and even by remote calls (then the preference is getting changed in shared preferences. YES I did commit the change).
I searched for different solutions, but to be honest I didn´t find a working one.
My own ideas are the following:
My Problem would be solved if I could get the Switch Element of the switch, so I could set the switch to true in the onStart() or onResume() method. But is there a change to get the Switch Object?
If I would load a usual layout I could access the switch like this:
View v = inflater.inflate(R.layout.start_fragment, container, false);
Switch status = (Switch) v.findViewById(R.id.switch1);
Afterwards I would be able to set the Position of the Switch like this:
status.setChecked(true);
Another solution would be to destroy the actual view and call addPreferencesFromResource() again, but to be honest I have no idea how this could be done....
The third solution could be to use a OnCheckedChangeListener in my PreferenceFragment, but the problem would again be how to change/update the switch.
I am sure that the Preference is updated correctly, because I´m running a OnSharedPreferenceChangeListener in one of my services and to debug this problem I made the listener log the status of my switchPreference.
Can you help me somehow?
unfortunately I cannot answer my own question before tomorrow morning, so I edit my question:
Thank you guys, I don´t want to restart the underlying activity, but only the fragment.
Fortunately I found a clue in this thread: How do you refresh PreferenceActivity to show changes in the settings?
I can really access the SwitchPreference that simple.
My solution is:
private SwitchPreference pref_phone_locked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
status = (SwitchPreference) findPreference("status");
}
public void onStart(){
super.onStart();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
if(sharedPref.getBoolean("status", false)){
status.setChecked(true);
}else{
status.setChecked(false);
}
}
This way I can simply cast the Preference to a SwitchPreference and modify it after every onStart() call. I guess this is the best solution for my problem.
Hopefully this answer will save someones time in future =)
Thanks you guys again!
I'm not sure if this will work for you ( I've a very basic knowlege of Android ) but I found this solution in a similar question and it works very well for me
private void simulateRefresh(){
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
Basically this destroy and recreate the activity without animation in between.
Found here
I have got an activity where the user can enter host name, user name and password and then click on a "Verify credentials" button. Then the credentials will be checked, which will take some time. In the meantime the user should neither be able to change the credentials nor to click on "Verify" again. So, a modal dialog like the ProgressDialog seems perfect for this.
Unfortunately, ProgressDialog has the well-know limitations regarding orientation changes etc. The guide (UI/Dialogs) tells to avoid ProgressDialog at all and use a ProgressBar in the layout instead (like in Progress & Activity). What does this mean? Shall I create another activity with just one progress bar? Or disable all input fields and put a progress bar on top of it? Sounds quite weird to me... whats your preferred solution?
Best thing which I use is:
Put a ProgressBar just beside the Login Button.
I have put a progressbar beside it(Whose visibility is set to View.GONE) in the OnCreate method.
When the user clicks on the Login/Submit button, I set the visibility of the button to View.GONE and visibility of ProgressBar to View.VISIBLE.
It looks good and the user cannot click on the button until the work is done, If an error occurs, toggle the visibility to let the user try again
Like #micro.pravi mentioned in his answer, you can implement the ProgressBar inside your layout. To keep the state after an orientation change you have to use onSaveInstanceState and onRestoreInstanceState to save and restore important values, i.e. private variables, like the private boolean isChecking
public class MyActivity extends Activity {
public boolean isProcessing;
#Override
public void onCreate(Bundle stateBundle) {
super.onCreate(stateBundle);
// Set Layout
setContentView(R.layout.main);
if(stateBundle!=null) {
// read your data here from the bundle
isProcessing = stateBundle.getBoolean("isProcessing");
}
setUiState(isChecking);
}
#Override
protected void onRestoreInstanceState(Bundle stateBundle) {
// Second value of getBoolean is the default value
isProcessing = stateBundle.getBoolean("isProcessing", false);
super.onRestoreInstanceState(stateBundle);
}
#Override
protected void onSaveInstanceState(Bundle stateBundle) {
// Save the critical data
stateBundle.putString("isProcessing", isProcessing);
super.onSaveInstanceState(stateBundle);
}
#Override
protected onResume() {
setUiState(isProcessing);
}
private setUiState(boolean processing) {
textView.setEnabled(!processing);
button.setEnabled(!processing);
progressbar.setVisibility(processing?View.VISIBLE:View.GONE);
}
}
This should be used to saved any critical data on orientation change or when the App is being killed and later restored by the OS. You don't have to save your TextView data, as the defautl View elements already handle this by themselves. Also do not store Image data this way. Instead store the Uri or path to the Url and load it on restore
For temporarily solving your problem, you can continue using the Progress Dialog and put this line in your Login Activity's tag in Manifest.xml file :
android:configChanges="orientation|keyboardHidden|screenSize"
Using this line of code will not affect the Progress Dialog on orientation changes. But it is considered a wrong practice according to Android Development's Documentation.
In the long run, i recommend you to Preserve the states for orientation changes.
I have successfully set an image to an ImageButton using this code:
mGetClickTime.setImageResource(randomImageId);
But I'm trying to problematically change the image by calling a method which contains:
mGetClickTime.setImageResource(randomImageId);
(In which the randomImageId vraiable is different)
However the programmatic change is not working.
Do I need to remove the current image before setting a new one? If so, how do I do that?
EDIT
The problem seems to be that the above command stops working after another activity has been called and completed. After that happens the setImageResource just doesn't work.
I'm not sure why this could be. I've tried commenting out everything from the second activity apart from this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
but the problem still occurs. Why?
Give this a try:
mGetClickTime.setBackgroundResource(randomImageId);
This will work in an onClick method. That might be your issue, but not 100% sure until I see the context of the code.
My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.