Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to play around with some ideas and develop a soft keyboard for Android to replace the default one.
Is there any general information about soft keyboard development for Android out there? Any best practices or guidelines?
Can I do with my keyboard application pretty much anything I could do with a normal Android application?
Can I do HTTP connections to synchronize keyboard data with a cloud DB and other phones I have?
Can I open other windows/screens from a key press, e.g. to display a custom input interface different to a normal QWERTY one. If that doesn't work, can I use a pop-up dialog instead?
Some tips:
Read this tutorial: Creating an Input Method
clone this repo: LatinIME
About your questions:
An inputMethod is basically an Android Service, so yes, you can do HTTP and all the stuff you can do in a Service.
You can open Activities and dialogs from the InputMethod. Once again, it's just a Service.
I've been developing an IME, so ask again if you run into an issue.
Create Custom Key Board for Own EditText
Download Entire Code
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);
}
}
A good place to start is the sample application provided on the developer docs.
Guidelines would be to just make it as usable as possible. Take a look at the others available on the market to see what you should be aiming for
Yes, services can do most things, including internet; provided you have asked for those permissions
You can open activities and do anything you like n those if you run into a problem with doing some things in the keyboard. For example HTC's keyboard has a button to open the settings activity, and another to open a dialog to change languages.
Take a look at other IME's to see what you should be aiming for. Some (like the official one) are open source.
first of all you should define an .xml file and make keyboard UI in it:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="12.50%p"
android:keyHeight="7%p">
<!--
android:horizontalGap="0.50%p"
android:verticalGap="0.50%p"
NOTE When we add a horizontalGap in pixels, this interferes with keyWidth in percentages adding up to 100%
NOTE When we have a horizontalGap (on Keyboard level) of 0, this make the horizontalGap (on Key level) to move from after the key to before the key... (I consider this a bug)
-->
<Row>
<Key android:codes="-5" android:keyLabel="remove" android:keyEdgeFlags="left" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="55006" android:keyLabel="clear" />
</Row>
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
</Row>
<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
</Row>
In this example you have 4 rows and in each row you have 3 keys.
also you can put an icon in each key you want.
Then you should add xml tag in your activity UI like this:
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="visible" />
Also in your .java activity file you should define the keyboard and assign it to a EditText:
CustomKeyboard mCustomKeyboard1 = new CustomKeyboard(this,
R.id.keyboardview1, R.xml.horizontal_keyboard);
mCustomKeyboard1.registerEditText(R.id.inputSearch);
This code asign inputSearch (which is a EditText) to your keyboard.
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class CustomKeyboard {
/** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {#link #mKeyboardView}. */
private Activity mHostActivity;
/** The key (code) handler. */
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL
public final static int CodePrev = 55000;
public final static int CodeAllLeft = 55001;
public final static int CodeLeft = 55002;
public final static int CodeRight = 55003;
public final static int CodeAllRight = 55004;
public final static int CodeNext = 55005;
public final static int CodeClear = 55006;
#Override
public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml
// file; all codes come in keyCodes, the first in this list in
// primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if (focusCurrent == null
|| focusCurrent.getClass() != EditText.class)
return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Apply the key to the edittext
if (primaryCode == CodeCancel) {
hideCustomKeyboard();
} else if (primaryCode == CodeDelete) {
if (editable != null && start > 0)
editable.delete(start - 1, start);
} else if (primaryCode == CodeClear) {
if (editable != null)
editable.clear();
} else if (primaryCode == CodeLeft) {
if (start > 0)
edittext.setSelection(start - 1);
} else if (primaryCode == CodeRight) {
if (start < edittext.length())
edittext.setSelection(start + 1);
} else if (primaryCode == CodeAllLeft) {
edittext.setSelection(0);
} else if (primaryCode == CodeAllRight) {
edittext.setSelection(edittext.length());
} else if (primaryCode == CodePrev) {
View focusNew = edittext.focusSearch(View.FOCUS_BACKWARD);
if (focusNew != null)
focusNew.requestFocus();
} else if (primaryCode == CodeNext) {
View focusNew = edittext.focusSearch(View.FOCUS_FORWARD);
if (focusNew != null)
focusNew.requestFocus();
} else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
#Override
public void onPress(int arg0) {
}
#Override
public void onRelease(int primaryCode) {
}
#Override
public void onText(CharSequence text) {
}
#Override
public void swipeDown() {
}
#Override
public void swipeLeft() {
}
#Override
public void swipeRight() {
}
#Override
public void swipeUp() {
}
};
/**
* Create a custom keyboard, that uses the KeyboardView (with resource id
* <var>viewid</var>) of the <var>host</var> activity, and load the keyboard
* layout from xml file <var>layoutid</var> (see {#link Keyboard} for
* description). Note that the <var>host</var> activity must have a
* <var>KeyboardView</var> in its layout (typically aligned with the bottom
* of the activity). Note that the keyboard layout xml file may include key
* codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the
* {#link #registerEditText(int)}.
*
* #param host
* The hosting activity.
* #param viewid
* The id of the KeyboardView.
* #param layoutid
* The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity = host;
mKeyboardView = (KeyboardView) mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview
// balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/**
* Make the CustomKeyboard visible, and hide the system keyboard for view v.
*/
public void showCustomKeyboard(View v) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if (v != null)
((InputMethodManager) mHostActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the
* hosting activity) for using this custom keyboard.
*
* #param resid
* The resource id of the EditText that registers to the custom
* keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext = (EditText) mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom
// keyboard when the edit box gets focus, but also hide it when the
// edit box loses focus
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showCustomKeyboard(v);
else
hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener() {
// NOTE By setting the on click listener, we can show the custom
// keyboard again, by tapping on an edit box that already had focus
// (but that had the keyboard hidden).
#Override
public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way:
// 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a
// cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard
// keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType()
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
// NOTE How can we change the background color of some keys (like the
// shift/ctrl/alt)?
// NOTE What does android:keyEdgeFlags do/mean
Related
Ok. These are my problems.
I need to user regular expressions to filet out everything except for letters and then I need to encase the found words within a $word tag.
With this str = str.replaceAll(pattern, "$0");.
right now I am filtering all of the right elements (punctuation, numbers etc) but its encasing every letter within each word in an a tag not the word. so how do I use the regular expression to group the letters to a word?
from "(a tag open)t(a close)(a tag open)h(a close)(a tag open)i(a close)(a tag open)s(a close) (a tag open)i(a close)(a tag open)s(a close) (a tag open)w(a close)(a tag open)r(a close)(a tag open)o(a close)(a tag open)n(a close)(a tag open)g(a close)";
to :
"(a tag open)This(a close) (a tag open)is(a close) (a tag open)right(a close)";
then I'm making them clickable and I need to catch the click event and get the position on screen on the clicked word as I want to use the clicked event to make a tool tip show up just below the clicked word. thank you for your help.
public class MainActivity extends Activity {
public String text = "This is just a sentence to test you. 23 this is another number23!g?";
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = explode(text);
tv = (TextView) findViewById(R.id.tv1);
tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());
Spanned article = Html.fromHtml(text, null, null);
setHTML(article);
}
public void setHTML(Spanned html) {
SpannableString message = new SpannableString(html.toString());
Object[] spans = html.getSpans(0, html.length(), Object.class);
for (Object span : spans) {
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);
int flags = html.getSpanFlags(span);
if (span instanceof URLSpan) {
URLSpan urlSpan = (URLSpan) span;
span = new CallbackSpan(urlSpan.getURL());
}
message.setSpan(span, start, end, flags);
}
tv.setText(message);
}
public String explode(String str){
String pattern = "([a-zA-Z])";
str = str.replaceAll(pattern, "$0");
return str;
}
private final class CallbackSpan extends ClickableSpan {
private String m_data;
private String url_main;
public CallbackSpan(String url) {
m_data = url.substring(0);
url_main = url;
}
public void onClick(View view) {
TextView item = (TextView)findViewById(R.id.tv2);
item.setText(url_main + " was clicked.");
Log.d("item", url_main);
}
}
}
Latest code ,pls see link form github
message.setSpan(span, start, end, flags);
You need remove origin span before set new span.
Please see below
The onClick() of ClickableSpan is not working for URLSpan?
EDIT
You can capture any span click event by extends LinkMovementMethod
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;
import java.lang.ref.WeakReference;
public class LinkMovementMethodExt extends LinkMovementMethod {
public static final int LinkMovementMethod_Down = 1001;
public static final int LinkMovementMethod_Up = 2002;
private static LinkMovementMethod sInstance;
private Class mSpanClass = null;
private WeakReference<Handler> mWeakReference = null;
public static MovementMethod getInstance(Handler handler, Class spanClass) {
if (sInstance == null) {
sInstance = new LinkMovementMethodExt();
((LinkMovementMethodExt) sInstance).mWeakReference = new WeakReference<>(handler);
((LinkMovementMethodExt) sInstance).mSpanClass = spanClass;
}
return sInstance;
}
#Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
/**
* get you interest span
*/
Object[] spans = buffer.getSpans(off, off, mSpanClass);
if (spans.length != 0) {
if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0]));
MessageSpan obj = new MessageSpan();
obj.setObj(spans);
obj.setView(widget);
Handler handler = mWeakReference.get();
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Down;
message.sendToTarget();
return true;
}
return false;
} else if (action == MotionEvent.ACTION_UP) {
Handler handler = mWeakReference.get();
if (handler != null) {
MessageSpan obj = new MessageSpan();
obj.setView(widget);
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Up;
message.sendToTarget();
return true;
}
return false;
}
}
}
return super.onTouchEvent(widget, buffer, event);
}
public boolean canSelectArbitrarily() {
return true;
}
public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode,
KeyEvent event) {
return false;
}
textView.setMovementMethod(LinkMovementMethodExt.getInstance());
Edit for "android developer"
It is better way to add Handler property for LinkMovementMethodExt class.
You can capture all Spanned which are delivered as Message object.
Snip code in onTouchEvent method:
Message message = Handler.obtainMessage();
message.obj = buffer.getSpans(off, off, Spanned.class);//get all spanned
message.what = 111;//which value ,it is up to you
message.sendToTarget(); //send message to you target handler
You can handler expected spanned in you handler class. May be it is flexible way to handle .
Hope to help you.
Above textview text is <a href='/a'>aaaa</a>123456<a href='/b'>bbbb</b>7890
I understand you requirement : Click 'aaaa',you want get its href value '/a', click 'bbbb',get its href '/b'; Do not trigger default action which is opened in web browser.
If my understand is right, you can do like this:
Set LinkMovementMethod for textview, etc:textview.setMovementMethod(LinkMovementMethodExt.getInstance(handler, URLSpan.class));
Get interest span, here is URLSpan.
In you handler handleMessage method, you can do like this:
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
int what = msg.what;
if (what == 100) {
Object[] spans = (Object[])msg.obj;
for (Object span : spans) {
if (span instanceof URLSpan) {
System.out.println(((URLSpan) span).getURL());
}
}
}
};
};
Download demo code
MainActivity has color property which you can assign which color value as you like.
How to do?
Step1, get current click span.
Step2, set BackgroundColorSpan for current click span
Assuming You want click individual words on the textview?. I din't understand the converts all letters to words in your comment.
The below can be used to click on individual works and it is displayed in a toast.
public class MainActivity extends Activity {
TextView _tv;
String[] each;
SpannableString ss1;
Button b;
EditText et;
String s;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.ed);
_tv = (TextView) findViewById( R.id.tv );
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
s=et.getText().toString();
_tv.setText("");
for(int i=0;i<s.length();i++)
{
each = s.split("\\s+");
}
for(int i=0;i<each.length;i++)
{
System.out.println("................"+each[i]);
ss1= new SpannableString(each[i]);
//StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
//spannable.setSpan( boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
ss1.setSpan(new MyClickableSpan(each[i]), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_tv.append(ss1);
_tv.append(" ");
}
_tv.setMovementMethod(LinkMovementMethod.getInstance());
}
});
}
class MyClickableSpan extends ClickableSpan{
String clicked;
public MyClickableSpan(String string) {
// TODO Auto-generated constructor stub
clicked =string;
}
//clickable span
public void onClick(View textView) {
//do something
Toast.makeText(MainActivity.this,clicked ,
Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLACK);//set text color
ds.setUnderlineText(false); // set to false to remove underline
}
}
}
I've been following the Custom Keyboard example to make the following:
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.text.InputType;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* When an activity hosts a keyboardView, this class allows several EditText's to register for it.
*
* #author Maarten Pennings
* #date 2012 December 23
*/
public class CustomKeyboard {
private static final String TAG = "[CustomKeyboard]: ";
/** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {#link #mKeyboardView}. */
private Activity mHostActivity;
/** The key (code) handler. */
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL
public final static int CodePrev = 55000;
public final static int CodeAllLeft = 55001;
public final static int CodeLeft = 55002;
public final static int CodeRight = 55003;
public final static int CodeAllRight = 55004;
public final static int CodeNext = 55005;
public final static int CodeClear = 55006;
#Override public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml file; all codes come in keyCodes, the first in this list in primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if( focusCurrent==null || focusCurrent.getClass()!=EditText.class ) return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
Log.d(TAG,"Pressed");
// Apply the key to the edittext
if( primaryCode==CodeCancel ) {
hideCustomKeyboard();
} else if( primaryCode==CodeDelete ) {
if( editable!=null && start>0 ) editable.delete(start - 1, start);
} else if( primaryCode==CodeClear ) {
if( editable!=null ) editable.clear();
} else if( primaryCode==CodeLeft ) {
if( start>0 ) edittext.setSelection(start - 1);
} else if( primaryCode==CodeRight ) {
if (start < edittext.length()) edittext.setSelection(start + 1);
} else if( primaryCode==CodeAllLeft ) {
edittext.setSelection(0);
} else if( primaryCode==CodeAllRight ) {
edittext.setSelection(edittext.length());
} else if( primaryCode==CodePrev ) {
View focusNew= edittext.focusSearch(View.FOCUS_LEFT);
if( focusNew!=null ) focusNew.requestFocus();
} else if( primaryCode==CodeNext ) {
View focusNew= edittext.focusSearch(View.FOCUS_RIGHT);
if( focusNew!=null ) focusNew.requestFocus();
} else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
Log.d(TAG,"INPUT is this: "+Character.toString((char)primaryCode));
}
}
#Override public void onPress(int primaryCode) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml file; all codes come in keyCodes, the first in this list in primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if( focusCurrent==null || focusCurrent.getClass()!=EditText.class ) return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
Log.d(TAG,"Pressed");
// Apply the key to the edittext
if( primaryCode==CodeCancel ) {
hideCustomKeyboard();
} else if( primaryCode==CodeDelete ) {
if( editable!=null && start>0 ) editable.delete(start - 1, start);
} else if( primaryCode==CodeClear ) {
if( editable!=null ) editable.clear();
} else if( primaryCode==CodeLeft ) {
if( start>0 ) edittext.setSelection(start - 1);
} else if( primaryCode==CodeRight ) {
if (start < edittext.length()) edittext.setSelection(start + 1);
} else if( primaryCode==CodeAllLeft ) {
edittext.setSelection(0);
} else if( primaryCode==CodeAllRight ) {
edittext.setSelection(edittext.length());
} else if( primaryCode==CodePrev ) {
View focusNew= edittext.focusSearch(View.FOCUS_LEFT);
if( focusNew!=null ) focusNew.requestFocus();
} else if( primaryCode==CodeNext ) {
View focusNew= edittext.focusSearch(View.FOCUS_RIGHT);
if( focusNew!=null ) focusNew.requestFocus();
} else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
Log.d(TAG,"INPUT is this: "+Character.toString((char)primaryCode));
}
}
#Override public void onRelease(int primaryCode) {
}
#Override public void onText(CharSequence text) {
}
#Override public void swipeDown() {
}
#Override public void swipeLeft() {
}
#Override public void swipeRight() {
}
#Override public void swipeUp() {
}
};
/**
* Create a custom keyboard, that uses the KeyboardView (with resource id <var>viewid</var>) of the <var>host</var> activity,
* and load the keyboard layout from xml file <var>layoutid</var> (see {#link Keyboard} for description).
* Note that the <var>host</var> activity must have a <var>KeyboardView</var> in its layout (typically aligned with the bottom of the activity).
* Note that the keyboard layout xml file may include key codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the {#link #registerEditText(int)}.
*
* #param host The hosting activity.
* #param viewid The id of the KeyboardView.
* #param layoutid The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity= host;
mKeyboardView= (KeyboardView)mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/** Make the CustomKeyboard visible, and hide the system keyboard for view v. */
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) ((InputMethodManager)mHostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the hosting activity) for using this custom keyboard.
*
* #param resid The resource id of the EditText that registers to the custom keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext= (EditText)mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom keyboard when the edit box gets focus, but also hide it when the edit box loses focus
#Override public void onFocusChange(View v, boolean hasFocus) {
if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener() {
// NOTE By setting the on click listener, we can show the custom keyboard again, by tapping on an edit box that already had focus (but that had the keyboard hidden).
#Override public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way: 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
// NOTE How can we change the background color of some keys (like the shift/ctrl/alt)?
// NOTE What does android:keyEdgeFlags do/mean
with MainActivity containing the following:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "[MainActivity]:";
EditText expressionEditText;
CustomKeyboard mCustomKeyboard;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomKeyboard = new CustomKeyboard(this, R.id.keyboardview, R.xml.calculatorkbd);
Log.d(TAG,"Init the keyboard");
expressionEditText = (EditText)findViewById(R.id.expressionEditText);
Log.d(TAG,"Found the EditText");
mCustomKeyboard.registerEditText(R.id.expressionEditText);
Log.d(TAG,"Registered with the keyboard");
}
#Override
public void onBackPressed() {
if( mCustomKeyboard.isCustomKeyboardVisible() )
mCustomKeyboard.hideCustomKeyboard();
else this.finish();
}
}
Now, the problem is when I press a key on the custom keyboard, the cursor in the EditText is not moving, and the EditText itself is empty. I've read somewhere here that manufacturers are no longer required to propagate such events, or something to that effect. Is that the problem in my case? If not, what is?
The crux was in the line focusCurrent.getClass()!=EditText.class
As it turns out, the class is something like AppCompatEditText.
I remember someone at SO issuing a warning about this in comments. Now everything works.
Good day All
I am struggling with this problem, I am not sure if I'm missing something obvious but the case statement/sorting method fails to return the correct result.
In short, the program recieves an input from a EditText (edtInput) and assigns it to
string temp in isValidNumeericSequence(string temp) {...}
where it gets tested for
1. if temp is empty
2. first character is a numeric value
3. the string contains anything except numbers and/or operators (operators specified in arrOperators)
The result from toast always refers to error_message 2, no matter what characters in the string, it always stays by error_message 2
IF I change the (temp == "" ) to (temp != ""), then it changes and sticks to error_message 1...
this might not be the most optimal method of calculating a equation, but its a simple start for me
p.s. If anything seems/looks abnormal, or irregular use of functions, please point them out, I would appreciate it...
I have tried debugging, but I seem to be missing something.
java file:
package com.cyberstudios.interpreter.interpreter;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.Toast.*;
public class MainForm extends Activity {
private TextView lblAnswer, showText;
private EditText edtInput;
public Button btnShowHint, btnCalculate, btnClose;
public String [] arrEditStore;
public String arrOperators [] = {"+", "-", "*", "/", "(", ")"} ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_form);
edtInput = (EditText) findViewById(R.id.edtInput) ;
lblAnswer = (TextView) findViewById(R.id.lblAnswer) ;
btnShowHint = (Button) findViewById(R.id.btnHelp);
btnCalculate = (Button) findViewById(R.id.btnShow) ;
}
public void calculate(View vw)
{
String temp = edtInput.toString(), error_message = "";
//--------------------------------------ERROR HANDLER----------------------------------------------------------
switch (isValidNumericSequence(temp)) {
case 0:
sortInput(temp);
break;
case 1:
error_message = "Dumb Shit, enter a sum, want me to read your mind?" ;
break;
case 2:
error_message = "Think I'm an idiot, I need a number to start with, not a sign or a letter";
break;
case 3:
error_message = "Moron!!! Are we doing word puzzles or character drawing, enter numbers and operators, not letters and punctuation, its not English class!" ;
break;
}
makeText(this, error_message, LENGTH_SHORT).show();
//-------------------------------------------------------------------------------------------------------------
arrEditStore = new String[edtInput.length()] ;
lblAnswer.setText("No Answer");
// applyBODMAS();
/* double answer = CalcFromArray();
lblAnswer.setText(String.valueOf(answer));*/
}
/* private double CalcFromArray() {
return 0;
}*/
/*private void applyBODMAS()
{
for (int k = 1; k <= arrEditStore.length; k++)
{
arrEditStore[k] = "0";
}
}*/
private int isValidNumericSequence(String temp)//checks if string contains any digits
{
// Works with error codes, error code will be updated to be more user friendly
// by reporting the position of errors:
// Code 0 : No error
// Code 1 : Empty String
// Code 2 : First character must be of numeric type
// Code 3 : There is an invalid character in the sequence
makeText(this, "this is edtInput text - " + edtInput.toString(), LENGTH_SHORT).show(); ;
makeText(this, "this is temp text - " + temp, LENGTH_SHORT).show(); ;
if (temp == "") {
return 1;
}else {
if (!Character.isDigit(temp.charAt(0)))
return 2;
for (int x = 1; x <= temp.length(); x++) {
for (int k = 1; k <= arrOperators.length; k++) {
if (temp.charAt(x) != arrOperators.toString().charAt(k) || temp.charAt(x) >= 9 || temp.charAt(x) <= 0)
return 3;
}
}
}
return 0 ;
}
private void sortInput(String temp)
{
int x,y, count = 0;
for (x=1; x<temp.length()+1; x=x+1)
{
for (y=0;y<= arrOperators.length;y=y+1)
{
if(temp.substring(x,x+1).equals(arrOperators[y]))
{
arrEditStore[count++] = temp.substring(0, x-1);
arrEditStore[count++] = arrOperators[y] ;
}
}
}
}
public void _showHint(View vw)
{
final Dialog showHintDialog = new Dialog(this);
showHintDialog.setContentView(R.layout.custom_dialog);
showHintDialog.setTitle("How to enter data");
showHintDialog.show();
btnClose = (Button) showHintDialog.findViewById(R.id.button) ;
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showHintDialog.dismiss();
}
});
}
#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_form, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I dont believe the logcat or any xml files are nessassary, but if it needs be, I can post them
Thanx in advance
Thank you too #oguzhand and #Chris Stratton for the comments
edtInput.toString() is totally wrong!!! you need to use
edtInput.getText().toString() You are using edittext totally wrong...
Read this:
developer.android.com/reference/android/widget/EditText.html –
oguzhand
and
Indeed #oguzhand seems to have identified a primary issue, you are
using what is basically a "handy name" of the editText object rather
than the contents of its text field. – Chris Stratton
I simply replaced any edtInput.toString() with edtInput.getText().toString() and it worked perfectly
Thank you
I'm currently working on developing a custom keyboard app that will be optimized for a device using a DPad as its primary input device.
My problem is that When the cursor is in the EditText field and you press down (e.g. KEYCODE_DPAD_DOWN), the keyboard view is not receiving focus and KeyEvents. Either nothing happens, or the element beneath the EditText in question receives focus.
Below is the relevant code.
Any help would be much appreciated. I've tried dissecting the SoftKeyboard example aswell as KeyboardView.java for hints without success.
Thanks,
Bryan
MyKeyboard.java
public class MyKeyboard extends InputMethodService {
private static final String TAG = "MyKeyboard";
private MyKeyboardView mInputView = null;
#Override public void onCreate() {
super.onCreate();
}
#Override public View onCreateInputView() {
mInputView = (MyKeyboardView) getLayoutInflater().inflate(R.layout.input, null);
// attempts to make this focusable
mInputView.setClickable(true);
mInputView.setFocusableInTouchMode(true);
mInputView.setFocusable(true);
mInputView.setEnabled(true);
return mInputView;
}
#Override public View onCreateCandidatesView() {
super.onCreateCandidatesView();
return null;
}
#Override public void onStartInputView(EditorInfo info, boolean restarting) {
super.onStartCandidatesView(info, restarting);
}
#Override public void onFinishInput() {
super.onFinishInput();
}
#Override public void onDestroy() {
super.onDestroy();
}
}
MyKeyboardView.java
public class MyKeyboardView extends TableLayout implements View.OnClickListener, View.OnFocusChangeListener {
private static final String TAG = "MyKeyboardView";
private ArrayList<Character> charList = new ArrayList<Character>();
public MyKeyboardView(Context context) {
super(context);
populateKeyboard();
this.setOnFocusChangeListener(this);
this.setOnClickListener(this);
}
public MyKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
populateKeyboard();
this.setOnFocusChangeListener(this);
this.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
Log.d(TAG, "onClick");
}
private void populateKeyboard() {
charList.add(new Character(','));
charList.add(new Character('.'));
charList.add(new Character('?'));
charList.add(new Character('<'));
charList.add(new Character('>'));
charList.add(new Character((char) 0x2798)); // arrow
charList.add(new Character((char) 0x2798)); // arrow
charList.add(new Character((char) 0x2798)); // arrow
charList.add(new Character((char) 0x005F)); // underscore
for(char c = '#'; c < 'Z'; c++) {
charList.add(new Character(c));
Log.d(TAG, "char: " + c);
}
TableRow tr = null;
for(int i=0; i<charList.size(); i++) {
if(i % 7 == 0) {
if(tr != null)
this.addView(tr);
tr = new TableRow(this.getContext());
tr.setGravity(Gravity.CENTER_HORIZONTAL);
}
TextView tv = new TextView(this.getContext());
tv.setPadding(21, 2, 21, 2);
tv.setText(charList.get(i).toString());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22);
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setFocusable(true);
tv.setEnabled(true);
tr.addView(tv);
}
if(tr.getChildCount() > 0)
this.addView(tr);
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(TAG, "mInputView onFocusChange " + (hasFocus ? "true" : "false"));
}
}
input.xml
<?xml version="1.0" encoding="utf-8"?>
<com.weirdtuesday.mykeyboard.MyKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF000000"
android:focusable="true" />
Key events must be processed manually in the onKey method. To move the cursor, I use this:
if (primaryCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
int position = connection.getTextBeforeCursor(Integer.MAX_VALUE, 0)
.length();
final CharSequence selected = connection.getSelectedText(0);
if (selected != null)
connection.commitText(
mComposing.substring(0,
mComposing.length() - selected.length()), 1);
else
connection.commitText(mComposing, 1);
connection.setSelection(position + 1, position + 1);
}
I have a project for an Android class, so I'm still learning and this should be a basic question. We were given a tip calculator and already made some modifications, now we have to add a menu.
When it starts up, it will be in multi-person mode. Gives a text box and Text Field for how many people you want the bill split into. When you hit menu, it should show a Single person mode which eliminates a text box and text field. The menu then changes to show a multi-person mode button in the menu.
I've got everything to work except it's showing both buttons, I cannot figure out how to hide a button temporarily. The main error is:
Cannot invoke setVisibility(int) on the primitive type int
on the statement:
multiple_button.setVisibility(View.GONE);
I've tried every combination of hiding the button I can think of, and think that the above line is correct, but unsure of how make it work.
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
I have this in the code, but it's not doing anything either.
Any help would be greatly appreciated.
edit: code. I've read through the link, but considering I don't have a OnPrepareOptions section, I need to re-read it
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class tipcalc extends Activity
{
public static int one_person_button = Menu.FIRST;
private int multiple_button = Menu.FIRST +1;
static final private int reset_button = Menu.FIRST +2;
private static final int MENU_ITEM = 0;
private EditText txtbillamount;
private EditText txtpeople;
private EditText txtpercentage;
private TextView txtperperson;
private TextView txttipamount;
private TextView txttotal;
private Button btncalculate;
private Button btnreset;
private double billamount = 0;
private double percentage = 0;
private double numofpeople=0;
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;
private View view;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem itemOne = menu.add(0, one_person_button, Menu.NONE,
R.string.one_person);
MenuItem itemMultiple = menu.add(1, multiple_button, Menu.NONE,
R.string.multiple);
MenuItem itemReset = menu.add(2, reset_button, Menu.NONE,
R.string.reset);
itemOne.setIcon(R.drawable.ic_menu_invite);
itemMultiple.setIcon(R.drawable.ic_menu_allfriends);
itemReset.setIcon(R.drawable.ic_menu_refresh);
one_person_button.setGroupVisible(0, true);
multiple_button.setVisibility(View.GONE);
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (one_person_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.INVISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.INVISIBLE) ;
multiple_button = View.VISIBLE;
one_person_button = View.GONE;
numofpeople = 1; }
else if (multiple_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.VISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.VISIBLE) ;
multiple_button = View.GONE;
one_person_button = View.VISIBLE;
}
return false;
}
private void initControls()
{
txtbillamount = (EditText)findViewById(R.id.txtbillamount);
txtpeople = (EditText)findViewById(R.id.txtpeople);
txtperperson=(TextView)findViewById(R.id.txtperperson);
txttipamount=(TextView)findViewById(R.id.txttipamount);
txttotal=(TextView)findViewById(R.id.txttotal);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calculate()
{
billamount=Double.parseDouble(txtbillamount.getText().toString());
numofpeople=Double.parseDouble(txtpeople.getText().toString());
RadioButton poorButton = (RadioButton) findViewById(R.id.radioButton1);
RadioButton goodButton = (RadioButton) findViewById(R.id.radioButton2);
RadioButton excellentButton = (RadioButton) findViewById(R.id.radioButton3);
if (poorButton.isChecked()){
percentage = Double.parseDouble(poorButton.getText().toString());
} else if (goodButton.isChecked()){
percentage = Double.parseDouble(goodButton.getText().toString());
} else if (excellentButton.isChecked()){
percentage = Double.parseDouble(excellentButton.getText().toString());
}
tipamount=(billamount*percentage)/100;
totaltopay=billamount+tipamount;
perperson=totaltopay/numofpeople;
txttipamount.setText(Double.toString(tipamount));
txttotal.setText(Double.toString(totaltopay));
txtperperson.setText(Double.toString(perperson));
}
private void reset()
{
txtbillamount.setText("");
txtpeople.setText("");
txtperperson.setText("");
txttipamount.setText("");
txttotal.setText("");
}
}
Post all of your relavent source code. Without it, we cannot give you specific advice about what is going wrong.
I can tell you though you'll be needing to override onPrepareOptionsMenu() and inside there you'll want to check which mode your in and make the proper button be visible. But you need to call setVisibility(View.VISIBLE); on a reference to the button widget, not on an int.
This page holds the answer to your questions.
try calling setVisibility with 8