how to use image as a theme - android

I need to use perticular image as the theme for my most of the activities in my application...
please help me how to do this..

Basic idea is to define and apply the custom style of your Activities in Res files. Tutorial here.

if your using XML display there is a field background
#drawable/custom_dialog_background
or
android:background in the XML anchors
setBackgroundResource(int) in your code
http://developer.android.com/reference/android/view/View.html#attr_android:background

Related

Android : Setting textview background in 'style' in XML

Currently, am setting the background of a TextView using code like this:
textView.setBackgroundResource(R.drawable.rect_with_border_grey);
I then came to know about dimens.xml usage and how you can set through that file. Is it possible to set the background through ? i.e. I want to do the above code line through XML. Any help please?
I wouldn't do this using layout xml file as #Opiatefuchs pointed out (in the comment below). B'cos, this textview's background will change depending on the user setting in the App dynamically.
Make a style like
<style name="MyTextStyle">
Do everything you want to do with your TextView here.
</style>
and then assign that style to your textview in xml like this
style="#style/MyTextStyle"
It will work.
dimen.xml is only use for giving dimension only. for setting background you need to do it with either layout xml file or from java.
Its up-to the requirement of setting background.
I finally found that you cannot set the background of a TextView via element (i.e. XML) in Android. You need to use layout XML. But, in my case, I cannot use layout since the background will change dynamically based on user choice in the App.
create dimens.xml file like this:
<resources>
<dimen name="paddingTop">10dp</dimen>
<dimen name="paddingRight">20dp</dimen>
...
</resources>
then use it in your layout xml files like this:
android:layout_marginTop="#dimen/paddingTop"
android:layout_marginTop="#dimen/paddingRight"

how i change background for all activity android

I put an image for all activity in my application (in style.xml ...). how I can change the background for all my activity dynamically. is it possible to change the properties of a theme dynamically?
thank you for your help
You can change window's background in your activity using
getWindow().setBackgroundDrawable(...);
getWindow().setBackgroundDrawableResource(...);
See Window docs.
Hi You can set the Background on each Activity XML Layout..
android:background="#drawable/bg"

change the background of actionbar from java code as defined in the style.xml

i have set the theme as:
<application
........
android:theme="#style/Theme.Example" >
.......
.......
</application>
i have several fragment in one activity. in one of my fragment i have change the color of actionbar as:
((MainActivity)getActivity()).getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(bitmap1.getPixel(0, 0)));
this works fine. now, i want to change the actionbar as it defined in #style/Theme.Example while i am traveling to another fragment.
after googling for several times, i find out only how to change the color or backround drawable of actionbar. not how to get back to the default actionbar style as it is defined in the style.xml
any help or link would be too much helpful :) thank you.
if it is needed, i can provide the style.xml. i have created them from this actionbar style generator tool
oh my god, i have found the answer in 5 min after posting the questions. i have used this:
Drawable d = getResources().getDrawable(R.drawable.ab_background_textured_example);
((MainActivity)getActivity()).getSupportActionBar().setBackgroundDrawable(d);
if you are asking that what the hell is R.drawable.ab_background_textured_example is, then you need to read my question properly and check out the link. on that page i have downloaded the zip file which contains the graphics. in the res/drawable there is a xml which is named ab_background_textured_example. and this is the default background of my actionbar. cheer.
if anyone need know more about it, just comment bellow.

How to view default android simple_spinner_dropdown_item style

Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout

How to change background image of a ImageButton in android

I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"

Categories

Resources