android.support.v7.preference.EditTextPreference how to set keyboard to numeric - java

android:inputType is not working on EdidtTextPreference from support library. Is changing the input type even possible with this preference? I want a preference with a numeric keyboard. More specifically I will want to set a custom Keyboard, but I can't seem to even open the default one with this preference. If it is not possible with EditTextPreference, can someone give me an idea how I would go about doing what I desire?
<android.support.v7.preference.EditTextPreference
android:key="key"
android:title="title"
android:maxLines="1"
android:dialogMessage="title"
android:inputType="number" />

Actually, the solution is quite easy. Since android:inputType="number" didn't seem to work, I went with this:
PreferenceFragment.java
#Override
public void onResume() {
EditTextPreference preference = (EditTextPreference) findPreference("key");
EditText et = preference.getEditText();
if (et != null) {
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//in fact, you can do whatever you can normally with an EditText here
}
}
Although I'm not sure if this is how it works with the latest androidx Preference.

Related

I want to change the icon color when selecting in library StickySwitch

I want to change the color of the icon upon selection, I found that the library does not exist in its properties
Just what I want to change the icon color to white when selected
<io.ghyeok.stickyswitch.widget.StickySwitch
android:id="#+id/sticky_switch"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:layout_marginHorizontal="20dp"
app:ss_animationDuration="600"
app:ss_iconPadding="10dp"
app:ss_iconSize="22dp"
app:ss_leftIcon="#drawable/ic_contact_s"
app:ss_leftText="Contact"
app:ss_rightIcon="#drawable/ic_hourglass_s"
app:ss_rightText="Requests"
app:ss_textVisibility="gone"
app:ss_selectedTextSize="13sp"
app:ss_sliderBackgroundColor="#color/white"
app:ss_switchColor="?colorPrimaryTheme"
app:ss_textColor="?colorPrimaryTheme"
app:ss_textSize="12sp"
app:ss_animationType="line"
/>
You will have to do it programmatically or using a selector.
Programatically
Set a listener and then change the icon based on that:
stickySwitch.setOnSelectedChangeListener(object : StickySwitch.OnSelectedChangeListener {
override fun onSelectedChange(direction: StickySwitch.Direction, text: String) {
if (direction == StickySwitch.Direction.RIGHT) {
stickySwitch.setRightIcon(R.drawable.ic_hourglass_s_white)
stickySwitch.setLeftIcon(R.drawable.ic_contact_s_white)
} else {
stickySwitch.setRightIcon(R.drawable.ic_hourglass_s)
stickySwitch.setLeftIcon(R.drawable.ic_contact_s)
}
}
})
If the library can support KT then the setter are not necessary:
stickySwitch.rightIcon = R.drawable.ic_hourglass_s_white
But considering the setter is passing an R.drawable resource then maybe it has to be used.
The library does support setting the drawable directly, so you could apply a tint to the drawable instead
stickySwitch.setLeftIcon(drawable)
Drawables are mutable so if you keep or get the reference you could replace the logic above and instead of using a tint. This question should help you with the tint part:
Change drawable color programmatically
Selector
For creating a selector to make the job, there are plenty of questions and answers on SO, this example should help you:
https://stackoverflow.com/a/17647520/4017501
That would only work if the library is working like a regular switch, and for applying it:
app:ss_leftIcon="#drawable/your_custom_selector"
A selector handles the view state, so if is selected it would choose a drawable otherwise the other.

Chip programmatically generated not responding to click in ChipGroup

I've made a chipgroup and I want to dynamically generate in java its content, populating it with filter chips made from strings. I've made the Chipgroup but trying to add chips I notice that, no matter what I do, the chips are not responding to clicks. I also tried to make a single chip in the design editor and moving it in the same chipgroup, the results is that the design editor created chip is working as intended (checking and unchecking it), the generated ones are static (in the same group).
the code I use for the single working one:
<com.google.android.material.chip.ChipGroup
android:id="#+id/iChipGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:chipSpacing="8dp"
app:singleSelection="true" >
<com.google.android.material.chip.Chip
android:id="#+id/chip2"
style="#style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cwae" />
</com.google.android.material.chip.ChipGroup>
the way I create the dynamic ones
Chip chip = new Chip(iChipGroup.getContext());
ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.Widget_MaterialComponents_Chip_Filter);
chip.setChipDrawable(chipDrawable);
chip.setText(myText);
iChipGroup.addView(chip);
I've also tried to set an OnCheckedhangeListener both on the single chips and ChipGroup, got nothing.
If it helps, my hierarchy is (inside a fragment):
frameLayout
-scrollView
--LinearLayout
---chipGroup
----chip...
managed to make the chip respond by changing the way I made them this way
LayoutInflater layoutInflater = getLayoutInflater();
Chip chip = (Chip)layoutInflater.inflate(R.layout.my_filter_chip,ingredientsChipGroup, false);

