Android Widget ImageView (vector). Set tint (programmatically) RemoteViewsCompat - android

I'm currently developing an Android widget containing an ImageView which is a Vector.
Now I need to set the tint of this ImageView (In best cast programmatically).
As a workaround (if this is not working), I could set the color within the ImageView, but not NOT within the image asset itself as described here.
app:tint="#color/colorPrimary" within the ImageView seems to be ignored.
The following seems to work:
<ImageView
android:id="#+id/item_plus_symbol"
android:layout_width="16sp"
android:layout_height="16sp"
android:layout_marginStart="2dp"
android:layout_marginTop="3dp"
android:src="#drawable/ic_plus_filled"
android:tint="#color/colorPrimary"
tools:ignore="UseAppTint" />
But is obviously not the best solution (tools:ignore="UseAppTint").
Afterward, I could multiply the ImageView and set the color using the visibility of the different ImageViews.
But as said before, I actually want to set the color programmatically.

Related

Java/Android get.Background() setting alpha to all instances of drawable? [duplicate]

I've recently updated my phone to Android Marshmallow and ran my existing app on it, but noticed a difference in color behavior: When applying changes to the background of a view (drawable), all views that share the same background (reference) will also the same changes applied. While previously, this was not the case.
Example
In this example, I have a two views with the same background color, and I want to change the alpha level of one of both views.
First we define the views in the layout:
<LinearLayout
android:id="#+id/test1"
android:orientation="horizontal"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/testColor2">
</LinearLayout>
<LinearLayout
android:id="#+id/test2"
android:orientation="horizontal"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/testColor1"
android:layout_marginLeft="5dp">
</LinearLayout>
Both views share the same background color or drawable:
<color name="testColor1">#3F51B5</color>
<color name="testColor2">#3F51B5</color>
The result looks like this:
Now we are going to change one of the two background, like this:
LinearLayout test1 = (LinearLayout) findViewById(R.id.test1);
LinearLayout test2 = (LinearLayout) findViewById(R.id.test2);
test1.getBackground().setAlpha(80);
Which results in this:
However, the desired and expected result is obviously this:
Download the sample project here.
A few thoughs:
When setting the Alpha level trough XML, this behavior does not apply.
It does not matter if both views refer to a different color definition in colors.xml (like in the example), refer to the same color definition of both have the same color (hex) directly in the view's xml file.
Question
How can I make changes to a view's background without this affecting other views that share the same background. Preferably while still being able to use a background that directly refers to a color defined in the color's xml file
Most likely the class of each view's background and constantstate are
the same object. It seems as if the two color resources have been
"merged" somewhere -- meaning they have shared ConstantState. Maybe in
the Resources class' caching? I would've expected them to stay
separate since they're different resources (albeit with the same color
value), but apparently not.
– Snild Dolkow
The ColorDrawable's state stores alpha, so any changes to one will change the others. To prevent this, you can first call mutate() on the drawable, separating the two drawables (by making a copy of the state).
In the example, this would result in using test1.getBackground().mutate().setAlpha(80); instead of directly applying the alpha.

Adding background to button breaks sizing

I have an icon I would like to scale down to 48dp and use as a button (no background). However, adding the android:background attribute (to make the background transparent) seems to break the sizing I have applied to the button and it displays at its full size:
Desired size but with ugly background:
<ImageButton
android:id="#+id/composition_send"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:scaleType="fitCenter"
android:src="#drawable/ic_send_black_48dp"/>
Full (wrong) size, but with no background (desired):
<ImageButton
...
android:background="?android:selectableItemBackground"/>
Why does adding that one line ruin the sizing?
try to add
android:src="#drawable/ic_send_black_48dp
android:backgroundTint="#0000"
to ImageButton, it will make the background transparent and then you can set the image on it.
When you don't set the background property for the Imagebutton, the Imagebutton will use the default Imagebutton background of Android. And this background contains the padding so your image is smaller.
So when you set the background for Imagebutton by a color or resource that doesn't contain padding, your image will look larger
You can find the default background of the Imagebutton and another View in
..\Android\sdk\platforms\android-23\data\res\drawable\
(Some resource in this is not public)
For example
If you compile your project with API 23, the default background of ImageButton is btn_default_material
<ImageButton
...
android:background="#android:drawable/btn_default_material"
android:src="#drawable/ic_send_black_48dp"/>
<ImageButton
...
android:src="#drawable/ic_send_black_48dp"/>
So you set the android:background="#android:drawable/btn_default_material" or don't use background property, 2 ImageButton will look same (note: btn_default_material is not public for use)
Hope this help
If you don't want that ugly background in imagebutton,then you can use an imageview as button and set onClickListener on it. It will behave like a button.
This Link will be helpful to you.

