I use standart EditTextPreference but on different Android versions it looks differently:
API 7
API 10
How can I achieve good view? Maybe resize keyboard or something else?
This helped me solve the problem:
https://stackoverflow.com/a/8481314/1752613
public class MyEditTextPreference extends EditTextPreference
{
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
Dialog dialog = getDialog();
if(dialog != null) {
Window window = dialog.getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
}
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 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 have created a custom dialog that is used from a preference screen. Everything works fine except one thing: I want to switch the visibility of the Cancel button based on the status of an internal check.
Normally you have onPrepareDialog and onCreateDialog and you can do this in onCreateDialog. But here we have onPrepareDialogBuilder... so where is onCreateDialogBuilder? Where can I do something like
builder.setNegativeButton(null, null);
after onPrepareDialogBuilder? I cannot do it IN onPrepareDialogBuilder since I need the Cancel button in case the internal check fails.
Can you please help me to get into the right direction?
public UnlockPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_enter_registration);
}
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setTitle(R.string.label_enter_registration);
}
// would need something like
#Override
protected void onCreateDialogBuilder(AlertDialog.Builder builder) {
super.onCreateDialogBuilder(builder);
if (internalCheckOk())
builder.setNegativeButton(null, null);
else
builder.setNegativeButton(..., ...);
}
I want to implement an about box in my wallpaper. For that, I want a window to show up which contains the information when a user clicks on it.
None of the available controls like checkbox, listview etc. can be used for this......
I also tried to make a Dialog Pref by following advice from some other questions I looked at but ended up with a dialog that just does not seem appropriate for an about box.
My DialogPref:
public class DialogPref extends DialogPreference {
public DialogPref(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
}
So can you help me implement a popup window for an about box?