2

The following code shows how you could prepare a custom style for all TextView widgets.

<style name="CustomTheme.TextView" parent="android:Widget.TextView">
    <item name="android:background">@color/viewBackground</item>
    <item name="android:textColor">@color/viewTextColor</item>
    <item name="android:textSize">@denim/viewTextSize</item>
</style>

How do I fix the following custom style for all ImageView widgets? I'm asking since I can't seem to find any such Widget.

<style name="CustomTheme.ImageView" parent="android:Widget.???">-->
</style>

The only Image related Widgets, in the list, are ImageButton and ImageWell.

goldenmaza
  • 149
  • 1
  • 10
  • `ImageView` doesn't have a defined style attribute, so you can't really do that with it as is. As you've noticed, `ImageButton` does, though, so if you can use those instead, that would be the easiest solution. – Mike M. Jan 23 '19 at 18:48
  • @MikeM. : I just tried just using "android:Widget" as the parent, to the style, and it seem to affect the ImageView in question. – goldenmaza Jan 24 '19 at 07:53
  • Maybe I'm not understanding what you're asking, exactly. How are you setting that style on the `ImageView`s? – Mike M. Jan 24 '19 at 07:56
  • @MikeM. : With the style attribute: style="@style/CustomTheme.ImageView.Large" – goldenmaza Jan 24 '19 at 08:38
  • Ah, OK, I thought you were asking how to style all of your `ImageView`s through a single attribute in your theme, like with `textViewStyle`. In that case, you don't need a `parent` style. Just remove it. – Mike M. Jan 24 '19 at 08:45
  • @MikeM. : I wanted that first yes, but since there is no attribute like that then I had to take this backwards solution by adding style attributes everywhere. I would have prefered an "imageViewStyle", yes. – goldenmaza Jan 24 '19 at 08:54

1 Answers1

2

you can define custom style like this

<style name="iconStyle" >
        <item name="android:contentDescription">""</item>
        <item name="android:clickable">true</item>
        <item name="android:focusable">true</item>
        <item name="android:padding">@dimen/size_12dp</item>
        <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>

then pass this style to your image view like this

<ImageView
    style="@style/iconStyle"
    android:layout_width="@dimen/size_48dp"
    android:layout_height="@dimen/size_48dp"
    android:src="@drawable/ic_search"
    android:id="@+id/imgSearch"/>
  • If you are not able to find `style.xml` file then, just add this `style="@style/iconStyle"` line and then click on `Error Alert toggle` and create new `style` for `day/night` then Android studio automatically create this file for your project. You can access and use. – TexD Jan 14 '23 at 10:14