Showing posts with label user defined. Show all posts
Showing posts with label user defined. Show all posts

Jun 28, 2011

Create Your Own Custom Keyboard for Android Devices



In this post i Created Simple Keyboard which contains Some special keys like ( France keys ) and it's supported Capital letters and small letters and Number keys and some Symbols .


package sra.keyboard;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class Main extends Activity implements OnTouchListener, OnClickListener,
OnFocusChangeListener {
private EditText mEt, mEt1; // Edit Text boxes
private Button mBSpace, mBdone, mBack, mBChange, mNum;
private RelativeLayout mLayout, mKLayout;
private boolean isEdit = false, isEdit1 = false;
private String mUpper = "upper", mLower = "lower";
private int w, mWindowWidth;
private String sL[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z", "ç", "à", "é", "è", "û", "î" };
private String cL[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "ç", "à", "é", "è", "û", "î" };
private String nS[] = { "!", ")", "'", "#", "3", "$", "%", "&", "8", "*",
"?", "/", "+", "-", "9", "0", "1", "4", "@", "5", "7", "(", "2",
"\"", "6", "_", "=", "]", "[", "<", ">", "|" };
private Button mB[] = new Button[32];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
// adjusting key regarding window sizes
setKeys();
setFrow();
setSrow();
setTrow();
setForow();
mEt = (EditText) findViewById(R.id.xEt);
mEt.setOnTouchListener(this);
mEt.setOnFocusChangeListener(this);
mEt1 = (EditText) findViewById(R.id.et1);

mEt1.setOnTouchListener(this);
mEt1.setOnFocusChangeListener(this);
mEt.setOnClickListener(this);
mEt1.setOnClickListener(this);
mLayout = (RelativeLayout) findViewById(R.id.xK1);
mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);

} catch (Exception e) {
Log.w(getClass().getName(), e.toString());
}

}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == mEt) {
hideDefaultKeyboard();
enableKeyboard();

}
if (v == mEt1) {
hideDefaultKeyboard();
enableKeyboard();

}
return true;
}

@Override
public void onClick(View v) {

if (v == mBChange) {

if (mBChange.getTag().equals(mUpper)) {
changeSmallLetters();
changeSmallTags();
} else if (mBChange.getTag().equals(mLower)) {
changeCapitalLetters();
changeCapitalTags();
}

} else if (v != mBdone && v != mBack && v != mBChange && v != mNum) {
addText(v);

} else if (v == mBdone) {

disableKeyboard();

} else if (v == mBack) {
isBack(v);
} else if (v == mNum) {
String nTag = (String) mNum.getTag();
if (nTag.equals("num")) {
changeSyNuLetters();
changeSyNuTags();
mBChange.setVisibility(Button.INVISIBLE);

}
if (nTag.equals("ABC")) {
changeCapitalLetters();
changeCapitalTags();
}

}

}

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == mEt && hasFocus == true) {
isEdit = true;
isEdit1 = false;

} else if (v == mEt1 && hasFocus == true) {
isEdit = false;
isEdit1 = true;

}

}

private void addText(View v) {
if (isEdit == true) {
String b = "";
b = (String) v.getTag();
if (b != null) {
// adding text in Edittext
mEt.append(b);

}
}

if (isEdit1 == true) {
String b = "";
b = (String) v.getTag();
if (b != null) {
// adding text in Edittext
mEt1.append(b);

}

}

}

private void isBack(View v) {
if (isEdit == true) {
CharSequence cc = mEt.getText();
if (cc != null && cc.length() > 0) {
{
mEt.setText("");
mEt.append(cc.subSequence(0, cc.length() - 1));
}

}
}

if (isEdit1 == true) {
CharSequence cc = mEt1.getText();
if (cc != null && cc.length() > 0) {
{
mEt1.setText("");
mEt1.append(cc.subSequence(0, cc.length() - 1));
}
}
}
}
private void changeSmallLetters() {
mBChange.setVisibility(Button.VISIBLE);
for (int i = 0; i < sL.length; i++)
mB[i].setText(sL[i]);
mNum.setTag("12#");
}
private void changeSmallTags() {
for (int i = 0; i < sL.length; i++)
mB[i].setTag(sL[i]);
mBChange.setTag("lower");
mNum.setTag("num");
}
private void changeCapitalLetters() {
mBChange.setVisibility(Button.VISIBLE);
for (int i = 0; i < cL.length; i++)
mB[i].setText(cL[i]);
mBChange.setTag("upper");
mNum.setText("12#");

}

