Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a button in Preference Row
    text
    copied!<p>I want a button in my Settings screen. This exact question has been asked <a href="https://stackoverflow.com/questions/10514005/add-a-button-into-prefrencescreen-android">here</a>. But unfortunately has no answers. :( </p> <p><img src="https://i.stack.imgur.com/4qKrf.png" alt="enter image description here"></p> <p>In order to get to this, I created a custom preference like this - </p> <pre><code> 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; } } </code></pre> <p>This does work. But it behaves strange. As you can see on click of that button I'm taking the user to Activity called <code>BuyScreenActivity</code>. The strange part is when I press back on <code>BuyScreenActivity</code>, I come back to my Settings screen but <code>onDestroy</code> and <code>onStop</code> of <code>BuyScreenActivity</code> is not called at all. Why would it behave that way? </p> <p>If I scroll down the settings screen, <code>onStop</code> &amp; <code>onDestroy</code> will then be called. Why does this have to behave that way? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload