Hot questions for Using Android EditText in android inputtype

Top Java Programmings / Android EditText / android inputtype

Question:

I want to change the input type of an edit text to visible when a switch is checked.

Switch a =(Switch) findViewById(R.id.switch1);
    a.setText("visible");
    a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }else{      
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        }
    });

It works when I turn on the switch and makes the password visible but when I turn it off it remains visible and doesn't change.


Answer:

pass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)

Try this in your else statement for showing the password type variation. The reason is because it needs to specify that the input class is of the type text.

Question:

I have an edittext that I want to accept positive and negative numbers.

It works phone on my phone, the keyboard shows the "-" sign, however it doesn't work on some phone whereas the "-" doesn't show up on their keyboard. See the attached images. How to fix it so that it works on all Android phones. I set the edittext to android:inputType="numberDecimal|numberSigned" The first image is on my phone whereas the 2nd image shows the user's phone and he's having problem entering the "-" sign. He uses Android version 5.0.1.

<EditText
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="numberDecimal|numberSigned"
/>

Thank you.


Answer:

Moving my comment to the answer:

I can see the - on both images. Probably on the lower one you need to hold the button for it though.

It worked, holding the button selects the -.

Question:

So what I would like is a very simple edit text with input type number or decimal that has the following aspects.

Number Edit Text: You can only type up to Long.MAX_VALUE / Long.MIN_VALUE, so I can cast to a long without modifying the user's input.

Number Decimal Edit Text: You can only type a number convertible to a double.

Also I do not need to use BigDecimal or BigIntegers, I need primitive values. Finally, I know this was already asked here but it is not what I want. There are some different requirements.


Answer:

I managed to do it with a custom class extending EditText that adds a TextWatcher in the constructor. In the afterTextChanged method it tries to convert that to a long. If it fails (NumberFormatException) it sets the number to the respective min / max value (based on the "-" sign at the beggining of the number).