There is a method in Java known as
void setActionCommand() and the complementary being
String getActionCommand() to get customized text from JButton / Button (default being text of the button itself).
Now I have switched to android, and converting an app from Java to Android. Is there any method in Android API which does the same thing with the android button, i.e. which can set and get a customized text for a button ?
Thanks !
EDIT : For Non- Swing API / Core Android people:
There are two methods associated with a swing button (JButton) 1. void setText(String s);2. String getText();and when an event occurs on a button, we take the command using object of ActionEvent (Event Listener class) using this methodActionEventObject.getActionCommand()
we also have a method to set this command called setActionCommand(String) which can be called from a button reference Eg.
button1.setText("Option");
button1.setActionCommand("Android");
Now even if button1's text is "Option", in event handling procedure the button can be identified by the string ("Android"). Moreover, the text "Option" is visible to the user while "Android" is not visible to the user and is used for event handling procedures only.
Simply get your button from your method (here assuming you're on an activity class, and use the setText method.
Button mButton=(Button) findViewById(R.id.contact);
mButton.setText("number");
Update
After looking at get/setActionCommand, i think tags are similar, have a look to this answer: https://stackoverflow.com/a/8083855/1766140
Related
By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.
I'm very new at coding Java and Netbeans. So basically, I have a "save" button and three text fields, I want to enable the Button when these three text fields are edited and disable the button when one of them is empty. Also I'm wondering where I should put my codes. Since it's Netbeans I'm only familiar with ActionPerformed methods, there you can set an action when a button is pressed.
If you can keep it simple it would be appreciated!
public project() {
initComponents();
//Here I want the window to appear in the middle of the screen
setLocationRelativeTo(null);
if(txfField1.getText().equals("")){
btnSave.setEnabled(false);
}
else {
btnSave.setEnabled(true);
}
}
I tried with this code on only one of the three text fields and It does not work, the button is always enabled. The button is initially disabled. Additionally I have also tried to put my code below this method:
public class project extends javax.swing.JFrame {
You can use event handlers to change the state of the button. For example, if you have one text field and you want to change the state of the button depending on the data inside the text field, you could use something like
if (!jTextField1.getText().equals("")) {
jButton1.setEnabled(true);
} else {
jButton1.setEnabled(false);
}
and the event handler you can use
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
You can generate this automatically in Netbeans by going to the event tab when you have clicked on a component in the design view.
It seems in your example you have the right idea, however you need to update the button using events such as key pressing, key releasing etc
You can add it in onblur() method of those text boxes.
If it can, you can add a validation with an error message on click of save button, which might be more meaningful.
I am developing a game with LibGdx. I have multiple languages in my app which in properties file. When i change my language in the menu text is not translating, so i need to reboot my app to apply translation. I think it is because i load my propereties file once on a boot and than it is not changing. I can use setText() method in the buttonListener to change all buttons text, but i have too many buttons. So are there any ways to transtlate my app immediately?
I believe setText() is the only way to get buttons to update their text. I don't know how you are storing the text in the different languages for your program, but as a general idea you could try creating a custom class which extends TextButton and inside have a reference to where you store the different language text. Then have a method like changeLanguage() which then gets the text for correct language and calls setText() on itself.
Then you could have an array which holds references to all of the buttons in your program and when you change the language use a loop to go through this button array calling changeLanguage() on each button to set all of your buttons to the correct language. This will be done very quickly and you won't have to manually call setText() on each button in your program.
Is there an equivalent for C's getch() in Android or Java? I want the execution to stop until the user does some action, like tap on the screen or maybe press one of the hardware buttons like volume control or whatever is available. Another option would be to show a modal message box window, which does not let the program continue until the user presses on OK. Is it possible to do this in a simple way in Android? What is the simplest way to get something equivalent to getch() function in android?
I need to be able to use this in a Thread as well
There's no getch() equivalent function/method in java.
You must event handlers to do that.
Like having click handler for button,Which let you to some stuff on onClick() method,Once you click the button.
This example might helpful:Button Click Listeners in Android
There's an event listener for android like onCLickListener. Then you can also used Jdialog then set its .isEditable value to false like dialog.isEditable(false);
I have an event handler that I want to be connected to a button through xml.
Basically, I want to define it as a string in my xml file, then use the property inspector to select the name of the handler for my button (the "on Click" property).
How do I define a call to the function in the string?
It's "Easier click listeners" section here http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
You can't can attach a function from XML.
What you do is create a layout, inflate it from Java code, find the button by id and attach a function that will be called on click.
This is covered in introductory tutorials.
EDIT:
As pointed out by Romain Guy, http://developer.android.com/intl/de/reference/android/view/View.html#attr_android:onClick
(but I'd say it's crazy anyways :))