private void changeCapitalTags() {
for (int i = 0; i < cL.length; i++)
mB[i].setTag(cL[i]);
mNum.setTag("num");

}

private void changeSyNuLetters() {

for (int i = 0; i < nS.length; i++)
mB[i].setText(nS[i]);
mNum.setText("ABC");
}

private void changeSyNuTags() {
for (int i = 0; i < nS.length; i++)
mB[i].setTag(nS[i]);
mNum.setTag("ABC");
}

// enabling customized keyboard
private void enableKeyboard() {

mLayout.setVisibility(RelativeLayout.VISIBLE);
mKLayout.setVisibility(RelativeLayout.VISIBLE);

}

// Disable customized keyboard
private void disableKeyboard() {
mLayout.setVisibility(RelativeLayout.INVISIBLE);
mKLayout.setVisibility(RelativeLayout.INVISIBLE);

}

private void hideDefaultKeyboard() {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

private void setFrow() {
w = (mWindowWidth / 13);
w = w - 15;
mB[16].setWidth(w);
mB[22].setWidth(w + 3);
mB[4].setWidth(w);
mB[17].setWidth(w);
mB[19].setWidth(w);
mB[24].setWidth(w);
mB[20].setWidth(w);
mB[8].setWidth(w);
mB[14].setWidth(w);
mB[15].setWidth(w);
mB[16].setHeight(50);
mB[22].setHeight(50);
mB[4].setHeight(50);
mB[17].setHeight(50);
mB[19].setHeight(50);
mB[24].setHeight(50);
mB[20].setHeight(50);
mB[8].setHeight(50);
mB[14].setHeight(50);
mB[15].setHeight(50);

}

private void setSrow() {
w = (mWindowWidth / 10);
mB[0].setWidth(w);
mB[18].setWidth(w);
mB[3].setWidth(w);
mB[5].setWidth(w);
mB[6].setWidth(w);
mB[7].setWidth(w);
mB[26].setWidth(w);
mB[9].setWidth(w);
mB[10].setWidth(w);
mB[11].setWidth(w);
mB[26].setWidth(w);

mB[0].setHeight(50);
mB[18].setHeight(50);
mB[3].setHeight(50);
mB[5].setHeight(50);
mB[6].setHeight(50);
mB[7].setHeight(50);
mB[9].setHeight(50);
mB[10].setHeight(50);
mB[11].setHeight(50);
mB[26].setHeight(50);
}

private void setTrow() {
w = (mWindowWidth / 12);
mB[25].setWidth(w);
mB[23].setWidth(w);
mB[2].setWidth(w);
mB[21].setWidth(w);
mB[1].setWidth(w);
mB[13].setWidth(w);
mB[12].setWidth(w);
mB[27].setWidth(w);
mB[28].setWidth(w);
mBack.setWidth(w);

mB[25].setHeight(50);
mB[23].setHeight(50);
mB[2].setHeight(50);
mB[21].setHeight(50);
mB[1].setHeight(50);
mB[13].setHeight(50);
mB[12].setHeight(50);
mB[27].setHeight(50);
mB[28].setHeight(50);
mBack.setHeight(50);

}

private void setForow() {
w = (mWindowWidth / 10);
mBSpace.setWidth(w * 4);
mBSpace.setHeight(50);
mB[29].setWidth(w);
mB[29].setHeight(50);

mB[30].setWidth(w);
mB[30].setHeight(50);

mB[31].setHeight(50);
mB[31].setWidth(w);
mBdone.setWidth(w + (w / 1));
mBdone.setHeight(50);

}

private void setKeys() {
mWindowWidth = getWindowManager().getDefaultDisplay().getWidth(); // getting
// window
// height
// getting ids from xml files
mB[0] = (Button) findViewById(R.id.xA);
mB[1] = (Button) findViewById(R.id.xB);
mB[2] = (Button) findViewById(R.id.xC);
mB[3] = (Button) findViewById(R.id.xD);
mB[4] = (Button) findViewById(R.id.xE);
mB[5] = (Button) findViewById(R.id.xF);
mB[6] = (Button) findViewById(R.id.xG);
mB[7] = (Button) findViewById(R.id.xH);
mB[8] = (Button) findViewById(R.id.xI);
mB[9] = (Button) findViewById(R.id.xJ);
mB[10] = (Button) findViewById(R.id.xK);
mB[11] = (Button) findViewById(R.id.xL);
mB[12] = (Button) findViewById(R.id.xM);
mB[13] = (Button) findViewById(R.id.xN);
mB[14] = (Button) findViewById(R.id.xO);
mB[15] = (Button) findViewById(R.id.xP);
mB[16] = (Button) findViewById(R.id.xQ);
mB[17] = (Button) findViewById(R.id.xR);
mB[18] = (Button) findViewById(R.id.xS);
mB[19] = (Button) findViewById(R.id.xT);
mB[20] = (Button) findViewById(R.id.xU);
mB[21] = (Button) findViewById(R.id.xV);
mB[22] = (Button) findViewById(R.id.xW);
mB[23] = (Button) findViewById(R.id.xX);
mB[24] = (Button) findViewById(R.id.xY);
mB[25] = (Button) findViewById(R.id.xZ);
mB[26] = (Button) findViewById(R.id.xS1);
mB[27] = (Button) findViewById(R.id.xS2);
mB[28] = (Button) findViewById(R.id.xS3);
mB[29] = (Button) findViewById(R.id.xS4);
mB[30] = (Button) findViewById(R.id.xS5);
mB[31] = (Button) findViewById(R.id.xS6);
mBSpace = (Button) findViewById(R.id.xSpace);
mBdone = (Button) findViewById(R.id.xDone);
mBChange = (Button) findViewById(R.id.xChange);
mBack = (Button) findViewById(R.id.xBack);
mNum = (Button) findViewById(R.id.xNum);
for (int i = 0; i < mB.length; i++)
mB[i].setOnClickListener(this);
mBSpace.setOnClickListener(this);
mBdone.setOnClickListener(this);
mBack.setOnClickListener(this);
mBChange.setOnClickListener(this);
mNum.setOnClickListener(this);

}

}


