Search code examples
androidtextviewandroid-theme

Two textviews with different colors using one theme?


I'm new to android and I have been trying to find out an answer to this. I have a layout file .xml for listview that has two textviews, one is for heading with a greater size and other below is for description. What I want is that heading textview's text stays white and description textview's text stays grey in my current theme. That is dark theme. But user has the option to change the theme, so when user selects Light theme I want my heading textview's text (that is within layout that I have for listview's rows) to become black and also the description textview's text to become black. Please help and thanks in advance.


Solution

  • You can change theme programmatically. There is one guy asked this hours ago. Can you check this: how-to-change-colour-when-user-wants-another-colour-in-the-app

    Also if you want to use diferent themes for different TextViews you can try like :

    textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
    

    How can i create custom themes for TextViews

    in your styles.xml create different styles :

    <style name="MyBlueTextTheme" parent="@android:style/TextAppearance.Medium"> 
        <item name="android:textSize">18sp</item> 
        <item name="android:textColor">#123456</item> 
        <item name="android:textStyle">bold</item>
    </style>
    

    Then use it like :

    <TextView
       android:id="@+id/textBlue"
       android:text="This is a blue styled text" 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       style="@style/MyBlueTextTheme" />
    

    Or use it like: textView.setTextAppearance(context,R.style.MyBlueTextTheme);

    but its deprecated and you can use textView.setTextAppearance(R.style.MyBlueTextTheme); but this can be used Api23 or higher