to speed up the development of an App I created this editText with a label attached.
This is the class:
public class EditTextWithLabel extends LinearLayout {
#InjectView(R.id.text_edittext_with_label)
protected TextView label;
#InjectView(R.id.edittext_edittext_with_label)
protected EditText editText;
public EditTextWithLabel(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
getAttributes(context, attrs);
}
...
private void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_edittext_with_label, this, true);
ButterKnife.inject(this);
setOrientation(VERTICAL);
}
private void getAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.EditTextWithLabel, 0, 0);
try {
CharSequence label = a.getText(R.styleable.EditTextWithLabel_label);
if (!TextUtils.isEmpty(label))
setLabel(label);
CharSequence text = a.getText(R.styleable.EditTextWithLabel_android_text);
if (!TextUtils.isEmpty(text))
setText(text);
CharSequence hint = a.getText(R.styleable.EditTextWithLabel_android_hint);
if (!TextUtils.isEmpty(hint))
setHint(hint);
int maxLength = a.getInt(R.styleable.EditTextWithLabel_android_maxLength, -1);
if (maxLength > 0)
setMaxLength(maxLength);
int type = a.getInt(R.styleable.EditTextWithLabel_android_inputType, InputType.TYPE_CLASS_TEXT);
setInputType(type);
} finally {
a.recycle();
}
}
...
}
And this is xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text_edittext_with_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/edit_text_radius"
android:paddingBottom="5dp"
android:text="#string/username"
android:textColor="#color/text"
android:textSize="#dimen/text_edit_text"
/>
<EditText
android:id="#+id/edittext_edittext_with_label"
style="#style/EditText"
android:inputType="textEmailAddress"/>
</merge>
I found out that if I put more of these in a fragment, when I restore it ALL the editTexts show the text that is wrote in the last one.
I cannot explain this behavior, so I hope that somebody could enlight me.
Thank you
EDIT
Thanks to J. Dow answer I was able to solve the issue, I've added at the end of the init method this code:
label.setId((int) System.currentTimeMillis());
editText.setId((int) System.currentTimeMillis());
This randomized the ids enough to avoid the issue.
What merge is doing is basically a simple include. So your final Layout will include multiple "copies" of your EditText.
From the Android documentation:
Note: In order for the Android system to restore the state of the views in our activity, each view must have a unique ID, supplied by the android:id attribute.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Thus, when restoring your merged layout, the Android system will encounter multiple EditTexts with the same id and therefore restore each of them with the same state.
Related
I've created a SettingsActivity with the template and put an EditTextPreference in my root_preferences.xml. It should contain a password, so I edited it like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorBackground">
<EditTextPreference
android:id="#+id/etPassword"
android:dialogTitle="Passwort"
android:inputType="textPassword"
android:key="pref_password"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Passwort" />
My problem is that neither inputType, singleLine nor setAllOnFocus is working. Do you know what's the problem?
In my case, InputType doesn't hide the password on input and also in the summary.
Here's how I got around the problem
XML file
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
app:key="pref_password"
app:title="Password"
app:dialogTitle="Set password"
app:useSimpleSummaryProvider="true"
/>
</PreferenceScreen>
In the PreferenceFragmentCompat set in your XML, find your EditTextPreference in the onCreatePreferences section and add an OnBindEditTextListener on it.
public static class YourFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Find the password EditText
EditTextPreference etpPassword = getPreferenceManager().findPreference("pref_password");
etpPassword.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
// Set keyboard layout and some behaviours of the field
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
// Replace -> android:singleLine="true"
// Not needed for password field, or set it before setTransformationMethod
// otherwise the password will not be hidden
//editText.setSingleLine(true);
// Replace -> android:inputType="textPassword"
// For hiding text
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
// Replace -> android:selectAllOnFocus="true"
// On password field, you cannot make a partial selection with .setSelection(start, stop)
editText.selectAll();
// Replace -> android:maxLength="99"
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(99)});
}
});
}
}
You also can create your own EditTextPreference class and set other things.
public class YourEditTextPreference extends EditTextPreference {
// Add some preferences, which can be used later for checking
private Integer mPasswordMinSize = 6;
public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public EditTextPreferencePassword(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EditTextPreferencePassword(Context context) {
super(context);
init();
}
private void init(){
// Set Dialog button text
this.setNegativeButtonText("RETURN");
this.setPositiveButtonText("CHECK");
this.setOnBindEditTextListener(new OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
// Put field parameters here
}
});
}
public void setMinSize(int minSize) { mPasswordMinSize = minSize; }
public Integer getMinSize(){ return mPasswordMinSize; }
// Hide password by stars
#Override
public CharSequence getSummary() {
return getText().equals("") ? super.getSummary() : "*******";
}
}
And in your XML, change <EditTextPreference to <complet.path.to.YourEditTextPreference
As pointed out in a previous answer, inputType is not understood by EditTextPreference and it seems it's not able to pass it to the underlying EditText object by itself, as one would expect.
Adding to the problem, there seems to be a lack of consistency depending on which libraries you are using. The accepted answer doesn't seem to work with AndroidX because there's no such getEditText() method in that EditTextPreference implementation. There's a way, though, by adding a listener that's exactly there for that (Kotlin example):
val myPref = findPreference<EditTextPreference>(
"my_pref_key"
)
myPref?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
}
Finally does the trick, but I have wasted a good hour trying to figure out a solution for this, mostly reading outdated questions and answers on the matter.
I see it's been awhile but setting this inside the SettingsFragment onViewCreated() worked for me...
val pw = preferenceScreen.preferenceManager.findPreference<EditTextPreference>("password")
pw?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_PASSWORD
}
You can't do it from XML, but EditTextpreference exposes the EditText so you can do it programmatically. After you load the preferences in your Activity/Fragment, you can do:
EditTextPreference pref = (EditTextPreference)
PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT); // set properties here
prefEditText.setSingleLine(true);
I'm using Android SharedPreferences API to build a settings screen.
I've one setting which I need a long text to explain the user its meaning (I would like to have something like main title and smaller subtitle,but i think it would require to much customization)
the settings.xml is like:
<EditTextPreference
android:defaultValue="15"
android:inputType="number"
android:icon="#drawable/ic_timer"
android:key="#string/pref_comment_interval"
android:persistent="true"
android:lines="2"
android:title="#string/time_between_comments" />
but even setting lines=2 and breaking the line with \n at time_between_comments the text is getting wrapped.
<string name="time_between_comments">Time between comments (in seconds)\nLower is faster</string>
like:
how can i make the text to break the line?
By default, the title of EditTextPreference is singleLine="true"
So we should custom it as below
public class MultiLineTitleEditTextPreference extends EditTextPreference {
public MultiLineTitleEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MultiLineTitleEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MultiLineTitleEditTextPreference(Context context) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView textView = (TextView) view.findViewById(your_package.R.id.title);
if (textView != null) {
textView.setSingleLine(false);
}
}
}
This doesn't work for all Preference types, but it works for EditTextPreference.
Add <![CDATA[ \n]]> around the first line of your title like in this example:
<string name="pref_title_special_with_note"><![CDATA[Special Title\n]]><small><i> ** followed by second line note</i></small></string>
This will display the second line in smaller text size and italic, but that's just decoration.
Is there a way to change how the detection of autoLink TextView finds phone numbers?
Thing is, it detects international format quite well, like +49123456789 but it fails on local formatted numbers like 0699777666555 (without a preceeding "+" character).
We need to have those numbers available too.
The TextView is set up with autoLink="all"
<TextView
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... some other settings ...
android:autoLink="all"
android:linksClickable="true"
android:textColorLink="#color/darkblue"
android:textColor="#color/black"/>
We have internal numbers (like 5532) and local phone numbers without any prefixes like 12345678. It would be great, if they can be highlighted too, without any, or at least without too much coding involved.
Any solutions to this?
Thanks in advance!
Try to do this programatically:
public class AutoLinkifyTextView extends TextView {
public AutoLinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoLinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setText(String text) {
super.setText(text);
parseLinks();
}
#Override
public void setText(int stringRes) {
super.setText(stringRes);
parseLinks();
}
private void parseLinks() {
Linkify.addLinks(this, Linkify.ALL);
}
}
and then use AutoLinkifyTextView instead of TextView
I have a settings activity, HomeActivity, that is loaded on startup. The launch mode is set to singleTask, due to certain requirements, In the layout of the HomeActivity there is an element with a icon, title and a switch. The idea is that you should be able to enable/disable Call, using the switch. And if you click the row (outside the switch) you are brought to a new activity, CallPreferences, where you can set Call specific settings. At the action bar of CallPreferences a switch should also be present where the user again can enable/disable call. Switches on both activites should reflect the "reality". That is, when the switch is changed, the value is stored to shared prefs. Both switches then read from shared prefs onCreate to set their value to on or off.
In the xml of HomeActivity I have a preference screen that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/lib/com.xxx.yy" >
<com.xxx.yy.preferences.IconSwitchPreference
foo:icon="#drawable/call_icn"
android:title="#string/call"
android:key="callIconSwitchPreference" >
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.xxx.yy.preferences.CallPreferences"
android:targetPackage="om.xxx.yy" />
</com.xxx.yy.preferences.IconSwitchPreference>
</PreferenceScreen>
IconSwitchPreference is a custom preference layout of mine containing a linear layout, a text view for the title, an image view and a switch:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a Preference in a PreferenceActivity. -->
<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">
<Switch
android:id="#+id/menu_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="onMenuSwitchClicked" />
</LinearLayout>
And the class that runs the code:
public class IconSwitchPreference extends IconPreference {
public IconSwitchPreference(Context context) {
this(context, null);
}
public IconSwitchPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon_switch);
if (attrs != null) {
int iconResId = attrs.getAttributeResourceValue(XMLNS, "icon", 0);
mIcon = context.getResources().getDrawable(iconResId);
mFilter = attrs.getAttributeValue(XMLNS, "filter");
mUrl = attrs.getAttributeValue(XMLNS, "url");
}
}
}
In CallPreferences I programmatically create the switch and add it to the action bar:
private void createActionBarSwitch() {
ActionBar actionBar = getActionBar();
Switch actionBarSwitch = new Switch(this);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL
| Gravity.RIGHT));
actionBarSwitch.setChecked(isSwitchOn());
}
This works and I can set the switch to the stored value, and the switch is updated to reflect the value.
In HomeActivity however the switch is not updated to reflect the value.
The following does not work:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.preference_icon_switch, null);
TextView tv = (TextView)view.findViewById(android.R.id.title);
tv.setText("Test");
Switch menuSwitch = (Switch)view.findViewById(R.id.menu_switch);
menuSwitch.setChecked(sharedPrefs.isCallhandlingEnabled());
}
Neither is the text view changed to test, nor is the switch enabled (default value is false).
The following however works:
final IconSwitchPreference ic = (IconSwitchPreference) findPreference("callIconSwitchPreference");
ic.setTitle("Test");
The title is set to "Test". The only problem is that I don't have any reference to the switch, so I can update it's value. Can IconSwitchPreference.java be updated to extract and store a reference to the switch in its used xml?
I've tried a number of solutions and code samples; but all of them have something that is not working. Another solution would be to use a standard SwitchPreference, but it doesn't differ between a click on the switch itself to change its state (without going to the new activity) and a click on the row to enter the activity (without changing the switch value).
Solved the issue by adding the following tag to the HomeActivity xml:
foo:switchPref="#string/call_enabled_pref_key"
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/lib/com.xxx.yy" >
<com.xxx.yy.preferences.IconSwitchPreference
foo:icon="#drawable/call_icn"
android:title="#string/call"
android:key="callIconSwitchPreference"
foo:switchPref="#string/call_enabled_pref_key" >
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.xxx.yy.preferences.CallPreferences"
android:targetPackage="om.xxx.yy" />
</com.xxx.yy.preferences.IconSwitchPreference>
</PreferenceScreen>
And then in IconSwitchPreference.java I extract the res id:
mSwitchPrefResId = attrs.getAttributeResourceValue(XMLNS, "switchPref", 0);
public IconSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon_switch);
if (attrs != null) {
int iconResId = attrs.getAttributeResourceValue(XMLNS, "icon", 0);
mIcon = context.getResources().getDrawable(iconResId);
mFilter = attrs.getAttributeValue(XMLNS, "filter");
mUrl = attrs.getAttributeValue(XMLNS, "url");
mSwitchPrefResId = attrs.getAttributeResourceValue(XMLNS, "switchPref", 0);
}
}
}
onBindView in the same class (IconSwitchPreference.java) I got a reference to the Switch:
#Override
public void onBindView(View view) {
super.onBindView(view);
mSwitch = (Switch) view.findViewById(R.id.menu_switch);
updateSwitch();
}
Then I used mSwitchPrefResId to fetch the shared prefs (to know if the switch is enabled or not) and set mSwitch to the corresponding value.
I'm facing a problem where I know the root cause but don't see a way to fix it. If a custom compound component is used multiple times in an activity, the values saved from views will overwrite each other. To explain it easier I made the following example.
The xml for the new component, only an EditText to make it shorter.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<EditText
android:id="#+id/custom_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
</EditText>
</merge>
The class implementing the new behavior, only inflating the layout.
public class CustomView extends LinearLayout {
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_view, this, true);
}
}
And a layout using 2 of them.
<?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" >
<test.customview.CustomView
android:id="#+id/customView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</test.customview.CustomView>
<test.customview.CustomView
android:id="#+id/customView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</test.customview.CustomView>
</LinearLayout>
When the screen is rotated, the value from second View is also restored in the first one.
Digging into the framework's code I found out that Parcelable objects returned from onSaveInstanceState defined in View class are put in a SparseArray with the key object's id. Because I'm including CustomView multiple times the EditText with id "custom_text" is also getting added multiple times. Having the same id, values saved will overwrite each other.
I'm looking for any suggestion on how this should be actually implemented. Right now, I don't see any way to change those identifiers.
Seems like I have some solution with this problem. I try to find it for some time.
1.First you must create inner class which extends BaseSavedState, inside your CustomView.
CustomView{
String value; //some text value from edittext
EditText edittext;
...
private static class Save extends BaseSavedState{
String savedValue;
public Save(Parcel incoming) {
super(incoming);
savedValue = incoming.readString();
Log.i("Save", "Parcel");
}
public Save(Parcelable parcelable) {
super(parcelable);
Log.i("Save", "Parcelable");
}
#Override
public void writeToParcel(Parcel outcoming, int flags) {
super.writeToParcel(outcoming, flags);
outcoming.writeString(savedValue );
Log.i("Save", "writeToParcel");
}
public static final Parcelable.Creator<Save> CREATOR =
new Creator<CustomView.Save>() {
#Override
public Save[] newArray(int size) {
Log.i("Parcelable.Creator<Save>", "newArray");
return new Save[size];
}
#Override
public Save createFromParcel(Parcel incoming) {
Log.i("Parcelable.Creator<Save>", "createFromParcel");
return new Save(incoming);
}
};
}
}
2.then override this two methods in CustomView
CustomView{
String value; //some text value from edittext
EditText edittext;
...
#Override
protected Parcelable onSaveInstanceState() {
Log.i("CustomView", "onSaveInstanceState");
Parcelable p = super.onSaveInstanceState();
Save save = new Save(p);
save.savedValue = value; // value is from CustomView class
return save;
}
#Override
protected void onRestoreInstanceState(Parcelable state) {
Log.i("CustomView", "onRestoreInstanceState");
if(!(state instanceof Save)){
super.onRestoreInstanceState(state);
return;
}
Save save = (Save) state;
value = save.savedValue;
//setting in this place value to edittext will not do anything.
//instead, you have to do this in step 3
super.onRestoreInstanceState(save.getSuperState());
}
...
}
3.override onAttachedToWindow() and set to edittext "value".
CustomView{
String value; //some text value from edittext
EditText edittext;
...
#Override
protected void onAttachedToWindow() {
edittext.setText(value);
super.onAttachedToWindow();
}
...
}
and now you can have multiple instances of your Custom View 's that are resistant to change orientation - they will have the correct values.I have not tested this solution in 100% but it seems to be good.