0

On my PreferenceScreen, I would like a return button. I found many links about how create a button in a PreferenceScreen. I tried to implement the solution which consists to import a layout in a Preference tag, but the implementation of setOnPreferenceClickListener in my code doesn't work, when I click there's no effect. I used a fragment inside the Preference tag.

PreferenceScreen :

<Preference
    android:key="fragment_return_key"
    android:layout="@layout/fragment_return"
    ></Preference>

Fragment:

    <RelativeLayout android:id="@+id/lytopnavreturn"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp">
    <ImageButton
        android:id="@+id/btnfragnavreturn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:src="@drawable/return_32"
        android:background="@android:color/transparent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"/>
    <TextView
        android:id="@+id/txtfragnavreturnsettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_settings"
        android:textSize="18dp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>   

Activity OnCreate code:

Preference button2 = (Preference)findPreference("fragment_return_key");
    button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            onBackPressed();
            return true;
        }
    });

The fragment displays, but no event launch on click.

Thanks.

Anthony
  • 73
  • 1
  • 2
  • 6
  • Need a clarification.. Do you need this "back button" just to add some processing when user clicks it? Or are you just trying to place a back button, because unless you have some specific reason, this is not needed..user can just press the phone back button to get to the previous activity – SoulRayder Nov 12 '16 at 18:52
  • Also would like to add that in your code, you are not returning to the previous activity, but you are launching a new instance of your previous activity, and it might lead to some faulty behaviour. – SoulRayder Nov 12 '16 at 18:53
  • @SoulRayder : You're right, I changed for onBackPressed(). But the event onPreferenceClick is never called, that's my problem, I know you can press the back button on the phone, but I want this button too. – Anthony Nov 12 '16 at 21:57

0 Answers0