I created button in Preferences in this way:
<PreferenceScreen>
<Preference
android:key="resetBD"
android:title="#string/ajustes_almacenamiento"
android:summary="#string/ajustes_almacenamiento_desc"
android:widgetLayout="#layout/pref_reset_bd_button" >
</Preference>
</PreferenceScreen>
layout/pref_reset_bd_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/submit_layout_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="10">
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/resetButton"
android:text="Reset"
android:layout_width="wrap_content"
android:onClick="submitWifiToDevice"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
In PreferenceFragment I successfuly access/get the button doing this:
View footerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pref_reset_bd_button, null, false);
resetButton= (Button) footerView.findViewById(R.id.resetButton);
But when I try to change button text
resetButton.setText("NEW ONE");
(or disable) nothing happening. What am I doing wrong?
Try this. Create a custom Preference:
public class PreferenceWidget extends Preference {
private View mView;
#Override
protected void onBindView(View view) {
super.onBindView(view);
mView = view.findViewById(R.id.resetButton);
}
public View getView() {
return mView;
}
}
Then change your preference xml
<PreferenceScreen>
<package.PreferenceWidget
android:key="resetBD"
android:title="#string/ajustes_almacenamiento"
android:summary="#string/ajustes_almacenamiento_desc"
android:widgetLayout="#layout/pref_reset_bd_button" >
</package.PreferenceWidget >
</PreferenceScreen>
Then to get the view
PreferenceWidget p = (PreferenceWidget)findPreference("resetBD");
Button b = (Button)p.getView();
b.setText("Hello");
Original answer:
You shouldn't have to inflate the View yourself. This should happen in your call to addPreferencesFromResource.
Then you can get the preference, get the preference's view, find the button view, then set the text.
Preference p = findPreference("resetBD");
View v = p.getView(null, null);
Button b = (Button)v.findViewById(R.id.resetButton);
v.setText("Hello");
Although, since no one seems to know how to use the Preference.getView function you may have to create a custom Preference object.
Related
In my application Settings Layout i need custom preference for profile Category. I searched and done something .
pref.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Profil">
<Preference
android:key="custom"
android:title="My Custom Preferenece"
android:layout="#layout/custom">
</Preference>
<Preference
android:icon="#drawable/transparency"
android:key="email"
android:selectable="false"
android:summary=""
android:title="Email"
/>
custom.xml (i used for preference layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/custom_image"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="#drawable/no_photo"
android:textColor="#color/black"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/custom_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Custom Text"
android:textColor="#color/black"
android:gravity="center"
/>
</LinearLayout>
I want to reach and set android:id="#+id/custom_image" or android:id="#+id/custom_text" , i didn't reach these items from my fragment.
How can i reach these items from fragment ?
Thank you
My fragment
public class GeneralPreferenceFragmentN extends PreferenceFragment {
private Context mContext;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_main);
mContext = getActivity().getApplicationContext();
bindPreferenceSummaryToValue(findPreference("phone"));
private static void bindPreferenceSummaryToValue(Preference preference)
{
preference.setOnPreferenceChangeListener(
sBindPreferenceSummaryToValueListener);
SharedPreferences prefs = preference.getSharedPreferences();
Object value;
if (preference instanceof MultiSelectListPreference) {
value = prefs.getStringSet(preference.getKey(), new HashSet<String>
());
} else {
value = prefs.getString(preference.getKey(), "");
}
..................
Try this:
in your onCreate() initialize the view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_abschliessen_einsatz, container, false);
//#INFO: Create a global View v element if you want to use the view in other funtions (for example on slected and so on). Otherwise just use
this.v = v;
//#INFO: Otherwise just use th View elemnt to to find by ID
myCustomImage = (AppCompatImageView) v.findViewById(R.id.custom_image);
myCustomText = (AppCompatTextView) v.findViewById(R.id.custom_text);
//Set Text or do stuff with the elements afterwards
return v;
}
//Use the view in other fuctions, ex. onSelcted
#Override
public void onSelected() {
//update UI when selected
//#INFO: Use the global View elemnt to to find by ID
myCustomImage = (AppCompatImageView) v.findViewById(R.id.custom_image);
myCustomText = (AppCompatTextView) v.findViewById(R.id.custom_text);
//Set Text or do stuff with the elements afterwards
}
Android Studio 0.5.2
Hello,
I have a preference screen that that has a custom preference layout, as I wanted to add a seekbar for a
sensitivity selection in the settings.
However, I can't seem to find away to get the view id (mSbrSensitivity - seekbar) to set the ChangeListener as its null.
I don't want to use setContentView(R.layout.custom_prefs) as this will display the UI, and when I make the call to addPreferenceFromResource(R.xml.prefs) it will display the layout again as in preference screen I have this set to my custom_prefs layout.
public class SettingsActivity extends PreferenceActivity {
private SeekBar mSbrSensitivity;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.custom_prefs, null);
addPreferencesFromResource(R.xml.prefs);
/* Get views */
mSbrSensitivity = (SeekBar)findViewById(R.id.sbSensitivity); // Returns NULL
/* Set listeners */
mSbrSensitivity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/* More stuff here */
}
My preference screen:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="custom_prefs_key">
<Preference
android:key="SensorKey"
android:title="Sensitivity"
android:summary="Adjust how sentivitive you are"
android:layout="#layout/custom_prefs"> <!-- Load custom prefs layout -->
</Preference>
</PreferenceScreen>
And my custom preference layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="#+id/sbSensitivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/tvDisplay"/>
<TextView
android:id="#+id/tvDisplay"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sbSensitivity">
</ListView>
</RelativeLayout>
Many thanks for any suggestions,
I need to create a custom ListPreference dialog so that I can add some header text (a TextView) above the List (ListView).
I've created MyListPreference class that extends ListPreference and overrides onCreateDialogView():
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = (View) inflater.inflate(R.layout.dialog_preference_list, null);
return v;
}
My XML layout dialog_preference_list.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</LinearLayout>
Problem: The TextView is displayed below the ListView instead of above. I need the TextView to be above. I've tried both with LinearLayout and RelativeLayout (using "below" or "above" attributes) with no success: I can't find a way to put the TextView above the ListView... The layout is pretty simple and I cannot see why the list stays above...
Also, note that the problem occurs on both a real device (Nexus 4, Android 4.2.2) and the emulator. However, when looking at the layout rendered in Eclipse's graphical layout, the layout is correct! See both attached pictures.
Any idea on how to solve this?
Layout rendered on the device (incorrect):
Layout rendered on Eclipse (correct):
Edit with solution 10.07.2013
As suggested by the accepted answer, the problem comes from the use of builder.setSingleChoiceItems() in ListPreference's onPrepareDialogBuilder().
I've fixed it by extending ListPreference and overriding onCreateDialogView() to build the Dialog without the builder so that I can create a custom View showing the header text above the list items.
GPListPreference.java:
public class GPListPreference extends ListPreference {
...
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
builder.setNegativeButton(null, null);
builder.setPositiveButton(null, null);
}
private int getValueIndex() {
return findIndexOfValue(getValue());
}
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView) inflater.inflate(R.layout.dialog_preference_list, null);
TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
header.setText(getDialogMessage()); // you should set the header text as android:dialogMessage in the preference XML
lv.addHeaderView(header);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.dialog_preference_list_singlechoice, getEntries());
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(getValueIndex() + 1, true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setValueIndex(position - 1);
getDialog().dismiss();
}
});
return lv;
}
}
dialog_preference_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
dialog_preference_list_singlechoice.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="2dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
dialog_preference_list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
I think the problem is with the way ListPreference works. ListPreference uses Builder.setSingleChoiceItems() to create the rows with the RadioButtons, and it has preference over the custom layout you are trying to add (in your case a TextView and a ListView inside a LinearLayout. The solution is extending DialogPreference instead. Here is a link to a GitHub where I created a custom DialogPreference that does what you need. I haven't coded the RadioButton logic.
I guess it's a theming issue. Try changing the theme of your dialog inside the constructor make it something like setStyle(STYLE_NO_TITLE, R.style.AppTheme). Your base app theme with no_title style.
If this is not the issue than it might be related with the ListPreference class itself. It might be overriding your layout for consistency in theming the preference views. However, I have not used ListPreference before, so its just a guess.
Can you reproduce the same result by playing with the themes in XML graphical layout preview?
Another option you can try is to add the TextView as a header to the ListView like this:
TextView textView = new TextView(getActivity());
ListView listView = new ListView(getActivity());
listView.addHeaderView(textView);
The addHeaderView takes a View so you theoretically have anything you want to be the header, but I have only used a TextView.
The link above is broken. On this solution the idea is overriding the ListPreference, and inflating your own listview, with the data defined on the ListPreference.
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = new ListView(getContext());
// Inflate the view into the header only if a message was set
if (getDialogMessage() != null && ! getDialogMessage().equals("") ) {
TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
header.setText(getDialogMessage());
lv.addHeaderView(header, null, false);
}
// Create a new adapter and a list view and feed it with the ListPreference entries
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
R.layout.custom_dialog_single_choice_list_adapter, getEntries());
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(getValueIndex() + 1, true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setValueIndex(position - 1);
getDialog().dismiss();
}
});
return lv;
}
Another important thing is to call onPrepareDialogBuilder and not calling super in it. This will avoid that the listview appears twice.
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// Not calling super, to avoid having 2 listviews
// Set the positive button as null
builder.setPositiveButton(null, null);
}
private int getValueIndex() {
return findIndexOfValue(getValue());
}
Where dialog_preference_list_header is in my case only a TestView, but it could be a more complex view, and custom_dialog_single_choice_list_adapter could be something like this:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="2dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
I have such piece of code :)
And i want to edit text property of my custom preference layout.
But any changes made on object from getView function does not affect actual list in preference screen. any ideas? I know that i cant extend PreferenceScreen, and i cant use any other type of preference in this case, i only want to be able to edit my custom textview from my layout in code.
PreferenceScreen settings = getPreferenceManager().createPreferenceScreen(this);
settings.setLayoutResource(R.layout.mypreference);
View test = (View) settings.getView(null,getListView());
TextView text = (TextView)test.findViewById(R.id.text);
text.setText("BLA BLA");
We should be using getView(convertView, parent) but from countless unanswered questions on StackOverflow, it's obvious no one knows how to use it. So our only option is to create a custom Preference:
Create a new Class:
public class CustomPreference extends Preference {
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onBindView(View rootView) {
super.onBindView(rootView);
View myView = rootView.findViewById(R.id.myViewId);
// do something with myView
}
}
In your xml/preferences.xml, add your custom preference:
<your.package.name.CustomPreference
android:key="custom_preference"
android:layout="#layout/custom_layout" />
Using the custom layout below:
custom_preferences_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/preview_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/notification_preview" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Using the file above with your user preferences xml file, you can do this to access the items in your custom layout:
public class CustomPreferenceActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
addPreferencesFromResource(R.xml.custom_preferences);
setContentView(R.layout.custom_preferences_layout);
}
private void initCustomizePreferences(){
//Button
Button button = (Button)findViewById(R.id.preview_button);
//Do something with the button.
}
}
I hope this helps :)
You need to override onBindView in your custom Preference class.
More details -
Get view of preference with custom layout
I never actually messed with the views of a PreferenceActivity, but here's something I do (usually for ListPreferences)
ListPreference listPref;
CheckBoxPreference cbPref;
// Set the summary as the current listpref value
listPref = (ListPreference) findPreference("list_pref_key");
listPref.setSummary(listPref.getEntry());
// change the title / summary / ??? of any preference
findPreference("anothe_pref_key").setTitle("New Title");
findPreference("anothe_pref_key").setSummary("New subtext for the view");
This way, even if the layout used by PreferenceActivity / PreferenceFragment change in a future version, you wont have to change anything in your code.
I have a preference screen that is populated with items from a database. I have this working by creating my own PreferenceActivity. In the activity I create DialogPreference items and add them to my PreferenceCategory To style to preference item on the screen I use a custom layout and apply it using setLayoutResource(R.layout.custom_pref_row)
This basically adds an ImageButton to the view aligned to the right of the layout. This all works fine and my preference screen shows the custom view with the button. My question is how do I attach a click listener to the button in the custom view? I was not able to find a way to get at View for the row from the PreferenceActivity. If my items were not created dynamically I might be able to do this all from XML and then reference the id or the button, but I can do that because I am creating the list dynamically.
Any suggestions on how to get a handle on the ImageButton for each item? In the end I want to configure the button to launch a delete confirmation dialog.
R.layout.custom_pref_row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="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_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
<ImageButton android:id="#+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="#null"></ImageButton>
</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="vertical" />
</LinearLayout>
Related part of my PreferenceActivity:
DialogPreference diaPref;
for (Station mStation : sList) {
diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation);
diaPref.setLayoutResource(R.layout.custom_pref_row);
diaPref.setTitle(mStation.getName());
diaPref.setKey(STATION_PREFIX + mStation.getId());
// add new preference
stationTypesCategory.addPreference(diaPref);
}
You can extend DialogPreference and override the onBindDialogView(View view). Inside this method you can do:
#Override
protected void onBindDialogView(View view) {
((ImageButton) view.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
super.onBindDialogView(view);
}
Your sublcass of DialogPreference can hold any state/value related to the item it represents.
Take a look at this question about general guidelines to extend DialogPreference.
Hope this helps!
OK, Chopin got me thinking in a different direction. I did not realize that the Preference object is also responsible for how its selector appears in a Preference screen.
The setLayoutResouce() function sets the resource for the Dialog itself not the row seen in a Preference screen. This was confusing and I was incorrectly trying to use this in the preference screen to adjust the selector layout there.
The solution is to override onCreateView and return a custom layout there. To me this is counterintuitive because that method usually controls the final view in most other situations.
I alraedy subclassed my Preference (DialogPreference) so all I had to do was add the following...
#Override
protected View onCreateView (ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customRow = inflater.inflate(R.layout.preferences_station_list_row, null);
((ImageButton) customRow.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("c","clicked");
}
});
customRow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(null);
}
});
customRow.setClickable(true);
return customRow;
}
One problem I ran into was that at first the row itself was no longer clickable but the button was. I had to add a listener on the whole view and manually call ShowDialog(). The only thing missing now is that when clicked from the Preference screen the item no longer shows a highlight. Any idea what styles I should apply so the list shows the highlight like it normally does?