Need Number Only Soft Keyboard?
Solution 1:
All you can do for standard keyboards is suggest input types. The keyboard can still display or not display whatever keys it wants. If you must have certain keys and only those, you need to create a custom soft keyboard. If it's only for your app, and especially if it's only for one activity, I wouldn't actually implement a standard keyboard, but just use views/buttons that do the appropriate actions.
Solution 2:
I've faced the same problem , and i found tat there is no android keyboard like this available and that the only way is to implement your own. so i would like to share with you my implement and hopefully save you some valuable time:
i've created this xml , you can modify the colors,fonts and the size of the keyboard accourding to your needs:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="300dp"android:layout_alignParentTop="true"android:layout_centerHorizontal="true" ><LinearLayoutandroid:id="@+id/one_to_three"android:layout_width="match_parent"android:layout_height="60dp"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:orientation="horizontal"android:weightSum="3" ><Buttonandroid:id="@+id/one_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="1"android:textSize="25sp" /><Buttonandroid:id="@+id/two_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="2"android:textSize="25sp" /><Buttonandroid:id="@+id/three_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="3"android:textSize="25sp" /></LinearLayout><LinearLayoutandroid:id="@+id/four_to_six"android:layout_width="match_parent"android:layout_height="60dp"android:layout_below="@+id/one_to_three"android:orientation="horizontal"android:weightSum="3" ><Buttonandroid:id="@+id/four_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="4"android:textSize="25sp" /><Buttonandroid:id="@+id/five_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="5"android:textSize="25sp" /><Buttonandroid:id="@+id/six_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="6"android:textSize="25sp" /></LinearLayout><LinearLayoutandroid:id="@+id/seven_to_nine"android:layout_width="match_parent"android:layout_height="60dp"android:layout_below="@+id/four_to_six"android:orientation="horizontal"android:weightSum="3" ><Buttonandroid:id="@+id/seven_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="7"android:textSize="25sp" /><Buttonandroid:id="@+id/eight_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="8"android:textSize="25sp" /><Buttonandroid:id="@+id/nine_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="9"android:textSize="25sp" /></LinearLayout><LinearLayoutandroid:id="@+id/zero"android:layout_width="match_parent"android:layout_height="60dp"android:layout_below="@+id/seven_to_nine"android:orientation="horizontal"android:weightSum="3" ><Buttonandroid:id="@+id/zero_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="2"android:text="0"android:textSize="25sp" /><Buttonandroid:id="@+id/back_btn"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="Back"android:textSize="25sp" /></LinearLayout><LinearLayoutandroid:id="@+id/done"android:layout_width="match_parent"android:layout_height="60dp"android:layout_below="@+id/zero"android:orientation="horizontal" ><Buttonandroid:id="@+id/done_btn"android:layout_width="match_parent"android:layout_height="match_parent"android:text="Done"android:textSize="30sp" /></LinearLayout></RelativeLayout>
i've created this fragment:
package com.galrom.keyboard; //replace it with your packageimport com.example.calculator.R;//import your own R classimport android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnLongClickListener; import android.widget.Button; publicclassKeyBoardFragmentextendsFragment { privateButton one_btn; privateButton two_btn; privateButton three_btn; privateButton four_btn; privateButton five_btn; privateButton six_btn; privateButton seven_btn; privateButton eight_btn; privateButton nine_btn; privateButton zero_btn; privateButton back_btn; privateButton done_btn; privateStringBuilder sb; private onKeyBoardEvent keyboardEventListener; private int maxLength=10; private int currentLength; publicstaticKeyBoardFragmentnewInstance(String EditTextValue) { KeyBoardFragment fragment=newKeyBoardFragment(); Bundle bundle=newBundle(); bundle.putString("et_value", EditTextValue); fragment.setArguments(bundle); return fragment; } @OverridepublicvoidonAttach(Activity activity) { try{ keyboardEventListener=(onKeyBoardEvent)activity; } catch(ClassCastException e) { Log.e("ClassCastException in KeyBoardFragment row 50",activity.toString()+" must implement onKeyboardEvent"); e.printStackTrace(); } super.onAttach(activity); } @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub sb=newStringBuilder(getArguments().getString("et_value")); currentLength=sb.length(); View rootView=inflater.inflate(R.layout.numeric_keyboard_layout, container, false); one_btn=(Button)rootView.findViewById(R.id.one_btn); one_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { // TODO Auto-generated method stubadd("1"); } }); two_btn=(Button)rootView.findViewById(R.id.two_btn); two_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("2"); } }); three_btn=(Button)rootView.findViewById(R.id.three_btn); three_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("3"); } }); four_btn=(Button)rootView.findViewById(R.id.four_btn); four_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("4"); } }); five_btn=(Button)rootView.findViewById(R.id.five_btn); five_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("5"); } }); six_btn=(Button)rootView.findViewById(R.id.six_btn); six_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("6"); } }); seven_btn=(Button)rootView.findViewById(R.id.seven_btn); seven_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("7"); } }); eight_btn=(Button)rootView.findViewById(R.id.eight_btn); eight_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("8"); } }); nine_btn=(Button)rootView.findViewById(R.id.nine_btn); nine_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { add("9"); } }); zero_btn=(Button)rootView.findViewById(R.id.zero_btn); zero_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { if(sb.length()>0) add("0"); } }); back_btn=(Button)rootView.findViewById(R.id.back_btn); back_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { if(sb.length()>0) { currentLength--; sb.deleteCharAt((sb.length())-1); keyboardEventListener.backButtonPressed(sb.toString()); } } }); back_btn.setOnLongClickListener(newView.OnLongClickListener() { @OverridepublicbooleanonLongClick(View v) { currentLength=0; sb=newStringBuilder(); keyboardEventListener.backLongPressed(); returnfalse; } }); done_btn=(Button)rootView.findViewById(R.id.done_btn); done_btn.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View v) { keyboardEventListener.doneButtonPressed(sb.toString()); } }); return rootView; } publicinterface onKeyBoardEvent { publicvoidnumberIsPressed(String total); publicvoiddoneButtonPressed(String total); publicvoidbackLongPressed(); publicvoidbackButtonPressed(String total); } public int getMaxLength() { return maxLength; } publicvoidsetMaxLength(int maxLength) { this.maxLength = maxLength; } publicvoidadd(String num) { currentLength++; if(currentLength<=maxLength) { sb.append(num); keyboardEventListener.numberIsPressed(sb.toString()); } else currentLength--; } }
3.the effect of a poping keyboard under the EditText when it is pressed is achived by creating an empty RelativeLayout that function as an container to the keyboard:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" >
<com.galrom.keyboard.EditTextNoKeyBoard
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Key_board_container"
android:layout_centerHorizontal="true"
android:clickable="true"
android:ems="10" />
<RelativeLayout
android:id="@+id/Key_board_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:background="#ffffff" >
</RelativeLayout>
when the user press on the EditText the we add the fragment to the container and when he presses done we hide it. the keyboard fragment comunicate with the Activity with the onKeyBoardEvent interace. NOTE:the hosting activity must implement this interface or else a ClassCastException will be thown.
VERY IMPORTANT: i didn't handled the orientation change, if you change to ladscape while the keyboard is open it will crash, so either disable landscape mode or handle the orientation change to avoid a nullPointerException on the key_board_fragment.
this is the Activity that implemets the keyBoard:
package com.galrom.keyboard;
import com.example.calculator.R;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
publicclassMainActivityextendsFragmentActivityimplementsKeyBoardFragment.onKeyBoardEvent{
privateEditText et;
privateKeyBoardFragment keyboard_fragment;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
et.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubif(keyboard_fragment==null)
{
keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString());
getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit();
}
else
{
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit();
else
{
keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString());
getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit();
}
}
});
}
@OverridepublicvoidnumberIsPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
}
@OverridepublicvoiddoneButtonPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit();
}
@OverridepublicvoidbackLongPressed() {
// TODO Auto-generated method stub
et.setText("");
}
@OverridepublicvoidbackButtonPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
}
@OverridepublicvoidonBackPressed() {
// TODO Auto-generated method stubif(keyboard_fragment!=null)
{
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().remove(keyboard_fragment).commit();
elsesuper.onBackPressed();
}
elsesuper.onBackPressed();
}
}
and the last thing: to disable the poping of the standart keyboard of android i've created an CustomEditText that simply returns false at: onCheckIsTextEditor() , this is the CustomEditText class:
package com.galrom.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
publicclassEditTextNoKeyBoardextendsEditText {
publicEditTextNoKeyBoard(Context context) {
super(context);
}
publicEditTextNoKeyBoard(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
publicEditTextNoKeyBoard(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverridepublicbooleanonCheckIsTextEditor() {
// TODO Auto-generated method stubreturnfalse;
}
}
Hope it helped you out... if you have suggestions for improvement i will be happy to hear. Gal.
Solution 3:
In addition to it set inputType="phone" on the EditText. That will open numeric pad keyboard once you start typing however it will include all extra characters related to the numbers. You would need to implement your own keyboard to keep only the numeric values.
Solution 4:
This solution uses numberPassword by overriding the default transformation method for the EditText to show characters instead of dots.
<EditText
android:id="@+id/userid"
android:inputType="numberPassword"
android:maxLength="6"
/>
Add to OnCreate.
// Numeric 6 character user id
EditText input = findViewById(R.id.userid);
// Process inputand show characters instead of dots
input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
Solution 5:
By default based on your device, the keyboard shows the special characters too in number keyboard . specifying the Keyboard type for the Text field you can achieve the expected result,such as
InputFieldName.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
i.e.
If you need only number included with special characters,then you can use
InputType.TYPE_CLASS_NUMBER
or
if you need to exclude those special characters too then use InputType.TYPE_NUMBER_VARIATION_PASSWORD
Post a Comment for "Need Number Only Soft Keyboard?"