Created a custom preference with a custom attribute.
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.example.android.sunshine.app;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.TypedArray;
|
||||||
|
import android.preference.EditTextPreference;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
public class LocationEditTextPreference extends EditTextPreference {
|
||||||
|
static final private int DEFAULT_MINIMUM_LOCATION_LENGTH = 2;
|
||||||
|
private int mMinLength;
|
||||||
|
|
||||||
|
public LocationEditTextPreference(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
TypedArray a = context.getTheme().obtainStyledAttributes(
|
||||||
|
attrs,
|
||||||
|
R.styleable.LocationEditTextPreference,
|
||||||
|
0, 0);
|
||||||
|
try {
|
||||||
|
mMinLength = a.getInteger(R.styleable.LocationEditTextPreference_minLength, DEFAULT_MINIMUM_LOCATION_LENGTH);
|
||||||
|
} finally {
|
||||||
|
a.recycle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
21
app/src/main/res/values/attrs.xml
Normal file
21
app/src/main/res/values/attrs.xml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright (C) 2015 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<resources>
|
||||||
|
<declare-styleable name="LocationEditTextPreference">
|
||||||
|
<attr name="minLength" format="integer" />
|
||||||
|
</declare-styleable>
|
||||||
|
</resources>
|
||||||
@@ -14,16 +14,19 @@
|
|||||||
See the License for the specific language governing permissions and
|
See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
-->
|
-->
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
<PreferenceScreen
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:custom="http://schemas.android.com/apk/res/com.example.android.sunshine.app"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<EditTextPreference
|
<com.example.android.sunshine.app.LocationEditTextPreference
|
||||||
android:title="@string/pref_location_label"
|
android:title="@string/pref_location_label"
|
||||||
android:key="@string/pref_location_key"
|
android:key="@string/pref_location_key"
|
||||||
android:defaultValue="@string/pref_location_default"
|
android:defaultValue="@string/pref_location_default"
|
||||||
android:inputType="text"
|
android:inputType="text"
|
||||||
android:singleLine="true" />
|
android:singleLine="true"
|
||||||
|
custom:minLength="3"/>
|
||||||
|
|
||||||
<ListPreference
|
<ListPreference
|
||||||
android:title="@string/pref_units_label"
|
android:title="@string/pref_units_label"
|
||||||
|
|||||||
Reference in New Issue
Block a user