3

I have read about applying style for whole application in AndroidManifest but I didn't find how to apply style within Linear Layout.

For example I want to change: size, padding and text allignment for all of these TextViews:

 <LinearLayout
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="30"
        style="@style/ItemTransacationHeader">
        <!--android:theme="@style/ItemTransacationHeader">-->


        <TextView
            android:text="Client"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView8"
            android:layout_weight="10"
            />

        <TextView
            android:text="Quantity"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView9"
            android:layout_weight="10"
            />

        <TextView
            android:text="Product Value"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView12"
            android:layout_weight="10"
            />


    </LinearLayout>

so I created style

<style name="ItemTransacationHeader" parent="android:Theme">
    <item name="android:textSize">10dp</item>
    <item name="android:textAlignment">textStart</item>
    <item name="android:padding">4dp</item>
</style>

and I have tried in various way to apply that style to LinearLayout

style="@style/ItemTransacationHeader"
android:theme="@style/ItemTransacationHeader"
app:theme="@style/ItemTransacationHeader"

but I haven't any idea is this even possible(I suppose yes it is) and how can I achieve that

Rafał
  • 572
  • 4
  • 21

3 Answers3

2

Create a style for your TextView (you've done this step, renamed for clarity)

<style name="AppTextStyle" parent="android:Theme">
    <item name="android:textSize">10dp</item>
    <item name="android:textAlignment">textStart</item>
    <item name="android:padding">4dp</item>
</style>

Define it as your TextView's style in your application's theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    ...
    <item name="android:textViewStyle">@style/AppTextStyle</item> 
</style>

All of your TextViews in Activities with that theme should now use AppTextStyle.

See existing question: Setting global styles for Views in Android

Community
  • 1
  • 1
Tom
  • 6,946
  • 2
  • 47
  • 63
2
<LinearLayout
    android:orientation="horizontal"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="30">

    <TextView
        android:text="Client"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView8"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

    <TextView
        android:text="Quantity"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView9"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

    <TextView
        android:text="Product Value"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView12"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

</LinearLayout>
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
0

You need to set @style for every TextView individually.

CrowsNet
  • 93
  • 5