How to define function in a string in Android? - java

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 :))

Related

How to call a button from another activity in Android?

I'm new to android/java and I'm not sure how to solve the following problem:
Basically, I am creating a fitness tracker app and I need to be able to add details about an exercise (a text view and a couple text input fields) to my 'activity_weights_main' when the relevant exercise button is clicked in my 'activity_exercises'page.
At the minute I am trying to call the exercise buttons from 'activity_exercises' in my 'WeightsMain' java class via the code below:
btnCableCrunches = (Button) findViewById(R.id.btnCableCrunches);
After I'm able to call the 'btnCableCrunches' in my 'WeightsMain' java class, I'm planning on creating a method which will add the textviews/input fields that I need. I'm aware this is probably a stupid question but I've been trying to research how to do this for quite a while now with no success.
So I guess my question is: How do I call this button from another activity (activity_exercises) so that I can create a method (in 'WeightsMain.java') to add textviews/input fields (in 'activity_weights_main').
I don't need to know how to create the onClick method, just need to know how to call the button from other activity so I can do so myself. Thanks in advance for any feedback!
Actually you are looking for " Callback Implementation".
You can follow this example for assistance.

How to use onSuggestionSelect, the abstract public method of SearchView.OnSuggestionListener?

How do we use onSuggestionSelect in Android programming?
The other abstract public method onSuggestionClick is the usual one we need to use after the user clicks on the item from the suggestion list.
However, I am having a hard time trying to understand the use of onSuggestionSelect.
According to the Official Android Developer Document,
onSuggestionSelect is called when a suggestion was selected by navigating to it
onSuggestionClick (int position) is called when a suggestion was clicked.
I don't understand the term "when a suggestion was selected by navigating to it". How to do the navigating to it?
The Android SearchView.OnSuggestionListener is used to Callback interface for selection events on suggestions. These callbacks are only relevant when a SearchableInfo has been specified.
Properties in the SearchableInfo are used to display labels, hints, suggestions, create intents for launching search results screens and controlling other affordances such as a voice button.
On suggestion select is called when you select one of your options in the Searchable info by navigating to it
boolean onSuggestionSelect (int position).
For examples on this have a look at http://www.programcreek.com/java-api-examples/index.php?api=android.widget.SearchView.OnSuggestionListener

LIbGDX How to translate all text when language changed?

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 something similar to setActionCommand in Android also?

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

Why do i have to cast a Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly

Categories

Resources