I'm fooling around with Android and trying to make a simple application to split a bill between friends.
I'm wondering if there is a way to create buttons and textfields based on user input. For example, if the user says he needs to split the bill 5 ways, how can I generate 5 buttons and 5 textfields? Similarly, splitting 10 ways will create 10 buttons and 10 text fields.
Thanks,
Ben
Edit: I should mention I have implemented the input already. I simply need to use the input to generate the buttons and textfields.
What you need is to create a Linear layout that have only a button and textfield then you can dynamically add that layout into your activity main's layout programatically. You can follow this thread on how to add view dynamically to another view
I've had a similar problem. You need to make a custom layout and then use LayoutInflater to "put it into another layout".You can use the return of inflater.inflate() to get the parent layout. then you just use parent.getChildAt(i) in a for loop for all "fields" and getChildAt(whatever the index of the button or text field you want is) to get the buttons and access them programatically. You can also give every Button a unique ID with setId(generateViewId(yourButton)) to access them from other functions.
Related
I'm trying to make a contact app which can create contacts and add it to the phone default database. Now the question I came across is, how can I add more text fields for the user to enter more numbers?
I was thinking about using defined fields (like in android contacts) and on creating of the view, show user only one text field and hide all the others. So the user can add more fields by the 'add' button next to the view.
Then the problem is, how to align them properly under the first text field if the user wants to use the third text field instead of the second.
I checked this in SO Android: Adding multiple Views to a Linear Layout dynamically but I need different IDs to getText from different textViews.
Is there a way to do this thing I want to do? Thanks in advance :)
I have some 8 text boxes(edit text) in my layout. How do I add them as a group instead of adding them individually.
I want to take input from user in those textboxes(edit text). Input is related, so I need a group of textboxes. Checked the documentation but didn't find.
Any way to add such textboxes as a group?
You can use EditTexts in a Layout (typically a LinearLayout or a RelativeLayout will do).
That container will act as a group.
Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.
I have a problem loading previously created layout. I would like to load it and change text on buttons inside, then show it to the user. It will be quiz question and I have to show it many times during one activity. I don't want to create new class for my layout.
What do I have to use? I read something about Inflate class, but I think it is used only to create new classes. I tried setContentView() method, but app stops when method doing this load starts:
LinearLayout layout = (LinearLayout) findViewById(R.id.CapitalQuestionLayout);
setContentView((View) layout);
Can someone give some hints?
Try using the layout field not the id field, when you call from the R class, like so :
LinearLayout layout = (LinearLayout) findViewById(R.layout.CapitalQuestionLayout);
setContentView((View) layout);
For creating a "Quiz" app your basic requirement is:
a layout which has a TextView for Question and 4 Buttons for options.
a set of questions; pretty obvious :-).
You can create a Custom Class - Questions that will hold Text for a question and its associated options (as Strings).
Now, whenever you want to display a new question with different text for buttons just do the following:
If user clicks on right answer then display a Right-Answer-Activity to the user (that has a next-question-Button).
When user clicks on next-question-button you can display Question-Activity and populate the layout-views with a randomly picked question-object's attributes (i.e. Question's text and options' text).
Hope this helps.
This is quite common in Android apps. Do your fields/buttons have an id? For the parent activity, you mostly do
Button someButton = (Button) findViewById(R.id.the_id_to_the_button_you_want_to_change);
This allows you to do things like
someButton.setText("What is the ultimate answer to life, the universe, and everything?");
Look up the Andorid documentation if you want to do other things like set the color. If you have a viewgroup of some sort (RelativeLayout / LinearLayout / etc), you can specify that specific one
Button someButton = (Button)viewGroup.findViewById(R.id.awesomely_named_button_identifier);
The above someOtherButton is used more often with inflated viewGroups
I have a standard layout and i have to populate it at runtime with a number of controls/views i.e. TextView / EditText depending on the number of products that come back from a REST service.
Of course the control I wish to add to the layout at runtime needs to contain a number of views (textview, edittext) etc. I was thinking a custom control to bring all the controls I need I am unsure.
The other idea I had was to inflate and existing XML into my layout but I am unsure if this is possible or if it was or would I control the ID names - inserting more than 1 would cause duplicate id's?
I will try and explain in detail what I am trying to do, we can wrap it in a for loop for test which would count form 1 to 5 hence 5 controls would get populated on my layout.
The custom controls would have a TextView which describes the product. The Edit text where the user can enter freely the amount in numbers using the virtual keyboard and a spinner control to the right of the EditText which would allow the increasing of the EdtiText value.
So all pretty simple eh ? :-) but of could I class all these controls as 1 specific view and I need to a number of them on my layout hence if there were 5 products there would be 5 custom controls, each custom control contain controls i.e. TextView, EditText and Spinner.
How can I accomplish this?
The examples I have seen have been inheriting from VIEW but I need my VIEW (CUSTOM CONTROL) to be a container for a number of other controls and then later be able to dynamically add this new custom control onto my Activity Layout.
What about using a ListView with custom adapter...
check http://www.ezzylearning.com/tutorial.aspx?tid=1763429
http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
You may want to use a ListView with a custom Adapter, and update the adapter with the information from the service.