0

I'm looking for the easiest way (ie without rewritting a keyboard, if possible - maybe using an existing one out there?) to create an editText to input a digit password.

It's not that uncommon, yet I found no flag combination to do that.

Since it's digit only characters, I either want a digit only keyboard (ideally like on the iphone, 4*3=12 keys, 0-10+del+ok), or at least a phone-like keyboard, with ()+* and characters like that, but so that they can't be written in the textEdit.

I don't mind using deprecated flags.

In particular, number|textPassword for inputType doesn't work (characters not hidden), android:password="true" android:inputType="phone" and android:numeric="integer" android:password="true" produce the same result, but the hint text disappears and special characters are still allowed.

Thanks.

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
John156
  • 83
  • 1
  • 1
  • 6
  • Is this perhaps a duplicate of http://stackoverflow.com/questions/1119583/how-do-i-show-the-number-keyboard-on-an-edittext-in-android ? – Phil Aug 18 '11 at 14:08

2 Answers2

0

I'm afraid that there might be no easy way to achieve that. android:password="true" only hides the input text and inputType="phone" still allow some special character for dialing although you can make those character do nothing by handling KeyEvent.

I was required to do the same thing once, and eventually implemented a custom layout that only rises when the EditText gets focused. If you can't find better solution and you don't have too many EditText of this type, maybe it is the way to go

romy_ngo
  • 1,061
  • 8
  • 15
  • Could you share the code or do you know of an open source code that produces the similar feature ? – John156 Aug 18 '11 at 13:57
  • Unfortunately, that piece of code doesn't belong to me. Have you tried this one? http://stackoverflow.com/questions/2420181/android-numeric-password-field/4014588#4014588. If you don't want special character ignore it by handling `KeyEvent` – romy_ngo Aug 18 '11 at 14:04
0
//declarations
AlertDialog keyboardBuilder;
String number;
float myValue;

//building the keyboard
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.keyboard, (ViewGroup) findViewById(R.id.keyboardLayout));
keyboardBuilder = new AlertDialog.Builder(this).create();
keyboardBuilder.setView(layout);

//When you your keyboard to appear
keyboardBuilder.show();

//actions of keys
public void kb1(View v) {number += "1"; keyboardBuilder.setTitle(number);}
    public void kb2(View v) {number += "2"; keyboardBuilder.setTitle(number);}
    public void kb3(View v) {number += "3"; keyboardBuilder.setTitle(number);}
    public void kb4(View v) {number += "4"; keyboardBuilder.setTitle(number);}
    public void kb5(View v) {number += "5"; keyboardBuilder.setTitle(number);}
    public void kb6(View v) {number += "6"; keyboardBuilder.setTitle(number);}
    public void kb7(View v) {number += "7"; keyboardBuilder.setTitle(number);}
    public void kb8(View v) {number += "8"; keyboardBuilder.setTitle(number);}
    public void kb9(View v) {number += "9"; keyboardBuilder.setTitle(number);}
    public void kbPoint(View v) {number += "."; keyboardBuilder.setTitle(number);}
    public void kbE(View v) {number += "e"; keyboardBuilder.setTitle(number);}
    public void kbMoins(View v) {number += "-"; keyboardBuilder.setTitle(number);}

    public void kbDelete(View v)
    {
        if (number.length() > 0)
        {
            number = number.substring(0, number.length()-1);
        }
        keyboardBuilder.setTitle(number);
    }

    public void kbSave(View v) 
    {
        try
        {
            myValue = Float.parseFloat(String.valueOf(number)));
            keyboardBuilder.dismiss();
        }
        catch(Exception e)
        {
            Toast.makeText(this,"Forbidden value.", Toast.LENGTH_SHORT).show();
        }
    }


              android:id="@+id/keyboardLayout"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

        <LinearLayout android:id="@+id/keyboardLayout"
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:gravity="center">
                      <Button android:text="-" android:onClick="kbMoins" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <Button android:text="1" android:onClick="kb1" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <Button android:text="2" android:onClick="kb2" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
        </LinearLayout>

        <LinearLayout android:id="@+id/keyboardLayout"
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:gravity="center">
                      <Button android:text="." android:onClick="kbPoint" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <Button android:text="3" android:onClick="kb3" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <Button android:text="4" android:onClick="kb4" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
        </LinearLayout>

        <LinearLayout android:id="@+id/keyboardLayout"
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:gravity="center">
                      <Button android:text="e" android:onClick="kbE" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <Button android:text="5" android:onClick="kb5" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <Button android:text="6" android:onClick="kb6" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>`
        </LinearLayout>

        <LinearLayout android:id="@+id/keyboardLayout"
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:gravity="center">
                      <Button android:text="delete" android:onClick="kbDelete" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="20px"></Button>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <Button android:text="7" android:onClick="kb7" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <Button android:text="8" android:onClick="kb8" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
        </LinearLayout>

        <LinearLayout android:id="@+id/keyboardLayout"
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:gravity="center">
                      <Button android:text="save" android:onClick="kbSave" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="20px"></Button>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <TextView android:id="@+id/espace" android:text="" android:gravity="center" android:textSize="35px" android:layout_height="65px" android:layout_width="85px"> </TextView>
                      <Button android:text="9" android:onClick="kb9" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>
                      <Button android:text="0" android:onClick="kb0" android:id="@+id/hideVerticalDocks" android:layout_height="65px" android:layout_width="85px" android:textSize="35px"></Button>`
        </LinearLayout>


</LinearLayout>

If you want to hide the password you can delete the keyboardBuilder.setTitle.

The xml was poorly made for a 800*480 landscape, feel free to write it better.

You can also delete the "e", "-" and "." since you just want it for password use.

Note that the checking for not a value with the toast can be deleted, but you can replace it by checking password size for example.

mthpvg
  • 3,789
  • 3
  • 26
  • 34
  • I tried your snippet but I must be doing something wrong because this is what I get: http://imageshack.us/photo/my-images/8/keyboardn.png/ Could you please tell in a little more details how to use it ? Thanks – John156 Aug 18 '11 at 14:37
  • As I said it was designed for a 800*480. Try in each of the 5 LinearLayout of the xml to delete 3 out the 5 of : – mthpvg Aug 18 '11 at 14:44
  • I tried to reuse your architecture idea but I completely rewrote the layout since it didn't fit my need. I tried using [this code](http://pastebin.com/szmysETj) to make the alertDialog go on the bottom of the screen but it just pops up in the center of it. I'm also not pleased with the fact that there is a white border, nor with the fact that it doesn't take the whole width. – John156 Aug 19 '11 at 13:36