0

I'm developing on Android (not comfortable yet) a SettingsActivity, I got the one on templates, did some tweaks, and it's working fine. But I want to add a button to the end. My xml is now like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="true"
        android:key="gravacao_baby_near"
        android:summaryOff="Quando o dispositivo estiver fora da zona segura, reproduzir aúdio gravado"
        android:summaryOn="Quando o dispositivo estiver fora da zona segura, reproduzir aúdio gravado"
        android:title="Habilitar reprodução da gravação" />

    <EditTextPreference
        android:capitalize="words"
        android:defaultValue="Informações de emergência"
        android:inputType="textMultiLine"
        android:key="texto_tag"
        android:maxLines="10"
        android:selectAllOnFocus="true"
        android:singleLine="false"
        android:title="Texto exibido por Tag" />

    <ListPreference
        android:defaultValue="10"
        android:entries="@array/settings_list_preference_titles"
        android:entryValues="@array/settings_list_preference_values"
        android:key="distancia_max_permitida"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null"
        android:title="Distância aproximada máxima permitida" />

</PreferenceScreen>

What I tried to do was adding after the last ListPreference the following code, but it says "It's not allowed here":

<Button
    android:id="@+id/gravarTag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/gravarTag"
    android:textAllCaps="false" />

How to do it the right way or the easy way?

2 Answers2

1

Add a parent LinearLayout and then add the preference screen, button children

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="match_parent"
android:width="match_parent>
<PreferenceScreen 

    <SwitchPreference
        android:defaultValue="true"
        android:key="gravacao_baby_near"
        android:summaryOff="Quando o dispositivo estiver fora da zona segura, reproduzir aúdio gravado"
        android:summaryOn="Quando o dispositivo estiver fora da zona segura, reproduzir aúdio gravado"
        android:title="Habilitar reprodução da gravação" />

    <EditTextPreference
        android:capitalize="words"
        android:defaultValue="Informações de emergência"
        android:inputType="textMultiLine"
        android:key="texto_tag"
        android:maxLines="10"
        android:selectAllOnFocus="true"
        android:singleLine="false"
        android:title="Texto exibido por Tag" />

    <ListPreference
        android:defaultValue="10"
        android:entries="@array/settings_list_preference_titles"
        android:entryValues="@array/settings_list_preference_values"
        android:key="distancia_max_permitida"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null"
        android:title="Distância aproximada máxima permitida" />

</PreferenceScreen>

<Button
    android:id="@+id/gravarTag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/gravarTag"
    android:textAllCaps="false" />
</LinearLayout>
Habibi
  • 56
  • 3
  • It's a nice getaway, but I'm having problem on my Activity now. `E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.LinearLayout"` – henrique.rivero Apr 09 '19 at 02:36
0

You can add button as preference in preference screen

<Preference android:title="@string/gravarTagBtn"
                android:key="gravarTag"
android:widgetLayout="@layout/btn_pref"
                android:summary="This is my button"/>

Then a custom button layout btn_pref.xml

 <?xml version="1.0" encoding="utf-8"?>
   <Button
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btn" 
        android:text="@string/ajustes_almacenamiento_bt" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


   </Button>

And in onCreate method

Preference button = findPreference("gravarTag");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {   
                    //code for what you want it to do   
                    return true;
                }
            });
Izhan Ali
  • 567
  • 7
  • 17