I am working on a mini-game that includes 40 different buttons. I guess I can make them manually on the XML code and then group them into an array, but can I make buttons using the code in the first place?
Yes, you can do it from code as well:
Button button = new Button(getContext());
button.setText("Button_text");
button.setId(xyz);
// ... add onClickListener etc.
// Add it to parent view, e.g. LinearLayout
parentLayout.addView(button);
If you have to add 40 different buttons, wrap it to a for loop for example.
Related
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.
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
In my project i have 3 xml files.
My main layout
and 2 layouts which i want to include in my main layout
*big_buttons.xml* >contains big size buttons
*small_buttons.xml* >contains the same buttons as above (same id's aswell) but they are smaller
By default i want the *big_buttons.xml* included, but id like to be able to "exclude" the *big_buttons.xml* and include the *small_buttons.xml* programmaticly after an onClickListener
Is it possible to do something like this?
By default you can use setContentView(R.layout.big_buttons);, and then in your onClickListener you could do setContentView(R.layout.small_buttons);
If it's specific buttons you want excluded rather than the entire XML, I think you need to combine the 2 XML files and by default give the "big buttons" the attribute android:visibility="visible" and the "small buttons" android:visibility="gone".
Then programmatically you can do
Button bigButton = (Button) findViewById(R.id.big_button);
Button smallButton = (Button) findViewById(R.id.small_button);
bigButton.setVisibility("View.GONE");
smallButton.setVisibility("View.VISIBLE");
You'll want to use GONE rather than INVISIBLE because GONE excludes layout features like height and width, where INVISIBLE just doesn't display the button, but keeps space for it.
Check out View.setVisibility. You can use this on a layout manager, so that you can make entire groups of controls visible or invisible from Java code.
i want to create a page ,in such a way that a list should come in which first two columns,shows product name and its cost,and the third column should be an edit text to fill quantity.moreover i need three buttons at the bottom of the page.i am a newbie t android programming,plz help
Create a RelativeLayout that contains 2 layouts:
TableLayout for the first requirement.
Another RelativeLayout for keeping buttons at the bottom:
For 3 buttons u set the property android:layout_alignParentBottom="true".
For the 1st button u give android:layout_alignParentLeft="true"
and the 2nd button and 3rd buttons u give android:toRightOf="" property.
You will need to use custom Listiview for this pupose. Your mainview will contain a listview and the 3 buttons at the bottoms as you want.
Checkout following example for custom listview
Custom Listview