2

I have this xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/FragmentSecondary" >
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true"/>

<RelativeLyout>

My style his is style="@style/FragmentSecondary", want to change it programmatically to style=@style/FragmentTrains. I don't want to rewrite another xml. i need to write it in the code. How can I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
haythem souissi
  • 3,263
  • 7
  • 50
  • 77

1 Answers1

0

I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a template"
        style="@style/my_style" />

then inflate this to instantiate your new TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

Hope this helps.

Also refer this LINK & set style from code

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64