set image button background dynamically - android

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

Related

android change background image from settings

In my application, four activies are present (Add, view , search and settings). in settings activity, it will display 10 buttons ie. apply to set background image. images are saved in drawable folder. if i click on button image it should change the background of all activities
First of all you need to use SharedPreferences.
set value of selected background in SharedPreference and display images according to the value of it in all Activities

Preserving View background colour on recreation of Activity

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

how to add background image to preferences activity in android?

i am new to custom PreferencesActivity, i am working since three days to get custom preferences, what i want is i want a preferences screen with background image and color views for every preference cell,
1) i had tried with res/xml/preferences.xml, but i cant create background image i had tried with many solutions but i couldn't find any solution.
2)i had tried with res/layout/pref.xml, but i couldn't able to save the settings of preferences
please help me in creating such type of preferencesActivity with background image
see How set background drawable programmatically in Android
and save this link http://developer.android.com/reference/android/content/SharedPreferences.html

Android save programmatically created button

In my program i create buttons programmatically on OnTouch event but when i reopen the app or activity get restarted these button do not appear
Can any one suggest me how can i create buttons programmatically those will stick to the layout even after the activity gets restart or app gets restart
Use SharedPreferences to store coordinates value.
As suggested in Comments saving state in shared preferences is good option .
So you can go with this links to know more about this topic, I think it will be more clear when you will look into all this .
Save instance of dynamically generated views when we switch back and forth between activities
How do you save a (user-created) dynamic Layout in Android?
android save programmatically created views
Android - How to save a programmatically created view and restore it on app restart

Preference with in-line options

I've created a PReference Screen which lets the user manage a series of elements marked by himself as "favorites". I create a Preference category then fill it up with options dynamically, taking them from a sqlite database. This is a screencap:
And this is a screen mockup of what I want to achieve:
That is, I want for every preference item to add an in-line remove option that allows users to directly remove favorites.
The problem is I don't know how to do this, and I've seen no similar examples. Is it even possible?
Android provides an easy means to implement preferences screen, but standard android ListPreference does not provide an easy way to display an image for each item if the list. To add the images to the Android ListPreference, we need to use a custom xml attribute
Here is some example that may help you
http://www.cmwmobile.com/index.php?option=com_content&view=article&id=4&Itemid=12
How to add icons to Preference
To delete prefrence from prefrence screen you you can refer this Link : How to determine which preference item from custom layout button click

Categories

Resources