For this tutorial, i will talk about setting up a custom keyboard that has only twelve buttons. The nine-digit buttons are labeled from 0 to 9. The remaining two buttons are the delete button (represented by the delete icon) and the dot button. When a digit button is pressed, its label will be appended to the EditText component. The delete button will remove the last character from the EditText. The dot button allows the user to append a dot sign (.) to the EditText.
To follow this tutorial, now you need to create a new Android project in Eclipse. The project name will be Keyboard.
The first step you will do in setting up the custom keyboard is adding the KeyboardView component in the activity_main.xml file. We also need an EditText component to display the characters pressed by the user. Here is the content of the activity_main.xml file.
activity_main.xml file
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txt_edit"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="top"
/>
<android.inputmethodservice.KeyboardView
android:visibility="gone"
android:id="@+id/customkeyboard"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
/>
</LinearLayout>
For another step, you have to write the buttons of the keyboard in a layout xml file. In this tutorial, this layout xml file of the keyboard is called keyboard.xml. The buttons can be grouped in rows by using the row tags. Each row consists of four to five buttons. You need to specify the code of each button and its label or icon. All row tags will be placed in the Keyboard tag. Below is the content of the keyboard.xml file.
keyboard.xml file
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="54dip">
<Row>
<Key android:codes="49" android:keyLabel="1" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3"/>
<Key android:codes="8" android:keyIcon="@drawable/delete_icon" />
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
<Key android:codes="55" android:keyLabel="7" />
</Row>
<Row>
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="46" android:keyLabel="."/>
</Row>
</Keyboard>
In the keyboard.xml file, the delete button is presented by the delete icon. Instead of using the keyLabel to specifying label of the button, you will use the keyIcon to specify the icon of the delete button.
In the last step, you need to write code to place the keyboard layout on the KeyboardView component and show it and to receive keys pressed by the user. The code will be written in the MainAcivity.java file. The Keyboard object will be created to point to the layout file. Then this object is supplied to KeyboardView component so that it is ready to show.
To receive the keys pressed on the keyboard, the KeyboardView component must be registered with the KeyboardActionListenter interface. The onPress method of the interface has to be implemented to receive the keys. Here is the content of the MainActivity.java file.
MainActivity.java file
package com.example.keyboard;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity{
private EditText et;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create Keyboard object
Keyboard keyboard=new Keyboard(this, R.layout.keyboard);
//create KeyboardView object
KeyboardView keyview=(KeyboardView)findViewById(R.id.customkeyboard);
//attache the keyboard object to the KeyboardView object
keyview.setKeyboard(keyboard);
//show the keyboard
keyview.setVisibility(KeyboardView.VISIBLE);
//take the keyboard to the front
keyview.bringToFront();
//register the keyboard to receive the key pressed
keyview.setOnKeyboardActionListener(new KeyList());
et=(EditText)findViewById(R.id.txt_edit);
}
class KeyList implements OnKeyboardActionListener{
public void onKey(View v, int keyCode, KeyEvent event) {
}
public void onText(CharSequence text){
}
public void swipeLeft(){
}
public void onKey(int primaryCode, int[] keyCodes) {
}
public void swipeUp(){
}
public void swipeDown() {
}
public void swipeRight() {
}
public void onPress(int primaryCode) {
if(primaryCode==8){ //take the last character out when delete button is pressed.
String text=et.getText().toString();
if(et.length()>0){
text=text.substring(0,text.length()-1);
et.setText(text);
et.setSelection(text.length());
}
}
else{
char ch=(char)primaryCode;
et.append(""+ch);
}
}
public void onRelease(int primaryCode) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now you are ready to run the Keyboard app. If you have any questions, please leave them at the comment section. I will reply as soon as possible.
I keep getting these 2 errors
ReplyDeleteerror: Invalid start tag Keyboard
R cannot be resolved to a variable
Need your help!
The xml ids are not consistent in this example
DeleteNull Pointer exception at the following line.
ReplyDeletekeyview.setKeyboard(keyboard);
Compiles properly, only exception@ run time.
Many thanks for simple code
ReplyDeleteThank you for this helpful code.
ReplyDeleteI am wondering how I could implement this with more than one EditText inputs layout?
Any hint? Thank you very much.
Thanks for sharing this.
ReplyDeletebut i want to know how is it looking in tablet landscape mode? I want to make the same keyboard but in tablet landscape mode its not looking good.
Hi and thank you for a great article. I really just love your blog. I will bookmark it and come back for more.!! recording app
ReplyDeleteGreat Kyara,,,Thank you so much for sharing total recall recording app. its really useful app.
DeleteNghĩ đến đây, hắn than nhẹ một tiếng, sờ sờ thạch châu trong ngực dùng dây thừng cột vào trước ngực, bảo bối này là thứ hắn cực kỳ lưu ý, Vương Lâm đọc sách nhiều, biết rất nhiều truyện xưa, trong đó bao gồm chuyện thất phu vô tội hoài bích có tội, hắn thầm hạ quyết tâm, không thể để cho bất luận kẻ nào biết bản thân có bảo bối này.
Không lâu sau, màn đêm buông xuống, một thiếu niên áo xám thân thể gầy yếu vẻ mặt mang thần sắc mệt mỏi, đẩy cửa phòng ra đi vào, sau khi hắn nhìn thấy Vương Lâm ngẩn ra, đánh giá cẩn thận một phen, cũng không hề để ý tới trực tiếp nằm ở trên giường, ngủ say như chết.
Vương Lâm cũng không để ý, hắn biết ngày mai phải dậy sớm, sờ sờ bụng, từ trong bọc lấy ra một ít khoai lang, đồ ăn này là cha mẹ hắn mang đến, chuẩn bị trên đường tìm kiếm hắn mà ăn, sau khi tìm được Vương Lâm thấy hắn được Hằng Nhạc Phái thu nhận, vì thế liền đem khoai lang còn thừa đều lưu lại cho hắn.
học kế toán thực hành
forum rao vặt cattleya
học kế toán tổng hợp
chung cư eco-green-city
học kế toán thực hành
học kế toán tại bắc ninh
dịch vụ kế toán trọn gói
chung cư hà nội
dịch vụ làm báo cáo tài chính
kế toán cho quản lý
khoá học kế toán thuế
keny idol
trung tâm kế toán tại long biên
trung tâm kế toán tại hải phòng
Khoai lang rất ngọt, Vương Lâm ăn vài miếng, lúc này thiếu niên nằm ở đối diện thân thể khẽ động, đứng dậy mắt cố định nhìn chằm chằm khoai lang, nuốt mấy nước bọt, thấp giọng nói:
- Cho ta một củ được không?
Vương Lâm lập tức đem ra mấy củ ném tới trên giường đối phương, cười nói:
- Ta nơi này có rất nhiều, ngươi thích ăn thì ăn nhiều một chút.
Thiếu niên lập tức nắm lên, ăn ngấu nghiến nuốt vào trong bụng, sau đó vội vàng chạy đến bàn bên cạnh rót chén nước, uống một ngụm, thở dài:
- Con mẹ nó, tiểu gia ta hai ngày cũng chưa ăn gì, bạn hữu, cám ơn. Đúng rồi, ngươi tên gì?
Vmware Training in Chennai
ReplyDeleteCCNA Training in Chennai
Angularjs Training in Chennai
Google CLoud Training in Chennai
Red Hat Training in Chennai
Linux Training in Chennai
Rhce Training in Chennai
Your site has a lot of useful information for myself. I visit regularly. Hope to have more quality items.
ReplyDeleteYou are so exellent. I'm your fan. I follow your post daily. Torrents now is one of the most popular type of download.
ReplyDeleteThanks for your good article. It's very important to know every person..
ReplyDeleteSEO, SMM, SMO, SEM, Web Designing Training in Chennai
Digital Marketing Course in Chennai
Digital marketing training institute in Chennai
Digital marketing classes in Chennai
Digital marketing training in Chennai
SEO Training Institute in Chennai
SEO Training in Chennai
SEO Classes in Chennai
SEO Course in Chennai
Best SEO Training in Chennai
SMO Training in Chennai
Social Media Marketing Institute in Chennai
SEM Training institute in Chennai
Web Designing Training in Chennai
Web Designing Classes in Chennai
Soft Skills Training in Chennai
شركة مكافحة حشرات ورش مبيدات ورش دفان
ReplyDeleteشركة مكافحة النمل الابيض بنجران
شركة مكافحة حشرات بنجران
شركة رش مبيدات بنجران
I love this post.
ReplyDeleteโปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
สมัครสล็อตออนไลน์ >>> goldenslot
สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย
Very cool!
ReplyDeleteเว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
และยังมีหวยให้คุณได้เล่น สมัครหวยออนไลน์ ได้เลย
สมัครสมาชิกที่นี่ >>> Gclub Royal1688
ร่วมลงทุนสมัครเอเย่นคาสิโนกับทีมงานของเราได้เลย
It was really an interesting blog, Thank you for providing unknown facts.
ReplyDeleteAviation Courses in Chennai
air hostess institute in Chennai
airline and airport management courses in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
ReplyDeleteنقل عفش
نقل عفش من الرياض الى مصر شركة شحن من الرياض الى مصرمن الباب للباب
شركة تركيب مكيفات سبليت بالرياض افضل شركة تركيب مكيفات بالرياض
شركة شحن عفش من المدينة لمصر شركة شحن عفش من المدينة لمصر
I’ll bookmark your website and take the feeds also¡KI’m happy to search out numerous useful info right here within the submit, we’d like develop extra strategies in this regard. Can you guess how much these celebrities are worth? Test your knowledge with Celebrity net worth.
ReplyDelete
ReplyDeleteIT Resource Hiring Services
Good post thanks for share information.
ReplyDeletefree movie streaming sites no sign up
terrarium tv replacement