Jun 22, 2011

How do I make my own keyboard for an app in android?

fd
am currently working on an app that requires a keyboard in a different language (Some Special keys).

in some cases we have to Put some extra keys in keyboard .
so that situations we have to design our own keyboard .


Main.java
main.xml
keyboard.java

for customized keyboard i used 3 files

Main.java
-----------
package sra.key;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class Main extends Activity implements OnTouchListener, OnClickListener,
OnFocusChangeListener {
private EditText mEt, mEt1; // Edit Text boxes
private Button mBa, mBb, mBc, mBd, mBe, mBf, mBg, mBh, mBi, mBj, mBk, mBl,
mBm, mBn, mBo, mBp, mBq, mBr, mBs, mBt, mBu, mBv, mBw, mBx, mBy,
mBz, mBs1, mBs2, mBs3, mBs4, mBs5, mBs6, mBSpace, mBdone;
private String st = "";
private RelativeLayout mLayout, mKLayout, mMainLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
mEt = (EditText) findViewById(R.id.xEt);
mEt.setOnTouchListener(this);
mEt.setOnFocusChangeListener(this);
mEt1 = (EditText) findViewById(R.id.et1);
mEt1.setOnClickListener(this);
mEt1.setOnFocusChangeListener(this);
mLayout = (RelativeLayout) findViewById(R.id.xK1);
mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);
mMainLayout = (RelativeLayout) findViewById(R.id.xMLayout);
// adjusting key regarding window sizes
setKeys();
setFrow();
setSrow();
setTrow();
setForow();
} catch (Exception e) {
Log.w(getClass().getName(), e.toString());
}

}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == mEt) {
// disable default keyboard in EditText box
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
enableKeyboard();
}
if (v == mEt1) {
disableKeyboard();
}
return true;
}