Color/drawable changes are applied to all views with the same background (color) [Marshmallow]

I've recently updated my phone to Android Marshmallow and ran my existing app on it, but noticed a difference in color behavior: When applying changes to the background of a view (drawable), all views that share the same background (reference) will also the same changes applied. While previously, this was not the case.
Example
In this example, I have a two views with the same background color, and I want to change the alpha level of one of both views.
First we define the views in the layout:
<LinearLayout
android:id="#+id/test1"
android:orientation="horizontal"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/testColor2">
</LinearLayout>
<LinearLayout
android:id="#+id/test2"
android:orientation="horizontal"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/testColor1"
android:layout_marginLeft="5dp">
</LinearLayout>
Both views share the same background color or drawable:
<color name="testColor1">#3F51B5</color>
<color name="testColor2">#3F51B5</color>
The result looks like this:
Now we are going to change one of the two background, like this:
LinearLayout test1 = (LinearLayout) findViewById(R.id.test1);
LinearLayout test2 = (LinearLayout) findViewById(R.id.test2);
test1.getBackground().setAlpha(80);
Which results in this:
However, the desired and expected result is obviously this:
Download the sample project here.
A few thoughs:
When setting the Alpha level trough XML, this behavior does not apply.
It does not matter if both views refer to a different color definition in colors.xml (like in the example), refer to the same color definition of both have the same color (hex) directly in the view's xml file.
Question
How can I make changes to a view's background without this affecting other views that share the same background. Preferably while still being able to use a background that directly refers to a color defined in the color's xml file
Most likely the class of each view's background and constantstate are
the same object. It seems as if the two color resources have been
"merged" somewhere -- meaning they have shared ConstantState. Maybe in
the Resources class' caching? I would've expected them to stay
separate since they're different resources (albeit with the same color
value), but apparently not.
– Snild Dolkow
The ColorDrawable's state stores alpha, so any changes to one will change the others. To prevent this, you can first call mutate() on the drawable, separating the two drawables (by making a copy of the state).
In the example, this would result in using test1.getBackground().mutate().setAlpha(80); instead of directly applying the alpha.

about semi-transparent image function in android

just a question wondering when I was working on my app
let say I have image which I want to make it semi-transparent but I don't want to add about image into the app (as in create another one and add it in)
is there a way to do it??
You have to set the alpha value of the image. ( If i understood you correctly)
If you load this Image in to a ImageView, you can set it from xml like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_in_res"
android:alpha="1"/>
where according to the developers site "alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque). " Play with the value to get the desired effect.
If you have your image int the res/drawable folder, you can use it anywhere in your code. In xml almost all elements have the following parameter:
android:background="#drawable/im_your_image"
. You just have to set this in all places, where you want this to be your background, for the parent views background. (e.g. your MainActivitys RelativeLayout that you inflate in the onCreate funciton. You see the image is put only once in the package, but this way you can set it several times as a background.

The android:src property of ImageView doesn't support .9.png file?

I need to draw some divider line with some background image. I tried the ImageView control with the image as the value of android:src, but it didn't work. So I replaced the ImageView with a TextView with no text inside and specified the background image as its background. It indeed worked. Is there any better solution besides using a TextView?
Why not use the View Widget?
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#drawable/fixed_divider_horizontal_bright" >
</View>
NOTE: The dot you see right above this, is a 9-patch image (used in the code snippet). ;-)
i always just use a View and set the backgound.
I don't exactly understand your need. If you want use the ImageView as a divider, you can try this attribute android:scaleType="fitXY"

Categories

Resources