I have a couple of custom DialogPreference implementations floating around, such as this one:
package apt.tutorial;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
public static int getHour(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[0]));
}
public static int getMinute(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
#Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());
return(picker);
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}
lastHour=getHour(time);
lastMinute=getMinute(time);
}
}
They work just fine. However, in an application with android:targetSdkVersion="11" defined, on a XOOM, they show up missing the indent when in the PreferenceActivity:
Also, the font size appears a smidge bigger, at least for the title.
There's nothing in DialogPreference where I am really overriding any formatting behavior for that stuff, AFAIK. The preference XML is unremarkable, other than referring to the above class:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="sort_order"
android:title="Sort Order"
android:summary="Choose the order the list uses"
android:entries="#array/sort_names"
android:entryValues="#array/sort_clauses"
android:dialogTitle="Choose a sort order" />
<CheckBoxPreference
android:key="alarm"
android:title="Sound a Lunch Alarm"
android:summary="Check if you want to know when it is time for lunch" />
<apt.tutorial.TimePreference
android:key="alarm_time"
android:title="Lunch Alarm Time"
android:defaultValue="12:00"
android:summary="Set your desired time for the lunch alarm"
android:dependency="alarm" />
<CheckBoxPreference
android:key="use_notification"
android:title="Use a Notification"
android:defaultValue="true"
android:summary="Check if you want a status bar icon at lunchtime, or uncheck for a full-screen notice"
android:dependency="alarm" />
</PreferenceScreen>
Anyone know where I'm going wrong?
Thanks!
UPDATE
Here is a link to a project that contains this custom preference and a simple preference XML file that demonstrates the problem. Even with just two Java classes, the preference XML, and an arrays.xml file, I get this phenomenon. Here is a compiled APK from this project.
(cross-posting from the associated android-developers thread)
OK, I figured it out.
There are three possible constructors on a Preference:
MyPreference(Context ctxt)
MyPreference(Context ctxt, AttributeSet attrs)
MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
Somewhere along the line, I picked up the pattern of having the
one-parameter constructor chain to the two-parameter constructor
(passing null for the 2nd parameter), and having the two-parameter
constructor chain to the three-parameter constructor (passing 0 for
the 3rd parameter).
And that's not the right answer.
I am hoping that the right answer is to only implement the second
constructor, because the correct default style is internal to Android
(com.android.internal.R.attr.dialogPreferenceStyle). The second
constructor is the one used with inflating preference XML.
Thanks to all for the help!
You can dance with void Preference.setWidgetLayoutResource(int widgetLayoutResId) method, although I prefer to override View Preference.onCreateView(ViewGroup parent) method in my custom Preference class and hack it by adding custom views just below #android:id/summary (use hierarchyviewer utility for details).
The complete method is:
#Override
protected View onCreateView(ViewGroup parent)
{
View ret = super.onCreateView(parent);
View summary = ret.findViewById(android.R.id.summary);
if (summary != null)
{
ViewParent summaryParent = summary.getParent();
if (summaryParent instanceof ViewGroup)
{
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup summaryParent2 = (ViewGroup) summaryParent;
layoutInflater.inflate(R.layout.seek_bar_preference, summaryParent2);
seekBar = (SeekBar) summaryParent2.findViewById(R.id.seekBar);
seekBar.setMax(maxValue - minValue);
seekBar.setOnSeekBarChangeListener(this);
statusText = (TextView) summaryParent2.findViewById(R.id.seekBarPrefValue);
unitsRightView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsRight);
unitsLeftView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsLeft);
}
}
return ret;
}
Source code of my SeekBarPreference class based on code from http://robobunny.com can be downloaded here
I tried your code on the emulator. There is no problem with the code that you have given, and all the lines have the same formatting; but they all look more similar (in format) to the third preference (Lunch Alarm Time) than the others.
It looks like the other three preferences are getting indented more than required. So, maybe you have some global formatting style that is used, but not picked up by the TimePreference preference.
EDIT: OK. So, the above is not (completely) true. There is definitely a problem when I tried with the target sdk set to HoneyComb. But on setting the theme for the PreferenceActivity class as android:theme="#android:style/Theme.Black", there is a consistency in the look of all the preferences as shown below.
This style looks similar to Froyo, but not the HoneyComb; in the latter, the title font is smaller and there is more indentation. Probably, the default theme is not being assigned to Custom Preferences - just a guess :) A workaround would be to assign the default theme to your preference activity explicitly, but I don't know what the default theme in HoneyComb is (and whether it can be set).
The solution which helped me:
I have replaced
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
with
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android"));
}
As you can see, I replaced third argument 0 with ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android") in the second constructor of custom preference class.
To make the accepted answer more clear. You only need this constructor:
public TimePreference(Context ctxt, AttributeSet attrs) {
// this(ctxt, attrs, 0); // wrong
super(ctxt, attrs);
}
Related
I am trying to customize the Google Cast SDK's Cast Dialog (shown when you tap the cast button and shows the list of available devices), but i haven't found a way to do it.
Currently, it just shows an AlertDialog with a list of the available devices to connect.
What i want to do instead, is open an Activity that will show the list of devices with my own UI implementation.
This is the dialog i am trying to modify:
I've found many customization aspects about this SDK, but nothing related to this dialog.
So i figured out a way to achieve this,
First i created a class that overrides MediaRouteActionProvider (which is the main class that controls that button's functionality)
public class CustomMediaRouteActionProvider extends androidx.mediarouter.app.MediaRouteActionProvider {
public CustomMediaRouteActionProvider(Context context) {
super(context);
}
#Override
public MediaRouteButton onCreateMediaRouteButton() {
return new CastButton(getContext());
}
}
Then you're gonna need to override the button's functionality with your own, in my case i open a new activity.
public class CastButton extends MediaRouteButton {
public CastButton(Context context) {
this(context, null);
}
public CastButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.mediaRouteButtonStyle);
}
public CastButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean performClick() {
Intent i = new Intent(getContext(), RemoteDevicesActivity.class);
getContext().startActivity(i);
return true;
}
}
Finally, you need to modify your xml that contains this button (i assume that you already implemented this part)
Change the field app:actionProviderClass with your custom class (in this case CustomMediaRouteActionProvider) and you're done.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/connect_to"
android:id="#+id/cast"
app:actionProviderClass="CustomMediaRouteActionProvider"
app:showAsAction="ifRoom" />
</menu>
Are you have more details of final result of this? I need to do something similar but I don't get it how did you achieve it
Background
I have an App with multiple custom TextViews in it.
These four Custom TextViews are all set up like this:
public class TextView_Light extends android.support.v7.widget.AppCompatTextView {
public TextView_Light(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TextView_Light(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextView_Light(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(tf ,1);
}}
I do have TextView_Bold, TextView_Light, TextView_Regular and TextView_Thin. Each of them uses another font which is saved in assets/fonts
I use the TextViews like that:
<de.mayr.wap.app.helper.TextView_Light
android:id="#+id/textView_Light7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/overviewViaBarcode"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
When the App is started and a view shows up for the first time everything looks great, like that:
The Problem
In this view the user klicks on one of the tablerows, an Dialog appears where the user types in some data. On save the onResume method of the view is called, which calls an Webservice etc. like that:
#Override
public void onResume() {
super.onResume();
callSerNrWS();
}
public void callSerNrWS() {
if (CheckNetwork.isNetworkAvailable(getContext())) {
final AsyncResponseFortschritt<ResponseObject<PruefauftraegeVO>> response = new AsyncResponseFortschritt<ResponseObject<PruefauftraegeVO>>() {
#Override
public void processFinishSerNr(ResponseObject<PruefauftraegeVO> output) {
anw = createPruefAnwArraylist();
seriennummerVO = findDataToSerNr();
adapt = new PruefAnwAdapter(getContext(), anw, seriennummerVO);
pruefListView.setAdapter(adapt);
setVisibilityLoadAnimation(View.GONE);
if (output.getException() != null && !output.getException().isEmpty())
AlertCreator.makeFailAlert(R.string.titelKeinFortschritt, R.string.textKeinFortschritt,getContext());
}
#Override
public void processStartSerNr() {
setVisibilityLoadAnimation(View.VISIBLE);
}
};
AsyncCallSerNrWS ws = new AsyncCallSerNrWS(response, pruefauftraegeVO);
ws.execute();
}
}
And after that, theres in 90% of cases one or more TextView which changed its looking from eg. TextView_Light to TextView_Thin. Like in the following picture: The appearence of the 'Anzugstrom [A]' in the third row now looks like a TextView_Thin and the '9/48' in the fifth row looks like a TextView_Light
What I know
This doesnt happen when the view is shown for the first time
There isn't any system behind it. It's totally random.
It happens in activitys and fragments
It happens in every view i have whatever the TextView contains 'hard-coded' text or text that comes from an Webservice
Its not just in lists, also in 'normal' views
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.
I am using autocomplete textview with arraylist value [aaa0,aaa1,...aaa100,aab0,aab1,...,aab100], I enter value 'aa' and scroll down till next 10 value ie.'aa10'and then i enter value 'aab' the suggestion list starts from 10th value i.e 'aab10'.
I have to scroll up to see the first value i.e 'aab0'.I want first value of suggestion list to appear when i change the search text.suggestion list screenshot
You need to extend the AutoCompleteTextView class
and override the onFilterComplete method
where you have to dismiss and show the drop down again to refresh the list view.
Also I would suggest instead of extending AutoCompleteTextView
extend the android.support.v7.widget.AppCompatAutoCompleteTextView
for obviously better support
here's the code
package your.package.name;
import android.content.Context;
import android.util.AttributeSet;
import android.support.v7.widget.*;
public class CustomAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//show and dismiss the drop down to refresh the list in it
this.dismissDropDown();
this.showDropDown();
}
}
EDIT
Also you can use the AutoCompleteTextView.setListSelection() method to scroll to the top.
Instead of dismissing and showing the DropDown on every filter
which is more efficient.
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//set list selection to 0
if(getListSelection() != 0)
this.setListSelection(0);
}
When a user want to paste text in MultiAutoCompleteTextView I want to overwrite it.
Means whatever he copies it must paste in my MultiAutoCompleteTextView as "Java is rocket".
I may achieve it from previous question here but I don't know how to hook the class MonitoringEditText to my MultiAutoCompleteTextView.
Can I achieve it or it is impossible.
You can do this using the same concept provided in the code from the link you provided. Extend the MultiAutoCompleteTextView and override the onTextContextMenuItem method.
Something along the lines of:
public class MonitoringMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
public MonitoringMultiAutoCompleteTextView(Context context) {
super(context);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//based on http://stackoverflow.com/a/14981376/1566836
#Override
public boolean onTextContextMenuItem(int id) {
// Do your thing:
boolean consumed = super.onTextContextMenuItem(id);
// React:
if (android.R.id.paste == id) {
setText("Java is rocket");
}
return consumed;
}
}
Then change your MultiAutoCompleteTextView in your layout file to whatever.your.full.package.is.MonitoringMultiAutoCompleteTextView.
After that, any attempt to paste into your MultiAutoCompleteTextView will result in the text being changed to "Java is rocket"