Reading EditTextPreference

I can't seem to understand why I keep getting the "default" string show up when I grab input from the EditTextPreference.
<EditTextPreference
android:title="#string/settings_title_signature"
android:summary="#string/settings_enter_signature"
android:singleLine="true"
android:key="edit_signature_key"
/>
I never seem to get what the textfield has using Sharedpreferences. It just defaults to the "default", and not what should be in the key.
SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
String sig = myPreference.getString("edit_signature_key", "default");
Make sure you are using the EditTextPreference inside of a Preference Screen or Preference Fragment. Sounds like you are just using a standalone EditTextPreference.
If you are using it in the right container, please update your post with all of your code and I will be able to help.
(had to put this as an answer instead of a comment because I don't have enough rep)

Incompatible Types: Cannot Cast View to CheckPreferenceBox

I'm playing in the settings someone else made for my app and I want to uncheck a CheckPreferenceBox when the user cannot properly connect to dev servers.
I call my CheckPreferenceBox this way:
CheckBoxPreference mCheck = (CheckBoxPreference) findViewById(R.id.prefs_dev_mode);
mCheck.setChecked(false);
And here is the part in my .xml file related to it:
<CheckBoxPreference
android:id="#+id/prefs_dev_mode"
android:key="SP_DEV_SERVER"
android:title="#string/prefs_server_title"
android:summary="#string/prefs_server_summary"
android:defaultValue="false"/>
Do you see what could be the error?
Thanks
The problem that you face, is that CheckBoxPreference is not actually a View. It is a Preference. Therefore the findViewById function will not work for you.
You cannot interact with this element like a View.
If you want to set it unchecked, you should so something like this:
CheckBoxPreference pref = (CheckBoxPreference) getPreferenceScreen().findPreference("SP_DEV_SERVER");
pref.setChecked(false);

Soft keyboard listener

I'm making a game using AndEngine which uses the soft keyboard of the device.
I'm NOT using EditText, but rather my own. I'm trying to detect presses on the soft keyboard,
I've already succeeded in showing and hiding the keyboard over the AndEngine scene.
My activity implements the OnKeyboardActionListener, which I read is used as a soft-keyboard listener, but I'm not sure how to register the keyboard with this class (the activity).
Obviously, at the moment, the code inside onPress() is useless..
I couldn't find any examples, most of them refers to EditText, which I'm not using..
Is it possible?
Maybe a service or something?
Thanks in advance.
EDIT:
I'm trying to create my own keyboard using KeyboardView and a custom Keyboard,
This way I could do
KeyboardView kbView = new KeyboardView(this,null);
kbView.setKeyboard(new Keyboard(this, R.xml.keyboard);
kbView.setOnKeyboardActionListener(new OnKeyboardActionListener() {
....
}
Has anyone done this? works, doesn't?
Thanks.
I ended up creating my own Keyboard and KeyboardView`, then I used this code
CustomKeyboardView kbView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
kbView.setKeyboard(new Keyboard(this, R.xml.myCustomKeyboard);
kbView.setOnKeyboardActionListener(new OnKeyboardActionListener() {
#Override
public void onPress(int primaryCode) {
// A Key was pressed
}
....
}
And created a KeyboardView in the xml layout
<pathToCustomKeyboardView.CustomKeyboardView
android:id="#+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:layout_alignParentBottom="true" />
CustomKeyboardView only extends KeyboardView, dunno why, but it works with the custom and not with the original
EDIT:
Also, The activity extends SimpleLayoutGameActivity, And then I selected the layout which contains the CustomKeyboardView

Categories