Of course, when doing it programmatically, the first thing we do is call requestFocus() but there are certain cases where the soft keyboard will not show up. The code above will ensure that the soft keyboard will always show when it is called.
When it came to a point wherein I wanted my Button and ImageButton to show an image that it was pressed, most of the forum answers suggested using different image drawable resources for pressed, focused and normal state.
Then I thought to myself, that is pretty much a hassle. I do not like to create a different image for any of those. So I checked the iTouch Facebook app to see how the image looks like when pressed. Nothing special really. It just changed tint while on some buttons, their colors changed.
If you feel the same sentiment as I do by wanting to use only one image as a drawable resource, then you only need to change the tint color of the ImageButton by changing its color filter.
To do this, you need to set the ImageButton an OnTouchListener class. Check the code below.
button.setColorFilter(Color.argb(0,155,155,155));// or null
What this does is whenever you tap on an ImageButton, it changes the image’s filter to a dark grayish bit of color. You can change it to any other color you like. 155, 155, 155 is the RGB value for my gray color.
If you check the first parameter, the values are 150 and 0. 0 means that there should be no color filter applied to it, hence displaying the ImageButton as normal (or you can always use null as the value). The 150 value shows the dark gray color of the ImageButton. 255 is the maximum value.
Easy right? Now you do not need to create separate images for the states.
Simulating input events in any component in Java can be done by using the Robot class. But what if I do not want to use the Robot class? Well, it is possible. The code below simulates an ENTER keypress event. The KeyEvent object created can easily be changed to cover any key event that you want. If you noticed, one of the parameters has a value of -1. This means that neither ALT, SHIFT or CONTROL is included.