I need to display number keyboard in EditText Preference which is androidx.
proxy.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:dialogTitle="edittext_dialogtitle_proxy_port"
android:key="edittext_key_proxy_port"
android:inputType="numberPassword"
android:digits="0123456789."
android:summary="Summary"
android:title="Port"/>
</PreferenceScreen>
MainActivity:
public class MainActivity extends PreferenceFragmentCompat {
EditTextPreference mPortField;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
addPreferencesFromResource(R.xml.proxy);
mPortField = (EditTextPreference) findPreference("edittext_key_proxy_port");
mPortField.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
}
});
}
I am not getting number keypad
[![enter image description here][1]][1]
according to this doc android:inputType is applicable only for EditText, not EditTextPreference (which isn't extending EditText, DialogPreference instead). but you already figured out how to get EditText hidden in EditTextPreference, so maybe just set digit limitation programmatically inside onBindEditText with below line
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
(more info HERE)
Related
I have following code
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
app:key="pref_password"
app:title="Password"
app:iconSpaceReserved="false"
app:dialogTitle="Password"
android:inputType="textPassword"/>
</androidx.preference.PreferenceScreen>
But the edit text field is not masked with dots even with android:inputType="textPassword"
I am using androidx. Anyone please help
Update
I tried following as a commenter suggested, but no luck
<EditTextPreference
android:key="pref_password"
android:title="Password"
app:iconSpaceReserved="false"
android:dialogTitle="Password"
android:inputType="textPassword"/>
Setting attributes directly on the EditTextPreference doesn't work with the AndroidX library - since an EditTextPreference isnt' an EditText, and shouldn't work as such. Instead you should use an OnBindEditTextListener to customize the EditText when it is displayed. (need androidx.preference:preference v1.1.0 and higher)
See the settings guide for more information
edit with code:
Java:
EditTextPreference preference = findPreference("pref_password");
if (preference!= null) {
preference.setOnBindEditTextListener(
new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
});
}
Kotlin:
val editTextPreference: EditTextPreference? = findPreference("pref_password")
editTextPreference?.setOnBindEditTextListener {editText ->
editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
}
I would like to crate a settings Activity with items with Title and subtitle.
I've checked a lot in documentation and even other posts here and people say it's not possible... android default settings page has a lot of it:
sorry for the picture in portuguese, but what is written there doesn't matter, the point is the settings page built on android has plenty of titile/subtitle items (and it happens in many other screens)
I would like to achieve it using android default Preference api. is that possible?
Yes it is possible. One fast solution is the following (that might be improvied and tuned according your needs):
public class SettingsActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.settings_fragment, new PrefsFragment()).commit();
}
}
public static class PrefsFragment extends PreferenceFragment {
public PrefsFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
activity_settings.xml
<?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="match_parent">
<fragment
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.vb.myapplication.SettingsActivity$PrefsFragment" />
</LinearLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="keyCategory"
android:title="titleCategory">
<CheckBoxPreference
android:key="keyCheckPreference"
android:title="titlePreference"
android:summary="summaryPreference"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>
I have one main activity with multiple Fragments. From the menu, the user can go to a new activity which contains the PreferenceFragment:
Preference Activity and Fragment:
public class SettingsActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_screen);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_options);
}
}
}
Preferences xml file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/pref_cat_display">
<ListPreference
android:key="pref_key_display_units"
android:title="#string/pref_title_units"
android:summary="#string/pref_summary_units"
android:entries="#array/entries_list_units"
android:entryValues="#array/entryvalues_list_units"
android:dialogTitle="#string/pref_dialog_title_list_units"
android:defaultValue="0"/>
</PreferenceCategory>
</PreferenceScreen>
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/settings_content"
android:paddingTop="?android:attr/actionBarSize">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.cs.biodyapp.usl.activity.SettingsActivity$Prefs1Fragment"
android:id="#+id/fragment" />
</LinearLayout>
Once the option has been initialized, I try to access to this preference from the original Activity but I always get an empty Map in the SharedPreferences object.
I tried many ways that supposely let you access preferences form a different activity or directly from the file but no chance:
//SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
SharedPreferences sharedPref = getActivity().getApplication().getSharedPreferences("pref_options", Context.MODE_PRIVATE);
int units = sharedPref.getInt("pref_key_display_units", 0);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
Is the correct line to use ( getting the application context and not the base context ).
I also detected a problem in using ListPreference with integer arrays in a PreferenceFragment. Finally I had to use string arrays and sharedPrefs.getString() to then cast it to an integer.
i have a xml layout main.xml like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#000"
android:textSize="16dp"
android:id="#+id/edtxt"
android:gravity="center|top|left"/>
</RelativeLayout>
and i declare this in my activity like this
public class MyActivity extends Activity {
private TextView txt = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt= (TextView)findViewById(R.id.edtxt);
}
}
but i have no problem in my activity. why?
see this link http://developer.android.com/reference/android/widget/EditText.html
and look here
you will find your answer
That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.
Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html
And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Answer is simple, it is so because EditText extends TextView.
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.