Add a Button into PrefrenceScreen : Android

非 Y 不嫁゛ 提交于 2019-12-03 20:32:35

This thread is stale, but since there is no solution yet, here is how I succeeded:

1. Set the button click listener

There are two ways to set the click listener for a button, either programmatically or via XML.

Programmatically: In your custom preference, override onBindView and attach the listener there:

@Override
protected void onBindView(View view) {
    View button = view.findViewById(R.id.myButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d(LOG_TAG, "button click listener works!");
        }
    });
    super.onBindView(view);
}

Via XML: Add android:onClick="yourMethodName" to the layout definition of the Button element (which is part of the layout for your preference, see below) and implement the method in your PreferenceActivityas described here: Responding to Click Events

2. Fix OnPreferenceClickListener

Now the button click listener should work, but the Preference's OnPreferenceClickListener will not work anymore. In order to fix this, add

android:descendantFocusability="blocksDescendants"

to the top element in the layout file for your preference:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

        <Button android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />
</LinearLayout>

(No android:onClick="yourMethodName" here as I chose to do it programmatically.)

3. Attach the Layout

Finally you need to set this layout to your preference XML file: Add android:layout="@layout/myLayoutName" or alternatively set it via setLayoutResource as you construct the preference in your PreferenceFragment.

NitZRobotKoder

Implement OnPreferenceClickListener in preferenceActivity

private Preference mBuyPreference = null;
mLogoutPreference = findPreference(yourkey);
mBuyPreference.setOnPreferenceClickListener(this);

@Override
public boolean onPreferenceClick(Preference preference) {
if (preference == mBuyPreference) {

    try {
        //Do Something
    }

    return false;
}

Instead of using the onClick() handler, override the onPreferenceTreeClick method in your PreferenceActivity. There you will get the clicked preference object and you can check its key.

public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

 if (preference.getKey().equals("key")) {           
       // do something
       return true;
 }

 return false;
}
Sudarshan Bhat

I've solved half of your problem by creating a custom preference like this -

In order to get to this, I created a custom preference like this -

public class CustomPreference extends Preference {

private LinearLayout mWidgetContainer;
private View mRowView;

public CustomPreference(Context context) {
    super(context);
}

public CustomPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected View onCreateView(ViewGroup parent) {
    LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);

    mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);

    Button button = new Button(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    button.setLayoutParams(params);
    button.setBackgroundResource(R.drawable.listview_row_bg);
    button.setTextSize(14);
    button.setPadding(5, 5, 5, 5);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), BuyScreenActivity.class);
            getContext().startActivity(intent);
        }
    });
    button.setTypeface(null, Typeface.BOLD);
    button.setText("Buy now");
    mWidgetContainer.addView(button);

    return mRowView;
}
}

Then in your settings xml file you can create a setting like

<package.of.your.java.file.CustomPreference 
    android:key="type" 
    android:title="Type"
    android:summary="" />

And my preferences_row_view.xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="50dip"
    android:paddingLeft="5dip"
    android:paddingRight="?android:attr/scrollbarSize" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:lineSpacingMultiplier="1.3"
            android:textSize="14sp" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textSize="13sp" />
    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal" />

</LinearLayout>

That will do.

But there is one more issue I'm facing which I've posted here on SO.

Add a button in Preference Row

Instead of

 Button btn = (Button) findViewById(R.id.buyItNowBtn);

do this

  Button btn = (Button)typePref.getView(view, viewGroup );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!