I have created a custom EditTextPreference:
<EditTextPreference
android:key="alert_planed_edittext_preference"
android:title="#string/alert_planed_edittext_preference"
android:summary="#string/alert_planed_summary_edittext_preference"
android:dialogTitle="#string/alert_planed_dialog_title_edittext_preference"
android:numeric="integer"
android:layout="#layout/edit_text_preference_layout" />
the custom layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/alert_planed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:text="5" >
</TextView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="6dip" >
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<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="2"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
I just added the text view with id alert_planed.
In my preference activity i am trying to get the new EditTextPreference value and put it in my text view alert_planed.
I am getting the EditTextPreference value, that's ok. But when i set the text i got a null exception telling me that the TextView is null.
Here is my Preference code:
addPreferencesFromResource(R.xml.preferences);
mTextViewAlertPlanned = (TextView) findViewById(R.id.alert_planed);
findPreference("alert_planed_edittext_preference").setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
#Override
public boolean onPreferenceChange(
android.preference.Preference arg0, Object value) {
if(value != null)
{
Log.i("Preference","String.valueOf(value) ="+String.valueOf(value));
Log.i("Preference","mTextViewAlertPlanned ="+mTextViewAlertPlanned);
int myVal = Integer.parseInt(String.valueOf(value));
mTextViewAlertPlanned.setText(myVal);
}
return true;
}
});
How can i get my TextView correctly (the TextView is a part of the custom EditTextPreference layout)?
Simple Inflate you xml
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.custome_layout, null);
mTextViewAlertPlanned = (TextView) view.findViewById(R.id.alert_planed);
I had the same issue has you, and what worked for me was to create a class extending from EditTextPreference:
class CustomEditTextPreference constructor(
context: Context,
attrs: AttributeSet) : EditTextPreference(context, attrs) {
lateinit var tvTitle: TextView
lateinit var summary: TextView
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
with(holder.itemView) {
// Here would go your ids
tvTitle = findViewById(R.id.tv_title)
summary = findViewById(R.id.tv_summary)
}
}
// Whenever you want to change the 'summary' you call this function and pass the value as parameter
fun changeSummary(value: String){
summary.text = value
}
}
In the preferenceFragment, instead of doing:
preference.summary = newValue.toString()
you just call the function:
changeSummary(newValue.toString())
Hope that works!
Related
I have a volume screen that I want the user to be able to inflate from anywhere in the app.
class VolumeScreen #JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
private var audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
fun inflate(context: Context) {
LayoutInflater
.from(context)
.inflate(R.layout.screen_volume, this, true)
initUI()
}
fun hide() {
this.visibility = View.GONE
}
private fun initUI() {
this.visibility = View.VISIBLE
TTSUtils.getInstance(context).speakText(resources.getString(R.string.volume_help))
val audioStream = AudioManager.STREAM_SYSTEM
var currentVolume = audioManager.getStreamVolume(audioStream)
val maxVolume = audioManager.getStreamMaxVolume(audioStream)
volumeLevelText.text = resources.getString(R.string.volume_percentage, currentVolume.times(10).toString())
increaseButton.setOnClickListener {
if (currentVolume < 10) {
currentVolume += 1
}
audioManager.setStreamVolume(audioStream, currentVolume, 0)
volumeLevelText.text = resources.getString(R.string.volume_percentage, currentVolume.times(10).toString())
}
decreaseButton.setOnClickListener {
if (currentVolume > 0) {
currentVolume -= 1
}
audioManager.setStreamVolume(audioStream, currentVolume, 0)
volumeLevelText.text = resources.getString(R.string.volume_percentage, currentVolume.times(10).toString())
}
volumeContinueButton.setOnClickListener {
this.hide()
}
}
}
.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout android:layout_width="768px"
android:layout_height="840px"
android:background="#color/main_background">
<TextView
android:text="#string/volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="78px"
android:textAppearance="#style/ScreenTitle"/>
<TextView
android:text="#string/adjust_volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="220px"
android:textAppearance="#style/ScreenSubtitle"/>
<TextView
android:text="#string/volume_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="350px"
android:textAppearance="#style/Amount"
android:id="#+id/volumeLevelText"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginBottom="171px"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<ImageView
android:src="#drawable/connector_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
<Button android:layout_width="348px"
android:layout_height="104px"
android:text="#string/decrease_volume"
android:gravity="left|center_vertical"
android:textAppearance="#style/SelectionButtonText"
android:background="#drawable/button_background_coral"
android:drawableRight="#drawable/ic_remove_81dp"
android:paddingRight="29px"
android:paddingLeft="30px"
android:id="#+id/decreaseButton"
/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginBottom="171px"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<Button android:layout_width="348px"
android:layout_height="104px"
android:text="#string/increase_volume"
android:gravity="left|center_vertical"
android:textAppearance="#style/SelectionButtonText"
android:background="#drawable/button_background_coral"
android:drawableRight="#drawable/ic_add_81px"
android:paddingRight="29px"
android:paddingLeft="30px"
android:id="#+id/increaseButton"
/>
<ImageView
android:src="#drawable/connector_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginBottom="31px"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<Button android:layout_width="348px"
android:layout_height="104px"
android:text="#string/cont"
android:textAppearance="#style/SelectionButtonText"
android:background="#drawable/button_background_olive"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="#+id/volumeContinueButton"
/>
<ImageView
android:src="#drawable/connector_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</LinearLayout>
In activity's .xml:
<group.flowbird.indygotvm.views.VolumeScreen
android:id="#+id/volumeScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80px"
android:layout_marginBottom="104px"
android:elevation="1px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</group.flowbird.indygotvm.views.VolumeScreen>
The first time inflate() is called, the views are updated according to the code inside of initUI(). But if it is re-inflated after hide() is called, none of that stuff happens, even though initUI() is called again. How do I make sure the view is being re-rendered or assigned its onClickListener() each time inflate() is called on it?
This view never is inflated because the init method do nothing. Try to use the init Kotlin constructor to inflate the view and init the UI:
init {
LayoutInflater
.from(context)
.inflate(R.layout.screen_volume, this, true)
initUI()
}
With the previous code, when the activity calls the setContentView method, the views are inflated including your custom view.
Use the constructor context in the inflate() method removing the context param. But if you donĀ“t need to inflate this view programatically, then is not necessary this method.
i have a customAdapter class and a listView. i made the single_row layout with 3 textviews and one imageButton.
now my question is how can i get the DB id field, only from clicking on the imageButton and ignore clicks on the entire row?
I know i can get the id with:
mListView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
view.delete.setOnClickListener(){
Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show()
}
but this is working only if i click the entire row.
I tried android:descendantFocusability="blocksDescendants" in the root of the xml and android:focusable="false" in the imageButton node, and the imageButton is not recognized anyway while the entire row is recognized. only if i using button.onClickListener()...
the click is recognized. but that way i don't know how to get the id.
my adapter class code:
class MyAdapter(activity: Activity, context: Context, cursor: Cursor) : CursorAdapter(context, cursor, true) {
var inflater: LayoutInflater
lateinit var mListView: ListView
init {
this.inflater = LayoutInflater.from(context)
mListView = activity.listView
}
override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.single_row, parent, false)
return view;
}
override fun bindView(view: View, context: Context, cursor: Cursor) {
val latitude = view.findViewById(R.id.latitude) as TextView
val longtitude = view.findViewById(R.id.longtitude) as TextView
val date = view.findViewById(R.id.date) as TextView
val asu = view.findViewById(R.id.asu) as TextView
latitude.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.LATITUDE)))
longtitude.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.LONGTITUDE)))
date.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.DATE)))
asu.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.ASU)))
}
}
my single_row layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:id="#+id/list_item"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/latitude"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:text="latitude"
android:gravity="center" />
<TextView
android:id="#+id/longtitude"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:text="longtitude"
android:gravity="center" />
<TextView
android:id="#+id/date"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:gravity="center"
android:text="date" />
<TextView
android:id="#+id/asu"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:gravity="center"
android:focusable="false"
android:text="asu"/>
<ImageButton
android:id="#+id/delete"
android:layout_width="0.23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:focusable="false"
android:src="#android:drawable/ic_menu_delete" />
</LinearLayout>
You are using cursor adapter, you should use ArrayAdapter. Here is why custom-cursoradapter-and-why-not-use
and example of ArrayAdapter
I need to change the image of an ImageView placed inside a custom preference. I am doing this:
View thumb1 = findViewById(R.id.thumb_1);
ViewGroup vg = (ViewGroup) thumb1.getParent();
final Preference myPref = (Preference) findPreference("test02");
((ImageView) myPref.getView(thumb1, vg)).setImageResource(R.drawable.ic_chooser);
I get a null error. What am I doing wrong?
perference.getView(null,null).findViewById(R.id.xx);
Well, I did in this way in Kotlin.
This sample has custom layout for Preference and update it in any function.
Preference XML:
<Preference
android:key="preference_key"
android:layout="#layout/custom_layout_name"
android:summary="Summary"
android:title="Title" />
custom_layout_name.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preference_margin_top_bottom"
android:layout_marginLeft="#dimen/preference_margin_right"
android:layout_marginTop="#dimen/preference_margin_top_bottom"
android:orientation="vertical">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black" />
<TextView
android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
<View
android:id="#+id/colorView"
android:layout_width="#dimen/preference_color_size"
android:layout_height="#dimen/preference_color_size"
android:layout_alignParentEnd="true"
android:layout_marginRight="#dimen/preference_margin_top_bottom"
android:background="#color/colorPrimaryDark" />
</RelativeLayout>
Class:
private var preferenceColor: Preference? = null
preferenceColor = preferenceManager.findPreference("preference_key)
preferenceColor!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
anyFunction(Color.BLUE)
true
}
Function for updating the View:
private fun anyFunction(color: Int) {
val colorViewOrigin = findViewById<View>(R.id.colorView)
val colorViewParent: ViewGroup = colorViewOrigin.parent as ViewGroup
val preferenceManager = preferenceColor?.getPreferenceManager()
val colorView = preferenceManager?.findPreference("preference_key")!!.getView(colorViewOrigin, colorViewParent) as View
colorView.setBackgroundColor(color)
}
Hope it would be help you.
I want to create a class named TabView which extends RelativeLayout and it is inflated from xml that contains RelativeLayout. The thing is that it doesn't seem to work as I expected. Did I do something wrong in the constructor? I know layoutInflater.inflate returns View object but what do I have to make it equal to? Any advice is apprecited!
private class TabView extends RelativeLayout {
private int mIndex;
public TabView(Context context) {
super(context, null, R.attr.vpiTabPageIndicatorStyle);
LayoutInflater layoutInflater = LayoutInflater.from(context);
layoutInflater.inflate(R.layout.tabview_title_subtitle, null);
}
public int getIndex() {
return mIndex;
}
public void setTitle() {
TextView title = (TextView)findViewById(R.id.title);
title.setText("Followers");
}
public void setSubitle() {
TextView subtitle = (TextView)findViewById(R.id.subtitle);
subtitle.setText("435");
}
}
The following is tabview_title_subtitle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/title"
android:text="Subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
First, your layout should look like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/title"
android:text="Subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
</merge>
In other way you'll end up with RelativeLayout that contains another RelativeLayout
Second. Inflate it like this:
layoutInflater.inflate(R.layout.tabview_title_subtitle, this);
You can see the following blog for creating and using custom views in android.
And probably using
inflater.inflate(R.layout.view_color_options, this, true);
instead of
layoutInflater.inflate(R.layout.tabview_title_subtitle, null);
is what you need to do.
Use:
layoutInflater.inflate(R.layout.tabview_title_subtitle, this);
to actually add the inflated view to TabView(right now you just inflate it and immediately discard it). Also if you want to use your custom view in a xml layout then also implement the constructor that takes a Context and an AttributeSet.
Hi i want to add a button into prefrencescreen, i got success into add a button into the prefrence but i could not get onClick event. i have attached my code a pic of prefence screen
setting.xml
<PreferenceCategory android:title="Application Details">
<Preference android:key="type"
android:title="Type"
android:summary="" />
</PreferenceCategory>
<PreferenceCategory android:title="Notification Settings">
<ListPreference android:key="sendNotificationType"
android:title="Status Notification For"
android:dialogTitle="Status Notification For" />
</PreferenceCategory>
settingdetail.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textView2"
android:layout_marginLeft="15dp"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="#+id/setFromTimeBtn"
android:text="Summary"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/buyItNowBtn"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:background="#drawable/button"
android:text="#string/buyItNowBtnTxt"
android:textColor="#color/white" />
and the onCreate Method of prefenceActivity Class
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.setting);
setContentView(R.layout.settingview);
Preference typePref = (Preference) findPreference("type");
typePref.setLayoutResource(R.layout.settingdetail);
typePref.setSelectable(true);
Button btn = (Button) findViewById(R.id.buyItNowBtn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG,"TEST");
Toast.makeText(Setting.this, "TEST", Toast.LENGTH_SHORT).show();
}
});
}
screenshot
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.
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;
}
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 );