Android set XML shape as drawable programmatically - android

Hello I have a drawable myshape.xml, it contains a <shape> and I cannot set an android:id to shapes.
In my code I want to set the background of a view to this file using
catAll.setBackgroundDrawable(getResources().getDrawable(R.id......???));
where myshape.xml does not show up in my R file because it has no id. and I cannot set id to object.
In my XML I do set the shape by simply typing the drawable resource name. But I need to do this programmatically.

You don't need to get the drawable yourself. Use this instead:
catAll.setBackgroundResource(R.drawable.myshape);
For future reference, if you do wish to get the drawable keep in mind that drawables live in the R.drawable namespace. So your code would became:
getResources().getDrawable(R.drawable.myshape);
This is akin to what you do in your XML:
#drawable/myshape
instead of
#id/myshape

The question is really old but googles first hit references to this thread.
So getDrawable(id) is deprecated.
Short solution (kotlin)
yourView.background = ContextCompat.getDrawable(context, R.drawable.your_ressource_id)
For more, please read this: https://stackoverflow.com/a/29146895/4420355

Related

Fill color of vector drawables that are icons in dialogs or toolbars

What if I would want to use vector drawables as icons in dialogs or logos in toolbars? Then I would use them like this:
alertDialogBuilder.setIcon(R.drawable.my_vector_drawable);
toolbar.setLogo(R.drawable.my_vector_drawable);
toolbar.setNavigationIcon(R.drawable.my_vector_drawable);
...
Am I right?
But here comes up a question: how can I change their fill color without modifying vector xml files?
For instatnce I can change fill color of any vector that is in view by using 'tint' tag in xml or 'setColorFilter()' method in code.
If you want change fill color not change xml file you must create Drawable instances of this file. This instance get you method to change your file.
for example:
Drawable myIcon = ContextCompat.getDrawable(this,R.drawable.my_vector_drawable);
myIcon.setColorFilter(ContextCompat.getColor(this, R.color.yourcolor));
alertDialogBuilder.setIcon(myIcon);
toolbar.setLogo(myIcon);
toolbar.setNavigationIcon(myIcon);

What are insets in android?

I am beginner at android development. I was recently looking at someone else's code and found some functions view.onApplyWindowInsets(windowInsets), windowInsets.getSystemWindowInsetTop(). And this word was used frequently in the same app.
I tried googling it and found InsetDrwable class with explanation
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.
Can some one explain me what is the meaning on Insets and what those piece of code up there meant?
A explanation with an example will be appreciated.
Suppose you have a TextView and you need to add a background to the TextView. But, on the other hand, you don't want the background to scan the entire View (TextView), therefore, you need to use Insets.
A Drawable that insets another Drawable by a specified distance or
fraction of the content bounds. This is used when a View needs a
background that is smaller than the View's actual bounds.
The background can be a Drawable. Hence you need to use the <inset> attribute inside your xml file (activity_main.xml) "For Example".
Then, after using the <inset> tag, you can specify some attributes such as:
<inset
android:drawable="#drawable/(Enter the file name under drawable)"
android:insetBottom="4dp"
android:insetTop="4dp"/>
For more information please have a look at InsetDrawable on Android developer.com
Hope this helps!

Is there any way to change the background color activity through java file in android?

I try to change the background color activity through java file but it does not work
so,
Is there are any way to change the background color through java file (Not throw XML file) ?
You can change the background of Activity from code using a Drawable
Set a Drawable from Resource like this
getWindow().setBackgroundDrawableResource(R.drawable.your_bg);
Or set a Color like this
getWindow().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
Yes, in code you can do this:
LinearLayout ll = (LinearLayout)findViewById(R.layout.blahblah);
ll.setBackgroundColor(R.color.FireBrick);
The java code above is equivalent to placing this xml attribute inside your layout:
android:background="#color/FireBrick"
To make it easier on you, I recommend you placing this color.xml file inside your res/values/ folder. The FireBrick color is defined inside that particular file.

Is it possible to set the background of Drawable to a Drawable class name in XML?

I found some code in our project where someone is iterating over the children of a ViewGroup so they can set a custom Java-based drawable as the background. I'd like to clean that up a bit by setting the background drawable in our layout XML files.
Is there a way to specify a Java class to use as the background via the XML layout definition?
I think you can reference the java class by specifying it's full path (i.e. its package).
For instance, com.example.android.yourClass.
After looking around quite a bit I was unable to find a way to do this. I had to specify the background drawable in code.

Path of android.R.id.empty drawable?

I am specifically looking for the drawable android.R.id.empty.
Does any one have an idea what the source path of that default Android drawable is?
The file where it is defined is located in
YOUR_SKD_DIRECTORY/platforms/A_PLATFORM/data/res/values/ids.xml
It is not a drawable but an id as you can clearly see from android.R.id.empty.
This id is meant to be set on a View which should be displayed in case your ListView is empty.
I stumpled upon a really nice guide for Froyo drawables. And don't forget to use android.R.drawables for your integer id. http://androiddrawableexplorer.appspot.com/

Categories

Resources