FreeRDP
ButtonPreference.java
1 /*
2  Custom preference item showing a button on the right side
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.utils;
12 
13 import android.content.Context;
14 import android.preference.Preference;
15 import android.util.AttributeSet;
16 import android.view.View;
17 import android.view.View.OnClickListener;
18 import android.view.ViewGroup;
19 import android.widget.Button;
20 import android.widget.LinearLayout;
21 
22 import com.freerdp.freerdpcore.R;
23 
24 public class ButtonPreference extends Preference
25 {
26 
27  private OnClickListener buttonOnClickListener;
28  private String buttonText;
29  private Button button;
30 
31  public ButtonPreference(Context context)
32  {
33  super(context);
34  init();
35  }
36 
37  public ButtonPreference(Context context, AttributeSet attrs)
38  {
39  super(context, attrs);
40  init();
41  }
42 
43  public ButtonPreference(Context context, AttributeSet attrs, int defStyle)
44  {
45  super(context, attrs, defStyle);
46  init();
47  }
48 
49  private void init()
50  {
51  setLayoutResource(R.layout.button_preference);
52  button = null;
53  buttonText = null;
54  buttonOnClickListener = null;
55  }
56 
57  @Override public View getView(View convertView, ViewGroup parent)
58  {
59  View v = super.getView(convertView, parent);
60  button = (Button)v.findViewById(R.id.preference_button);
61  if (buttonText != null)
62  button.setText(buttonText);
63  if (buttonOnClickListener != null)
64  button.setOnClickListener(buttonOnClickListener);
65 
66  // additional init for ICS - make widget frame visible
67  // refer to
68  // http://stackoverflow.com/questions/8762984/custom-preference-broken-in-honeycomb-ics
69  LinearLayout widgetFrameView = ((LinearLayout)v.findViewById(android.R.id.widget_frame));
70  widgetFrameView.setVisibility(View.VISIBLE);
71 
72  return v;
73  }
74 
75  public void setButtonText(int resId)
76  {
77  buttonText = getContext().getResources().getString(resId);
78  if (button != null)
79  button.setText(buttonText);
80  }
81 
82  public void setButtonText(String text)
83  {
84  buttonText = text;
85  if (button != null)
86  button.setText(text);
87  }
88 
89  public void setButtonOnClickListener(OnClickListener listener)
90  {
91  if (button != null)
92  button.setOnClickListener(listener);
93  else
94  buttonOnClickListener = listener;
95  }
96 }