Reputation: 2487
I'm new to android development. I have created a PreferenceFragment. there is a button(Preference) bottom of the page.after adding the custom layout to the button, the button is not clickable. But outside the button(within preference is clickable) please help me to solve this issue
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="@string/header_user_detail"
android:title="YOUR DETAIL">
<EditTextPreference
android:key="user_name"
android:title="@string/user_name"></EditTextPreference>
<com.empite.oneonus.helpers.custom_preferences.DatePreference
android:key="dob"
android:title="@string/date_of_birth"></com.empite.oneonus.helpers.custom_preferences.DatePreference>
<EditTextPreference
android:key="work_postcode"
android:title="@string/work_postcode"></EditTextPreference>
<EditTextPreference
android:key="home_postcode"
android:title="@string/home_postcode"></EditTextPreference>
<ListPreference
android:dialogTitle="Application language"
android:key="work_category"
android:summary="Select the Application language"
android:title="Language"></ListPreference>
<Preference
android:key="update_profile_key"
android:layout="@layout/update_profile_btn"></Preference>
<Preference
android:key="button"
android:summary="This will act like a button"
android:title="Acts like a button" />
</PreferenceCategory>
<PreferenceCategory
android:key="@string/header_user_account"
android:title="YOUR ACCOUNT">
<EditTextPreference
android:key="edit_email"
android:layout="@layout/edit_email_address"></EditTextPreference>
<EditTextPreference
android:key="edit_password"
android:layout="@layout/edit_password"></EditTextPreference>
</PreferenceCategory>
layout/update_profile_btn
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/update_profile_btn"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:background="@color/btn_light_green_color"
android:focusable="false"
android:text="Update Profile"
android:textAllCaps="false"
android:textColor="@color/white"></Button>
public class UpdateProfilePrefFragment extends com.github.machinarius.preferencefragment.PreferenceFragment implements Preference.OnPreferenceClickListener {
protected static void setListPreferenceData(ListPreference lp) {
CharSequence[] entries = {"English", "French"};
CharSequence[] entryValues = {"1", "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.update_profile);
final ListPreference listPreference = (ListPreference) findPreference("work_category");
setListPreferenceData(listPreference);
listPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
setListPreferenceData(listPreference);
return false;
}
});
Preference pref = findPreference("update_profile_key");
pref.setOnPreferenceClickListener(this);
Preference button = (Preference) findPreference("button");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
Toast.makeText(getActivity(), "button", Toast.LENGTH_SHORT).show();
return true;
}
});
}
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
return false;
}
}
Upvotes: 0
Views: 1125
Reputation: 2783
Your button
custom preference does not specify a layout. You may need to specify a layout and set android:focusable="false"
. See Custom preference not clickable
Edit: For the @layout/update_profile_btn
file, you may try the following modifications:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false">
<Button
android:id="@+id/update_profile_btn"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:background="@color/btn_light_green_color"
android:onClick="onButtonClick"
android:text="Update Profile"
android:textAllCaps="false"
android:textColor="@color/white"></Button>
</RelativeLayout>
and define the method
void onButtonClick(View v) {
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
}
Upvotes: 1