I've just about got the concept of using the Bundle instance state to store stuff that wouldn't otherwise be stored when an Activity is destroyed and recreated (e.g. on screen rotation). I also see that you don't need to do this for basic information for each View, like text in a TextView, because the system does that for you (at least for those Views that have an ID assigned). See http://developer.android.com/training/basics/activity-lifecycle/recreating.html.
But when I change the background color of a View programmatically (I'm using a basic View as a color swatch linked to a color picker) using setBackgroundColor(), I find that the color is lost when the screen rotates, and reverts back to the original setting. I do have an ID assigned to the View.
Should that color information be preserved automatically, or am I just being hopeful, and do I have to keep track of that separately and restore the color on recreation of the Activity?
Thanks.
Yes when you set your Background Color directly by View.setBackgroundColor() Always when the app is changed to landscape this is replace by the default layout. You should implement:
setContentView(R.layout."activitylayoutname");
And of course on the layout set a background parameters.
Edited 1:
Try this link to set background valĂșes:
Setting background colour of Android layout element.
And also if you want to preserve other valĂșes between Activities, just recreate an activity:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Related
I have defined array of ImageButton dynamically and that will take its buttons background from the user selected photos, my problem is that when I restart the app, the image buttons does not stay, how can I make it stay "along with the user selected background for it" without define it in the XML? I think of shared preferences but I am not sure if it will solve the problem!
icon[count]= new ImageButton(this);
icon[count].setImageResource(R.drawable.p1);
icon[count].setImageBitmap(photo);
To save Background color you have applied , Shared preferences is perfect solution.
You need to save state of background color and retrieve it when you are restarting app(onRestart()) or onPause() or onResume().
how can I make it stay "along with the user selected background for
it" without define it in the XML?
Using Shared preferences
Checkout Android Shared preferences example
I come from iOS background where I can display new views stuff on top of everything easily. In android, it doesn't seem as straight forward because each Activity has a layout already defined. So, if I wanted to create a loading view (spinner in the middle), adding a new Layout inside the original layout would mess things up.
Is there any way to do this?
I also tried use a dialogFragment and customizing the dialog, but it always shows the space where a title of the dialog is shown. I'm not sure how to get rid of this, so there must be some universal way for apps to do this.
You can use shared preferences to flag a value true, and in each each Activity's onResume, you can check for that value. If that value is true, then you can start an activity in a new thread which has just a spinner and a transparent background.
I'm using the following workaround:
Use a FrameLayout as your main layout. Define your UI as the first child (or just not the top frame), then define your spinner in the top. When you want to hide the spinner, just setVisible(View.GONE).
I have an application with a large number of buttons across the various screens. Each of the buttons has a background image as well as text (so ImageButton is no good). I am aware that it's possible to have multiple versions of an image for the different states a button can be in (e.g. pressed and not pressed), that it's possible to put the different versions of the image in a selector, and then to set the background of the button to be the selector. However, this means I need to create two versions of each image (times four for each of the different densities I'm considering) and thus bloating the size of my apk as well as adding considerable time to produce the duplicate images.
Is this the only way to change the look of a button for when it's pressed or not (i.e. to set its background to be a selector which links to two different images), or is there some other easier way to change the look of a button, like specifying some built-in property of the button (its 'darkness' for example)?
Found one way of showing a View as pressed or not by wrapping the View in a FrameLayout, giving the FrameLayout a foreground drawable which is a selector consisting of a black-ish transparent colour (pressed state) and fully transparent colour (default state), and then setting the click listener on the FrameLayout instead of the View. Wrote it out in greater detail on my Blog here:
http://adilatwork.blogspot.co.uk/2012/12/android-show-view-as-pressed-without.html
How to set the back ground image for whole application?I have achieved this but in some cases it throwing the Application background as black in some screens.
The thing you see on your screen is not an "application", but a contentView. this is made up from several things that can come from an XML file, and from code.
In the end you have several things on your screen that are Views. (for instance: linearView with textViews, etc).
Depending on what you have on your screen, you should do somehting to them. It is impossible to say what to do for all cases, as it differs. But in the end you should make sure that the background image is set for the 'big' View, and the views that are on top do not overlay it with something else.
You could set background for entire app activity window using
getWindow().setBackgroundDrawable(Drawable drawable)
//or
getWindow().setBackgroundDrawableResource(int resid)
but of course preferable to set background for root group of activity ContentView
I currently have my app using a xml style sheet but all the the test users are asking to set their own colors. I can easily get the data for the user but how do I go about changing everything.
I could have a class called after every activity was fired and have it reset the colors based on the user preferences. is there a better way?
COuldn't you use the findViewById(R.id.something) to get the view element from your xml layout
and then you change the colors you want?
(sorry if I misunderstood your question, in this case could you be more specific>-?)