How I can change the Background in runtime in Android? - android

I want to make a simple program with 2 Radiobutton (rd1 and rd2), what can change background to picture s1, or s2, depend on what was selected. How I can make it? I tried background.setBackground (background is my RadioGroup's name), but it didn't work.

try this for changing android wallpaper.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/SetWallpaperActivity.html
and if you want change the background for the current activity means
if your layout is lay means then,
lay.BackgroundDrawable(R.drawable.abc);
in onClick listenerof ur radio button
sure it will help you dude..let me know what happened...
Thank you.

Related

Android, Simple box inside a button?

I want to have a button like the one in the link below. How can i achieve it just by java code or xml? By the way it's going to be of a different color when pressed. I've explored similar posts but none helped. Any help appreciated.
The button which i want
Just make a View with large rectangle and add what you want(e.g. Label contains 'Number Of Students' and/or something else) in the view.
Then, add onClickListener to the View.
To change clicked colour, refer to here

Fill with Color on Android

I have an image which include 10 boxes on my image view. When I click the one of boxes I want to change color of box (fill with color). How do I do that? Should I use Surface View or something else?
Update:
I found a solution. But if I use AsyncTask the code does not work. If I use a normal class the code works. I do not know why but I think problem is related with invalidate().

Show selected button

I have a group of buttons which enable user to select certain views on the touch screen.
I want to have the user know which view is active by showing a halo (or any other style) around the button. I mean when the button is clicked it's normal state is changed to different style but it is still normal.
I googled and checked in stackoverflow, that it is not possible to change the style at runtime.
How can I achieve the desired effect?
I have some silly ideas which I won't implement unless as last resort, like making two buttons at same positions and changing their visibility.
Or I can put each button in it's own linear layout and then set the background color of each of them, if it is possible.
Or some ImageButton manipulation maybe.
I have already overridden the default style for button for normal pressed states.
And setting focus(when in focusInTouchMode) causes problem, so that's a NO (already tried that).
Even if style can't be changed, please advise some tip to achieve the effect, it's not necessary to do it the style way. I just want o let the user know which view is selected through buttons.
use state list and set its focussed state drawable as the button with a ring around it and then at runtime say button.requestFocus(). see here for more on state list...
Go for selector xml background for each button. Best Resort
Check this link
How to set image button backgroundimage for different state?
Hpoe this helps
or you can use like ::
Button button;
public void onClick(View v)
{
button.button.setBackgroundColor(**color**);
button.setBackgroundResource(**res**)
}

setBackgroundDrawable is not changing the LayoutBackground

I have seen several instances of this question on here and the provided solutions dont seem to work.
My goal : Update the background of my LinearLayout depending on what color i get back from a database query.
What I have so far:
if(teamc=="black"){
drawable = this.getResources().getDrawable(R.drawable.blackbackground);
Toast.makeText(TeamActivity.this,teamc, Toast.LENGTH_LONG).show();
teamColor.setBackgroundDrawable(drawable);
}
Team color is defined as
teamColor = (LinearLayout) findViewById(R.id.teamcolor);
What is happening is that the Toast is appearing just fine but the background is not changing.
Any help would be appreciated.
Are you sure that that toast is showing and not other toast? Because you try to check a String if it's equal to another String by using == operator and that is wrong.
You have to use .equals() method to make a comparison between two Objects.
It is really strange if Toast is working but background is not changing.
Try using teamc.equals("black"){}
or
teamColor.setBackgroundResource(R.drawable.blackbackground);
try to make directly teamColor.setBackgroundResource(R.drawable.blackbackground);
call the invalidate() of the LinearLayout using teamColor object after set the background
teamColor.invalidate();
In my application I use a WebView to display a web page, when the page is scale fits well but now are whites spaces on each side of the page, I can change the background to black color??

Android - Changing the size of the Checkbox itself

I did my homework and this question has been asked a few times, but never answered with a solution to the problem. The question is, how do you change the size of the checkbox button in a checkbox compound button?
In my case I have a checkbox with a custom button attribute pointing to a selector xml file containing items for each possible state pointing to png images. I want to be able to specify in dp the size that these images are displayed as the button for the checkbox.
The answer to this question is NOT to change the android:layout_width of the checkbox itself. Anyone know how to do this?
Also, things I've tried include adding widths and heights to the selector and items. I've tried adding my selector to a Button and resizing the button (which works for the button alone), but I can't figure out how to specify to the CheckBox that it should use that Button xml.
I just ran into this problem myself. My checkbox has no text, and in that case this works well:
set
android:background="#my_selector_drawable"
instead of the button attribute. To avoid the standard button on top of the image, set
android:button="#null"
Now you can set the size of the checkbox with the normal android:layout_width & android:layout_height.
If you want a checkbox with text as well, this wont work
Well for now the best I can do seems to be to build my own checkbox using a button by keeping a boolean for the state that gets inverted in the OnClickListener and calling setBackgroundResource to swap between the "checked" and "unchecked" drawables.
Meh. At least I can resize the button to be exactly what I want. Hopefully someone will come up with a better way to do it.

Categories

Resources