matiszz
matiszz

Reputation: 616

Changing text with preferences

I'm trying to make an application which have some activities inside a FragmentPagerAdapter and I want to change the text depending on the preference selected.

preferencias.xml:

<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferencias_principal" >
    <ListPreference
        android:key="formato"
        android:title="Formato"
        android:summary="Escoja el formato de entrada de texto"
        android:entries="@array/formato"
        android:entryValues="@array/formatoValores"
        android:defaultValue="1" />
</PreferenceScreen>

arrays.xml:

<resources>
<string-array name="formato">
    <item>Format 1</item>
    <item>Format 2</item>
    <item>Format 3</item>
</string-array>

<string-array name="formatoValores">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>
</resources>

Myclass.java:

public class Myclass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.acruz, container, false);
    return rootView;
    }
}

And finally, the acruz.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
style="@style/Relative" >

     <TextView
        android:id="@+id/tv1"
        style="@style/TextoSmallCursiva"
        android:text="This is a text" />

 </RelativeLayout>

What I want to do is, for example, when I select the Format 1 item in arrays.xml, it changes the tv1text view to another text.

Thank you very much!

Upvotes: 0

Views: 39

Answers (1)

Christopher Francisco
Christopher Francisco

Reputation: 16288

I believe you have to set the text programatically:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.acruz, container, false);

    TextView tv1 = (TextView) rootView.findViewById(R.id.tv1);

    String selectedOption = // get your preference as string

    tv1.setText(selectedOption);

    return rootView;
    }
}

Upvotes: 1

Related Questions