I'm currently working on own implementation of Input Method Editor (IME) or can be called as Softkeyboard in Android, I have read creating an input method and I have downloaded the SoftKeyboard sample code provided as part of the SDK. I have the following code from sample Softkeyboard:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
/**
* Swapping here with my desired unicode character
* */
if (primaryCode >= 97 && primaryCode <= 122 ) {
mComposing.append(Swap.swapLetters(primaryCode));
}else{
mComposing.append((char) primaryCode);
}
getCurrentInputConnection().setComposingText(mComposing, 1);
updateShiftKeyState(getCurrentInputEditorInfo());
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}
}
the code given above works fine, but when I click the key:
<Key android:codes="-1"
android:keyWidth="15%p" android:isModifier="true"
android:isSticky="true" android:keyEdgeFlags="left"/>
which is like Shift key, this converts the keys to uppercase I don't know how to show my next keys this key is clicked/pressed, the following is Logcat of exception:
I have traced that this might be occurring in this place where we handle the Shift key :
private void handleShift() {
if (mInputView == null) {
return;
}
Keyboard currentKeyboard = mInputView.getKeyboard();
if (mQwertyKeyboard == currentKeyboard) {
// Alphabet keyboard
checkToggleCapsLock();
mInputView.setKeyboard(mSindhi);
} else if (currentKeyboard == mSymbolsKeyboard) {
mSymbolsKeyboard.setShifted(true);
mInputView.setKeyboard(mSymbolsShiftedKeyboard);
mSymbolsShiftedKeyboard.setShifted(true);
} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
mSymbolsShiftedKeyboard.setShifted(false);
mInputView.setKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
}
now I have not any idea how to get rid of exception mentioned above, and get my code working. please solve this one!
There may be some other solutions for your problem, but I have done one trick and came up with following :
Step-1 You have to remove all the calls to the method updateShiftKeyState(getCurrentInputEditorInfo()); in your code like:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
/**
* Swapping here with my desired unicode character
* */
if (primaryCode >= 97 && primaryCode <= 122 ) {
mComposing.append(Swap.swapLetters(primaryCode));
}else{
mComposing.append((char) primaryCode);
}
getCurrentInputConnection().setComposingText(mComposing, 1);
//updateShiftKeyState(getCurrentInputEditorInfo()); // you can delete this method from your class and clean-up all the occurances of that
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}}
Step-2 change your handleShift method to his:
private void handleShift() {
if (mInputView == null) {
return;
}
Keyboard currentKeyboard = mInputView.getKeyboard();
if (mQwertyKeyboard == currentKeyboard) {
mInputView.setKeyboard(mSindhi);
} else if (currentKeyboard == mSymbolsKeyboard) {
mInputView.setKeyboard(mSymbolsShiftedKeyboard);
} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
mInputView.setKeyboard(mSymbolsKeyboard);
}
}
that is all, Hope this may work for you...
Related
if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
if (buttonClicked.contains(3) && buttonClicked.contains(4) && buttonClicked.contains(5)) {
if (playerOneLastClicked) {
imgViewBackground2.setImageResource(R.drawable.rca)
} else {
imgViewBackground4.setImageResource(R.drawable.wac)
}
}
when player1 or player2 last click in (buttonClicked.contains(1) && buttonClicked.contains(2)) imgViewBackgound1 change but when the players click button 3,4 or 5 imgViewBackground keep changing.
I want when imgViewBackground1 take (R.drawable.rca) or (R.drawable.wac) don't change any more.
If imgViewBackground is not going to have an imageview you could just add an extra check e.g.
if (imgViewBackground1.drawable == null) {
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
}
or if it would have an imageview already and you don't want it set again
if (imgViewBackground1.drawable != resources.getDrawable(R.drawable.rcs, null)
|| imgViewBackground1.drawable != resources.getDrawable(R.drawable.wac, null))
if (playerOneLastClicked) {
imgViewBackground1.setImageResource(R.drawable.rca)
} else {
imgViewBackground1.setImageResource(R.drawable.wac)
}
}
}
I made a project Led on/offbut with 5 leds. I send strings to arduino bluetooth for on or off leds. But i need led on only some leds. For example i wont led on pin 10 and pin 9. But Before on pin 12 i can't on pin 11, and before off pin 12 i can't off another pins? Why? I put my code here:
For arduino:
while(Serial.available() > 0)
{
command = ((byte)Serial.read());
if(command == ':')
{
break;
}
else
{
string += command;
}
delay(1);
}
if(string == "A")
{
ledOn();// ledOn(){analogWrite(led, 255); delay(10);}
}
if(string =="B")
{
ledOff();
}
if(string == "C")
{
ledOnB();
}
if(string =="D")
{
ledOffB();
}
if(string == "E")
{
ledOnC();
}
if(string =="F")
{
ledOffC();
}
if(string =="G")
{
ledOnD();
}
if(string =="H")
{
ledOffD();
}
if(string == "I")
{
ledOnE();
}
if(string =="J")
{
ledOffE();
}
I created registration screen i want to disable submit button and it should enable only when all the fields get correct input, bellow i added list of text fields these fields must get correct input and submit button must enable.
protected boolean validateData() {
if (email.getText().toString().trim().length() == 0) {
email.setError("Email-ID/Registered Number can not be empty!");
return false;
} else if (!(email.getText().toString().trim().contains("#"))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
} else if (!(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (!(email.getText().toString().trim().contains("#"))
&& !(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() > 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (email.getText().toString().trim().contains("#")
|| email.getText().toString().trim().contains(".")) {
if (!WebServiceSingleTon.getInstance().isEmailValid(
email.getText().toString().trim())) {
email.setError("Email is not valid!");
return false;
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
return true;
}
to make a button "disabled" You'll need to set it's android:clickable property to false. Then, set a custom background on it that changes its background to gray (or however you want to it look while its disabled). Then after you call that validateData() function you can check to see if you should change the background of the button to be "enabled"
I'm facing a problem with the focus on my application. I'm trying to catch the click event to animate my edittext, but for a reason I don't understand, sometimes my edittext is focused but the click event is not trigged.
The user has to click a 2nd time to start the animation.
I've got 2 AutocompleteTextView in my SherlockFragment.
Is it possible that come from the animation I'm doing ?
Edit:
mEtWhere.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "mEtWhere've been clicked");
if (!mllWhereHasBeenClicked) {
mllWhereHasBeenClicked = true;
if (mAlphaFrameLayout != null) {
animate(mAlphaFrameLayout).setDuration(ANIM_DURATION / 2).alpha(0.9f);
}
if (mllWhere != null) {
oldllWhereYpos = ViewHelper.getY(mllWhere);
Log.v(TAG, "OldYWhereLLpos: " + oldllWhereYpos);
animate(mllWhere).setDuration(ANIM_DURATION).y(25.0f);
mBringToFrontView(mllWhere);
}
}
}
});
This is the code to reset the position of all my edittexts
private void resetPositionViews() {
closeKeyBoard();
if (mAlphaFrameLayout != null && ViewHelper.getAlpha(mAlphaFrameLayout) > 0.0f) {
animate(mAlphaFrameLayout).setDuration((7 / 4) * ANIM_DURATION).alpha(0.0f);
mBringToFrontView(mAlphaFrameLayout);
}
if (ViewHelper.getY(mllWho) == 25.0f && mEtWho.isFocused() && mllWhoHasBeenClicked) {
Log.i(TAG, "mEtWho has the focus");
Log.i(TAG, "Back to oldllWhoYpos: " + oldllWhoYpos);
animate(mllWho).setDuration(ANIM_DURATION).y(oldllWhoYpos);
mllWhoHasBeenClicked = false;
}
if (ViewHelper.getY(mllWhere) == 25.0f && mEtWhere.isFocused() && mllWhereHasBeenClicked) {
Log.i(TAG, "mEtWho has the focus");
Log.i(TAG, "Back to oldllWhoYpos: " + oldllWhereYpos);
animate(mllWhere).setDuration(ANIM_DURATION).y(oldllWhereYpos);
mllWhereHasBeenClicked = false;
}
}
public void onClick(View v) {
//this contains 4,095 if conditions
}
The code is inexecutable because it says:
Multiple markers at this line
-implements android.view.View.OnClickListener.onClick
-The code of method onClick(View) is exceeding the 65535 bytes limit
Can anyone help me solve this one? because i actually need those 4,095 if conditions in my code.
If you want to keep the 4095 if statements then I'd do something like this:
#Override
public void onClick(View v) {
if (! processPart1(v)) {
processPart2(v);
}
}
private boolean processPart1(View v) {
if (q1.isChecked() && !q2.isChecked() && !q3.isChecked() && !q4.isChecked() && !q5.isChecked() && !q6.isChecked() && !q7.isChecked() && !q8.isChecked() && !q9.isChecked() && !q10.isChecked() && !q11.isChecked() && !q12.isChecked()) {
// do your thing
return true;
}
else if (!q1.isChecked() && q2.isChecked() && !q3.isChecked() && !q4.isChecked() && !q5.isChecked() && !q6.isChecked() && !q7.isChecked() && !q8.isChecked() && !q9.isChecked() && !q10.isChecked() && !q11.isChecked() && !q12.isChecked()) {
// do your thing
return true;
}
// more conditions here...
return false;
}
private void processPart2(View v) {
// more conditions
}
However you can implement this much faster and also easier to modularize:
boolean q[] = {q1.isChecked(), q2.isChecked(), q3.isChecked(),
q4.isChecked(), q5.isChecked(), q6.isChecked(),
q7.isChecked(), q8.isChecked(), q9.isChecked(),
q10.isChecked(), q11.isChecked(), q12.isChecked()};
int value = 0;
for (int i = 0, len = q.length; i < len; i++) {
value += (q[i] ? 1 : 0) << i;
}
switch(value) {
case 0: // do your thing
case 1: // do your thing
case 2: // do your thing
// more case statements ...
}
While your code has to go through 2048 if statements in average the code above processes merely 12 values and then jumps directly to the correct case statement. It's also much less error prone and can be split into different methods easily like this:
processValues0To1023(value);
processValues1024To2047(value);
processValues2048To3071(value);
processValues3072To4095(value);
private void processValues0To1023(int value) {
switch(value) {
case 0: // do your thing
// more case statements ...
}
}
private void processValues1024To2047(int value) {
switch(value) {
case 1024: // do your thing
// more case statements ...
}
}
private void processValues2048To3071(int value) {
// ditto
}
private void processValues3072To4095(int value) {
// ditto
}