Image related to the Problem: Multi-Textboxes
Situation:There are two textboxes. There is one 'Speak Now' imageButton. Whenever I touch 'Speak Now' button, I wish my text would be written to the textbox where cursor is blinking.
I did this easily for one textbox since I didn't have to locate the cursor blink:
if(editText.length()==0)
{
editText.setText(result.get(0));
}
But for two text boxes - I am stuck! Following is code for only one text box.
public void onActivityResult(int request_code, int result_code, Intent intent) {
super.onActivityResult(request_code, result_code, intent);
switch (request_code) {
case 100:
if (result_code == RESULT_OK && intent != null) {
ArrayList<String> result = intent.getStringArrayListExtra
(RecognizerIntent.EXTRA_RESULTS);
//todo:get location of cursor
if (editText.length() == 0) {
editText.setText(result.get(0));
} else {
editText.setText(concatenateText
(editText.getText().toString(),
result.get(0)));
}
}
break;
}
}
If cursor is at upper textBox then setText in textbox_up else setText in textbox_down. Thank you for the help.
What you may need to do is declare a Class global variable of whatever data type you like so as to distinguish which TextBox last had focus (remember, your button selection will take the focus away). In a focusLost() event for each TextBox set a value to this global variable so as to know which TextBox had last focus. Now in your actionPerformed event (or whatever event you're using) for your Button you can easily determine which TextBox last had focus by reading what is contained within your class global variable.
Related
I am trying to program a GUI that uploads a file with a song data base and allows a user to add, edit, or remove songs from this database. Song names appear in a combo box and when a song is selected, the pertinent information appears in un-editable text fields. The interface has buttons for add, edit, delete, accept, cancel, and exit. When either edit or add are selected, text fields become editable and the accept and cancel buttons are enabled. This functionality works okay, but the accept and cancel buttons do not work. When accept is chosen, a song is added, or current song is edited, and added to the combo box and accept and cancel are disabled while the other buttons become enabled and text fields become un-editable. Cancel should perform much the same way, but instead of adding or editing a song, the interface just reverts back to its original state. Below is the code for the actionPerformed class:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int index = songBox.getSelectedIndex();
Song selection = songList.get(index);
Song newSong = new Song();
if (source == songBox) {
itemCodeField.setText(selection.getSongCode());
descriptionField.setText(selection.getSongName());
artistField.setText(selection.getSongArtist());
albumField.setText(selection.getSongAlbum());
priceField.setText(selection.getSongPrice());
}
if (source == addButton) {
//Enable and disable appropriate buttons
addButton.setEnabled(false);
editButton.setEnabled(false);
deleteButton.setEnabled(false);
acceptButton.setEnabled(true);
cancelButton.setEnabled(true);
exitButton.setEnabled(false);
//Clear text fields and make editable
itemCodeField.setText("");
itemCodeField.setEditable(true);
descriptionField.setText("");
descriptionField.setEditable(true);
artistField.setText("");
artistField.setEditable(true);
albumField.setText("");
albumField.setEditable(true);
priceField.setText("");
priceField.setEditable(true);
//Set song values
newSong.setSongCode(itemCodeField.getText());
newSong.setSongName(descriptionField.getText());
newSong.setSongArtist(artistField.getText());
newSong.setSongAlbum(albumField.getText());
newSong.setSongPrice(priceField.getText());
}
if (source == editButton) {
//Enable and disable appropriate buttons
addButton.setEnabled(false);
editButton.setEnabled(false);
deleteButton.setEnabled(false);
acceptButton.setEnabled(true);
cancelButton.setEnabled(true);
exitButton.setEnabled(false);
//Make text fields editable
descriptionField.setEditable(true);
artistField.setEditable(true);
albumField.setEditable(true);
priceField.setEditable(true);
}
if (source == deleteButton) {
songBox.removeItemAt(index);
}
if (source == acceptButton)
{
if (source == addButton)
{
//Add new song to array
songBox.addItem(newSong);
//Enable and disable appropriate buttons
addButton.setEnabled(true);
editButton.setEnabled(true);
deleteButton.setEnabled(true);
acceptButton.setEnabled(false);
cancelButton.setEnabled(false);
exitButton.setEnabled(true);
}
if (source == editButton)
{
//Make text fields uneditable
descriptionField.setEditable(false);
artistField.setEditable(false);
albumField.setEditable(false);
priceField.setEditable(false);
//Set new text
selection.setSongName(descriptionField.getText());
selection.setSongArtist(artistField.getText());
selection.setSongAlbum(albumField.getText());
selection.setSongPrice(priceField.getText());
songBox.addItem(selection);
//Enable and disable appropriate buttons
addButton.setEnabled(true);
editButton.setEnabled(true);
deleteButton.setEnabled(true);
acceptButton.setEnabled(false);
cancelButton.setEnabled(false);
exitButton.setEnabled(true);
}
}
if (source == cancelButton)
{
//Enable and disable appropriate buttons
addButton.setEnabled(true);
editButton.setEnabled(true);
deleteButton.setEnabled(true);
acceptButton.setEnabled(false);
cancelButton.setEnabled(false);
exitButton.setEnabled(true);
//Make text fields uneditable
descriptionField.setEditable(false);
artistField.setEditable(false);
albumField.setEditable(false);
priceField.setEditable(false);
}
if (source == exitButton) {
System.exit(0);
}
}
The program currently compiles and runs. The add and edit buttons do what they intend, but the accept and cancel buttons do not. When chosen, they don't do anything at all. Text fields remain editable and the accept and cancel buttons remain enabled while all other buttons remain disabled.
Update: The cancel button works to make the correct items enabled or disabled, but any changes made are not reset immediately, you have to toggle the combo box.
I understand now that 'source' can't equal two button inputs at the same time. However, the accept button has to do two different things depending on whether the user first selected add or edit, and I don't know how to handle that.
Your code looks like this:
if (source == acceptButton)
{
// Irelevant code removed.
if (source == cancelButton)
{
// This code is newer run, because you require first that source
// is==acceptButton and here you require that source==cancelButton.
}
}
But a better solution, is to have a method for each button, instead of looking at the event source. If you are using java 8(And you should be) have a look at http://www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example
I just want to know how to start an Action (like starting a new Activity) after for example 3 Button clicks. So the Activity will only start if the Button has been clicked for 3 Times.
Keep a counter of the times you've clicked the Button anywhere you like, such as a global variable or if you want it to be cleaner, the Button itself. Add a listener to your button, you can simply add android:onClick="buttonClick" to the xml and then implement a method with that name like so:
public void buttonClick(View yourButton){
if(yourButton.getTag() == null){ //We have no tags, so first click :)
yourButton.setTag(1);
}
if((Integer)yourButton.getTag() == 3){
//Do whatever
}else{
//Increment the value of the tag
yourButton.setTag(((Integer)yourButton.getTag())+1);
}
}
You can maintain a counter .. Increment it after every click... Check if value equals desired value then build intent and start the activity .. Reset the counter before starting the activity.
Java Android question
I have x, say 5, buttons in a row.
Each button has a different number value displayed on the button.
Button one is active, the rest are not- not clickable. They are greyed out.
To show Button 1 is active it fades up and down.
Once clicked the button pops up a message. The user Ok's that, this activates Button 2, and deactivates Button 1.
Then it happens through all buttons, one by one. The final button doesn't produce the pop up message.
My question...
I want to create a method that sets the first button as current and then once clicked sets the next as current, and so on.
Can anyone tell me how to do this? I don't need to know how to fade buttons etc, its literally how to set button as current, and within that method the user click sets the next button as current.
Many thanks in advance.
EDIT
OK, I've had a go...its not working, but it seems so close...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_one);
int[] buttonIds = new int[] {R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5};
setButton(buttonIds);
}
private void setButton(int[] buttId){
int isCurrent = 0;
while(isCurrent < 5) {
Button currentButton = (Button) findViewById(buttId[isCurrent]);
//TODO Make current button pulse
currentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.clearAnimation();
v.setBackgroundColor(0xFF00FF00);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
v.setClickable(false);
setTimer();
isCurrent++;
}
});
I know that the problem is the isCurrent++ is not accessible outside the onClick method. How do I right this? Am I close or is this a major funk up and do I have to rethink?
Just use a global variable which track the current button, and check this variable to identify the current active button for determining the action in onClickListener. To fade out a button try this code snippet
button.setClickable(false);
button.setBackgroundColor(Color.parseColor("#808080"));
You need something like this:
private int activeButton = 1;
private void buttonClickHandler(){
switch(activeButton++){
case : 1
button1.setEnabled(true):
// show popup, hide/animate for button 1
break;
case : 2
button2.setEnabled(true);
// same for button 2
case : 3
// same for button 3
case : 4
// same for button 4
}
In my app, I have an edit text which, when pressed, sets an int value to "1". Obviously, when you press the edit text, a keyboard pops up. The keyboard is dismissed by pressing back, and I have used the following code...
#Override
public void onBackPressed() {
if (editingText == 1) {
editingText = 0;
setUnit();
}
Log.d("Back pressed", "Back was pressed :)");
super.onBackPressed();
}
... to change the value back. setUnit() then changes the value of a TextViewH depending on the contents of the EditText. However, as I have seen by using the logs, this method is only called when the user backs out of the entire activity, and not when the user backs out of the keyboard.
Is there any way to detect "back" presses when the keyboard is displayed? Or, alternatively, is it possible to change a string's value to match the value of the edit text in real-time? All help appreciated.
I have two elements (TextView) in my XML layout that when a LongClick is pressed it will prompt the user to enter in a new value and then when the DONE button is clicked it should show the newly inputed value to the tvScoreHome using setText().
When I do a Long Click on the mentioned element the edit field and keyboard appear as expected. However, it won't allow me to type anything. When I type something it nothing shows up (but the device vibrates as if a button was pressed) and when the DONE button is clicked it vibrates as well but it does not exit the keyboard and show anything in the tvScoreHome element.
Any ideas why?
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText userInput = (EditText) findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
userInput.setVisibility(View.VISIBLE);
imm.showSoftInput(userInput, 0);
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
return true;
}
});
You need to give the user a chance to input some text before copying the data and hiding the EditText.
Remove these two lines from your listener:
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
Perhaps you could use an OnFocusChangeListener to run these two lines when userInput loses focus.