How to create buttons like keyboard keys in android? <p>This question seems trivial. I want to create a button like keyboard keys for my app. When I click on it, a popup window appears above that button showing the letter pressed. Everything works great till now except one thing. When I add onFocusChangedListener to the button, nothing happens. I need to let my button act as a keyboard key, but I don't know how.</p> <p><a href="https://i.stack.imgur.com/Zq2Ny.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zq2Ny.png" alt="Keyboard Button Example"></a></p> <p>As you can see here, when a button is focused, a popup window appears. I want to do that, but <code>onFocusChangeListener</code> doesn't work. I know I can use a <code>KeyboardView</code> to achieve that, but I don't want to use that due to some other issues like centering buttons and setting keys' height with layout_weight. So I need to make it with normal buttons.</p> <h1>What I tried:</h1> <p><strong>My First Try:</strong></p> <pre><code>button.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight()); } else { popupWindow.dismiss(); } } }); </code></pre> <p><strong><em>Result:</em></strong> Nothing happened. The popup window didn't appear at all.</p> <p><strong><em>Edit:</em></strong> After I have added <code>button.setFocusableInTouchMode(true);</code> as Ashley suggested, onFocusChanged is now getting called, but it acts so weird. The popup is <strong>sometimes</strong> shown, but at the same time when it is shown, it never disappears...</p> <p><strong>My Second Try:</strong></p> <pre><code>button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight()); break; case MotionEvent.ACTION_UP: popupWindow.dismiss(); break; } return true; } }); </code></pre> <p><strong><em>Result:</em></strong> This one acted so weird. Sometimes the popup shows and sometimes not, but when it is shown, the button didn't also change its state. It should have been focused, but nothing happened to the button, it acts as if it was in a normal state (Button's background doesn't change with state_focused declared in my drawable xml). It seems that onTouchListener overrides the button's functionality.</p> <p><strong>Here is a part of my layout:</strong></p> <pre><code>&lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="3"&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="Q" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="W" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="E" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="R" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="T" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="Y" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="U" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="I" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="O" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="P" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"&gt; &lt;View android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="A" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="S" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="D" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="F" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="G" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="H" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="J" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="K" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="L" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;View android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"&gt; &lt;View android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1.5" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="Z" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="X" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="C" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="V" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="B" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="N" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;Button android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:text="M" android:background="@drawable/keyboard_button" android:textColor="#FFFFFF" android:onClick="onKeyboardClick" /&gt; &lt;View android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1.5" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>In code:</strong></p> <pre><code>public void onKeyboardClick(View view) { //The view pressed is a button. final Button button = (Button) view; //Create a PopupWindow. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); final View keyboardPopup = inflater.inflate(R.layout.keyboard_popup, null); final PopupWindow popupWindow = new PopupWindow(keyboardPopup, view.getWidth() + 20, view.getHeight()); TextView keyboardKey = (TextView) keyboardPopup.findViewById(R.id.keyboard_key); keyboardKey.setText(button.getText().toString()); //Get button location to show the popup above it. int[] keyLocation = new int[2]; button.getLocationOnScreen(keyLocation); final Rect location = new Rect(); location.left = keyLocation[0]; location.top = keyLocation[1]; location.right = location.left + button.getWidth(); location.bottom = location.top + button.getHeight(); //This is a temporary solution. I don't want to use that. button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Show popup. popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight()); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //Dismiss popup. popupWindow.dismiss(); } }, 200); } }); } </code></pre> <p>Any help will be greatly appreciated. Thanks.</p>