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
Related
I am working on an android app in java, and ive created a sub-class of android.support.v7.widget.AppCompatButton, because youre not supposed to directly subclass Button. When i create the subclass object (hencforth refered to as "subButton") and add it to the activity via XML, the item looks and acts like a button, however when i do so via Java (programatically) , it looks and acts like a textView instead.
my XML is
<com.example.appName.subButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="xmlTestItem"/>
While my Java is
subButton testItem = new subButton(getApplicationContext());
testItem.setText("javaTestItem");
testItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT ,ViewGroup.LayoutParams.MATCH_PARENT));
mainListScroll.addView(testItem);
but it still only displays text instead of a button. Any clues?
The issue is most likely caused by the use of the application context rather than activity context when instantiating the subButton instance. In particular, the themes associated with these contexts, which are used when instantiating view components, are usually different.
As a general rule you should use the context directly available to you at the time. In an activity, this means making use of its own context via this.
For more details, see this classic post: https://stackoverflow.com/a/7298955/2259854
I just created my own Container, its structure looks like this:
Container
|- TabsContainer
|-Button1
|-Button2
|-Button3
...
It should be like this:
and be positioned at the bottom of the screen, like this:
in every Form I create.
When I add this custom Container to the 4 of my Forms, I still want all of the buttons to do the exact same thing. How could I do this? And In what function? before?
I already tried onPostShow()of my login Screen by getting all of them with their unique component root and adding an actionListener, but it did not work.
Furthermore the tabs are no "blank containers" but still depicting the buttons, but they should not. See here:
How can I solve these two issues?
To get this to work globally, you need to add the actionListenerdirectly to the buttons.
After creating your Container and adding the Buttons, right click on them one after the other and select Event -> Action Event. This will generate onComponentAction method where you can write your code for that button.
You should get something similar to this:
#Override
protected void onContainer_Button1Action(Component c, ActionEvent event) {
//Write all you want this button to do here
}
I'm sure you're trying to avoid code repetition, as mentioned in your previous question. Using universal container is more secure and less prone to errors than the earlier method.
Codename One also supports the EmbeddedContainer functionality in the old GUI builder. So you can create a Container instance for the 6 tabs (in the add new GUI element menu pick "Container") and add them to the tabs as an EmbeddedContainer UI. Note that this isn't supported by the new GUI builder which is based on a more traditional GUI builder approach and it might make navigation behavior in the app somewhat "odd".
In the new GUI builder you can just write generic code to map to this as the form would just map directly to a single class.
So here is what my app looks like so far. Every time I click the "+" button I go into another activity where I enter description, date and time and dynamically create a horizontal LinearLayout. With the X button to the left I'm deleting said layouts with this code (I know it's not the best way but it works for me so far):
final Task toBeRemoved = x;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myLayout.removeView((ViewGroup) v.getParent());
Task.tasks.remove(toBeRemoved);
}
});
..while iterating through each element in a list where I store my values in my OnCreate method.
What I want to do now is make it so I can remove them with the assistance of the checkboxes and the "Clear" button as well.
I have added each layout dynamically, though, so I can't think of any way for me to determine which one I've checked for deletion. They have no id, they can't be stored anywhere so I can iterate through them, as far as I know. What can I do in this situation?
A couple of ways. One would be to set listeners on each new checkbox and keep a Set of views with a checked state (add to the Set when checked, remove when unchecked). Then when clear is pressed, you remove all views in that Set.
The other way is to lopp through all the child views of the parent layout above the dynamically. For each one, find the checkbox child via findViewById, and see if its check. Remove it if it is. This is computationally expensive if you have lots of complex views
I prefer method 1 myself, but either works.
I have added each layout dynamically, though, so I can't think of any
way for me to determine which one I've checked for deletion
that's not true. You can call setId( ) or setTag() while you are adding each layout,
They have no id, they can't be stored anywhere so I can iterate
through them, as far as I know. What can I do in this situation?
now they have one. You can either use findViewById or findViewWithTag
Here's a solution that I consider pretty cool.
Create a widget on your own.It's easy.
So, you would have a class that extends ...probably LinearLayout.Depends on your needs.
You create the button,the editText, the textview and the check box, than you add functionality like you want.
When the Check box is pressed, I suppose you want the X circle button to get activated and when you press it, it deletes the whole "thing".This way you won't even need the clear button.You will still need the add button though, so you can add how many of those items you need.
It's easy,practical and makes the code reusable, which is something I look for always.
If you need more help, I can help you in a bit more detail ,but I can't always be around so I am sorry if I will respond a bit slow.
I am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.
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 :))