int w, mWindowWidth;

private void setKeys() {
mWindowWidth = getWindowManager().getDefaultDisplay().getWidth(); // getting
// window
// height
// getting ids from xml files
mBa = (Button) findViewById(R.id.xA);
mBb = (Button) findViewById(R.id.xB);
mBc = (Button) findViewById(R.id.xC);
mBd = (Button) findViewById(R.id.xD);
mBe = (Button) findViewById(R.id.xE);
mBf = (Button) findViewById(R.id.xF);
mBg = (Button) findViewById(R.id.xG);
mBh = (Button) findViewById(R.id.xH);
mBi = (Button) findViewById(R.id.xI);
mBj = (Button) findViewById(R.id.xJ);
mBk = (Button) findViewById(R.id.xK);
mBl = (Button) findViewById(R.id.xL);
mBm = (Button) findViewById(R.id.xM);
mBn = (Button) findViewById(R.id.xN);
mBo = (Button) findViewById(R.id.xO);
mBp = (Button) findViewById(R.id.xP);
mBq = (Button) findViewById(R.id.xQ);
mBr = (Button) findViewById(R.id.xR);
mBs = (Button) findViewById(R.id.xS);
mBt = (Button) findViewById(R.id.xT);
mBu = (Button) findViewById(R.id.xU);
mBv = (Button) findViewById(R.id.xV);
mBw = (Button) findViewById(R.id.xW);
mBx = (Button) findViewById(R.id.xX);
mBy = (Button) findViewById(R.id.xY);
mBz = (Button) findViewById(R.id.xZ);
mBs1 = (Button) findViewById(R.id.xS1);
mBs2 = (Button) findViewById(R.id.xS2);
mBs3 = (Button) findViewById(R.id.xS3);
mBs4 = (Button) findViewById(R.id.xS4);
mBs5 = (Button) findViewById(R.id.xS5);
mBs6 = (Button) findViewById(R.id.xS6);
mBSpace = (Button) findViewById(R.id.xSpace);
mBdone = (Button) findViewById(R.id.xDone);

mBi.setOnClickListener(this);
mBb.setOnClickListener(this);
mBc.setOnClickListener(this);
mBd.setOnClickListener(this);
mBe.setOnClickListener(this);
mBf.setOnClickListener(this);
mBg.setOnClickListener(this);
mBh.setOnClickListener(this);
mBi.setOnClickListener(this);
mBj.setOnClickListener(this);
mBk.setOnClickListener(this);
mBl.setOnClickListener(this);
mBm.setOnClickListener(this);
mBn.setOnClickListener(this);
mBo.setOnClickListener(this);
mBp.setOnClickListener(this);
mBq.setOnClickListener(this);
mBr.setOnClickListener(this);
mBs.setOnClickListener(this);
mBt.setOnClickListener(this);
mBu.setOnClickListener(this);
mBv.setOnClickListener(this);
mBw.setOnClickListener(this);
mBx.setOnClickListener(this);
mBy.setOnClickListener(this);
mBz.setOnClickListener(this);
mBs1.setOnClickListener(this);
mBs2.setOnClickListener(this);
mBs3.setOnClickListener(this);
mBs4.setOnClickListener(this);
mBs5.setOnClickListener(this);
mBs6.setOnClickListener(this);
mBSpace.setOnClickListener(this);
mBdone.setOnClickListener(this);

}

@Override
public void onClick(View v) {

if (v != mBdone) {
String b = "";
b = (String) v.getTag();
if (b != null) {
// adding text in Edittext
mEt.append(b);

}

}
if (v == mBdone) {

disableKeyboard();

}

if (v == mMainLayout) {
disableKeyboard();
}
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == mEt) {
if (!hasFocus) {
enableKeyboard();
getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
} else {
disableKeyboard();
}
}
if (v == mEt1)
disableKeyboard();

}

// enabling customized keyboard
private void enableKeyboard() {
mLayout.setVisibility(RelativeLayout.VISIBLE);
mKLayout.setVisibility(RelativeLayout.VISIBLE);
}

// Disable customized keyboard
private void disableKeyboard() {
mLayout.setVisibility(RelativeLayout.INVISIBLE);
mKLayout.setVisibility(RelativeLayout.INVISIBLE);
}

private void setFrow() {
w = (mWindowWidth / 10);
mBq.setWidth(w);
mBw.setWidth(w);
mBe.setWidth(w);
mBr.setWidth(w);
mBt.setWidth(w);
mBy.setWidth(w);
mBu.setWidth(w);
mBi.setWidth(w);
mBo.setWidth(w);
mBp.setWidth(w);

mBq.setHeight(50);
mBw.setHeight(50);
mBe.setHeight(50);
mBr.setHeight(50);
mBt.setHeight(50);
mBy.setHeight(50);
mBu.setHeight(50);
mBi.setHeight(50);
mBo.setHeight(50);
mBp.setHeight(50);

}

private void setSrow() {
w = (mWindowWidth / 10);
mBa.setWidth(w);
mBs.setWidth(w);
mBd.setWidth(w);
mBf.setWidth(w);
mBg.setWidth(w);
mBh.setWidth(w);
mBs1.setWidth(w);
mBj.setWidth(w);
mBk.setWidth(w);
mBl.setWidth(w);
mBs1.setWidth(w);

mBa.setHeight(50);
mBs.setHeight(50);
mBd.setHeight(50);
mBf.setHeight(50);
mBg.setHeight(50);
mBh.setHeight(50);
mBj.setHeight(50);
mBk.setHeight(50);
mBl.setHeight(50);
mBs1.setHeight(50);
}

private void setTrow() {
w = (mWindowWidth / 9);
mBz.setWidth(w);
mBx.setWidth(w);
mBc.setWidth(w);
mBv.setWidth(w);
mBb.setWidth(w);
mBn.setWidth(w);
mBm.setWidth(w);
mBs2.setWidth(w);
mBs3.setWidth(w);

mBz.setHeight(50);
mBx.setHeight(50);
mBc.setHeight(50);
mBv.setHeight(50);
mBb.setHeight(50);
mBn.setHeight(50);
mBm.setHeight(50);
mBs2.setHeight(50);
mBs3.setHeight(50);

}

private void setForow() {
w = (mWindowWidth / 10);
mBSpace.setWidth(w * 4);
mBSpace.setHeight(50);
mBs4.setWidth(w);
mBs4.setHeight(50);

mBs5.setWidth(w);
mBs5.setHeight(50);

mBs6.setHeight(50);
mBs6.setWidth(w);

mBdone.setWidth(w + (w / 2));
mBdone.setHeight(50);

}

}

main.xml
--------------
android:orientation="horizontal" android:layout_width="fill_parent"
android:id="@+id/xMLayout" android:background="#000000"
android:layout_height="fill_parent">
android:orientation="horizontal" android:layout_width="fill_parent"
android:id="@+id/xsubLayout" android:keepScreenOn="true"
android:layout_height="fill_parent">
android:focusableInTouchMode="true" android:layout_height="wrap_content" />
android:layout_below="@+id/xEt" android:layout_height="wrap_content" />
android:orientation="horizontal" android:layout_width="fill_parent"
android:id="@+id/xK1" android:layout_height="wrap_content"
android:visibility="gone">

keyboard.xml
------------------
android:id="@+id/hebrwKeyboardView" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:layout_below="@+id/xsubLayout"
android:background="#252625" android:visibility="visible"
android:layout_height="180sp">
android:layout_width="fill_parent" android:layout_height="200sp"
android:layout_alignParentBottom="true" android:clipChildren="false"

android:layout_gravity="center_vertical|center_horizontal|bottom"
android:fitsSystemWindows="true" android:orientation="horizontal">
android:layout_width="fill_parent" android:layout_height="200sp"
android:layout_gravity="center_vertical|center_horizontal|bottom"
android:orientation="vertical">
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"

android:orientation="horizontal" android:stretchColumns="true">
android:layout_width="fill_parent" android:layout_gravity="center"
android:layout_height="45sp" android:fitsSystemWindows="true"

android:orientation="horizontal">