Adding fields dynamically to a layout in Android - android

In my application, I want to add fields dynamically based on the selection. While there are 4 fixed spinner fields, the values of spinner are dependent on the selection of the first spinner field etc. The second spinner field has to be populated based on the first field and the third spinner field based on the second field and so on.
Based on the options selected in these 4 spinner fields, the layout will contain additional fields that are text fields or radio buttons etc.
Can someone suggest what is the best way to achieve this or refer to other examples for this?
Thanks

You can add a View to a Layout similar to the below code:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.setLayoutParams(layoutParams);
TextView view = new TextView(mContext);
view.setLayoutParams(layoutParams);
relativeLayout.addView(view);
And then you need to add it to an existing ViewLayout:
LinearLayout originalLayout = findViewById(R.id.mylayout);
originalLayout.addView(relativeLayout);
This idea can be expanded on to add custom Views etc to your screen.

Related

Add EditText dynamically with (if possible) String id into a Fragment

I would like to add an EditText dynamically into a Fragment.
I would like also, adding a String id to this EditText.
The following code is called after pressing a Button:
int number_of_editTexts; //At the beginning=0
Context context = getActivity();
EditText editText = new EditText(context);
editText.setId("NofET"+number_of_editTexts);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
editText.setLayoutParams(params);
RelativeLayout rel=(RelativeLayout) getView().findViewById(R.id.list);
rel.addView(editText);
number_of_editTexts++;
It adds the EditText, but i can't write editText.setId("NofET"+numer_of_editTexts); but only editText.setId(numer_of_editTexts);
Is there a way to do what I want?
And also, how can i do something like params.addRule(RelativeLayout.BELOW,R.id.DYNAMIC_ID)?
Element IDs are pure integers, they can't be set as strings. IDs assigned to elements created in XML are converted into an integer internally and stored as an int.
Dynamically created elements always have a ID of -1 by default. They can be manually assigned an ID through setID() but there is a chance of collision with other IDs created automatically by the system.
To prevent such a collision, the method given in this answer may be used to manually assign an ID.
EDIT: Basically, the link says that if you have API level 17+, you use View.generateViewId() else if you do it manually, you don't go above 0x00FFFFFF as an ID as these are reserved for statically created elements. Other than that, avoid conflicts among IDs created through your code.
However, in the case of this question, a LinearLayout may be a better way to go.
Suppose this is your XML.
<LinearLayout
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"/>
The Java code to add an EditText to this may be something like the following:
List<EditText> edittexts;
...
LinearLayout rel=(LinearLayout) getView().findViewById(R.id.list);
...
void addEditText(Context myContext,int edittextno)
{
EditText ed=new EditText(myContext);
ed.setText("EditText"+edittextno);
LayoutParams lParamsMW = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ed.setLayoutParams(lParamsMW);
edittexts.add(ed);
rel.addView(ed);
}
You can modify all the created EditTexts through the List edittexts.
The vertical LinearLayout automatically gives the vertical list format required by the OP. A margin can be added to each added element if required. If required to add more elements to the left or right of the EditText, a horizontal LinearLayout may be dynamically created, elements added to it and the horiz. LinearLayout added to the static one in a similar manner as in the code above.

How to implement this table view in android

How to implement below image in Android.
In this view the time row should static, but table row are dynamic and each row the cell width is depend on time.
Here green box are cells.
http://i.stack.imgur.com/bBtsJ.jpg
If I understand correctly, you want to change the blue and green boxes dynamically and the rest is static. So you want to change the column span programmatically. To do this, first find the View you want to change using TextView tv = (TextView) findViewById(R.id.some_sensible_id); and then call
TableRow.LayoutParams params = (TableRow.LayoutParams) tv.getLayoutParams();
params.span = columnsToSpan;
tv.setLayoutParams(params);
Where columnsToSpan is how many columns the View should occupy.

How to generate multiple layout and get value of each filed in it in android

Actually i am going on with the page which has a add button if the user click add button it
should generate multiple layoutwith DropDown and edittext tried it programatically how can i take each value of dropDown
and edittext from all layout and pass it.
(for eg: if user click the button for 10 times it should generate 10 layout with the fields but the their is no limit for click).
If any other approach please help me out to solve this issue.
Tried:
LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.progm_view);
EditText editText = new EditText(this);
editText.setId(i);
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
int i = edittext.getId();
editText.setTag("data");
EditText ed = (EditText)findViewByTag("data");
String text = ed.getText().toString();
Have an xml with dropdown and edittexts. Whenever user clicks, inflate the layout and add to the parent layout, so that you can user getChildAt(position) method to get each inflated layout so as to get the dropdown and edittext objects.

Android programming : how to programmatically create various view types in a grid fashion

I'm not sure how to ask this question exactly, but I'll give it a try.
Here's what I'm trying to do, in one Activity.
Build a Grid, that contains [x] rows of 3 columns each, with this content
[a TextView (containing a name)] [a Spinner (containing a list of states)] [an EditText]
How to start ? The Views I can create programmatically, that's not a problem, I even store them in 3 array lists for later easy reference, but I can't see how to do it right.
Should I create an xml layout with e.g. (and pseudocode)
LinearLayout (horizontal)
TextView ...
Spinner ...
EditText ...
/LinearLayout
and try to inflate it in the loop I use to create each row, and setting the id of each view in a standard way (e.g. viewName[x] where x is the current "i" from my for, but is it of any use?), as we do for example for an ExpendableList Adapter's groups/childs ?
Or is there a way to actually use a GridView/GridLayout to do that (in this case, being in my Activity, how do I put each specific created View into the correct GridView/GridLayout) ?
Or still another way I don't suspect at all ?
Thanks in advance
if you want to add views programatically then, just create a layout.xml with 3 Linearlayouts(horizontal) in it. Also assign id to those LinearLayouts. Then in your java code, just find the views and call addView() on those LinearLayouts.
Example:
LinearLayout ll_1 = (LinearLayout)findViewById(R.id.linearlayout1);
LinearLayout ll_2 = (LinearLayout)findViewById(R.id.linearlayout2);
LinearLayout ll_3 = (LinearLayout)findViewById(R.id.linearlayout3);
...
ll_1.addView(new TextView(this));
ll_1.addView(new Spinner(this));
ll_1.addView(new EditText(this));
...
In detail, say for example, if i want to add spinner to LinearLayout, programatically then,
You need to get layout
LinearLayout linearLayout = findViewById(R.id.layoutID);
Create spinner as below:
Spinner spinner = new Spinner(this);
spinner .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, spinnerList);
spinner.setAdapter(spinnerArrayAdapter);
Then add spinner to view
linearLayout.addView(spinner);

Programmatically Position items in activity. NO XML

I have an Activity that has all the display elements added dynamically. Theres no xml for the acvtivity at all.
The Activity consists of the following controls:
RelativeLayout (Layout object that all the child views sit in)
TextView (Title for the page, sits at top of the RelativeLayout)
ScrollView (Scrollable area that holds all the data controls)
LinearLayout (Layout object to hold the activity buttons)
I want to know how it is possible to define that the ScrollView sits below the Title TextView and above the LinearLayout button holder where the LinearLayout is set to the Activity Page bottom
I have tried using RelativeLayout.LayoutParams to set up rules but cannot seem to understand the way to do it. Any help or links to tutorials would be apreciated
I have included my Code to see if someone can help
// declare the items for display
RelativeLayout baseLayout = new RelativeLayout(this);
// add the customer name and number field.
// NOTE: We will always require this widget regardless of dialog design fields
tvCustomerNameNumber = new TextView(this);
tvCustomerNameNumber.setTextSize(20);
tvCustomerNameNumber.setText("Customer Name & Number");
// build up the linear layout of controls
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Scroll view.
// NOTE: We will always need this widget to enable us to scroll the page
// if too many items are added for the display size
ScrollView sv = new ScrollView(this);
sv.addView(ll);
// buttons
LinearLayout buttons = new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
// button edit
Button edit = new Button(this);
edit.setId(EDIT_BUTTON_ID);
// button save
Button save = new Button(this);
save.setId(SAVE_BUTTON_ID);
// button cancel
Button cancel = new Button(this);
cancel.setId(CANCEL_BUTTON_ID);
// add each button to the button layout
buttons.addView(edit);
buttons.addView(save);
buttons.addView(cancel);
// Scroll view Layout parameters
RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollParams.addRule(RelativeLayout.BELOW, tvCustomerNameNumber.getId());
scrollParams.addRule(RelativeLayout.ABOVE, buttons.getId());
// buttons Layout parameters
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonParams.addRule(RelativeLayout.BELOW, sv.getId());
// add the customer name number field to the base layout
baseLayout.addView(tvCustomerNameNumber);
// add the scroll view to the base layout
baseLayout.addView(sv); //, scrollParams);
// add the buttons to the base layout
baseLayout.addView(buttons, buttonParams);
// set the content view
this.setContentView(baseLayout);
Deva has already answered your question, but it sounds to me like you could define an xml layout as you have described above, inflate it and populate it dynamically programmatically... perhaps the layout would initially contain an empty LinearLayout, and/or no text set for the TextView, maybe even set everything to android:visibility="gone" and show it when you have added/updated all your views?
See the links below which might help you achieve this :
Programmatically adding items to a relative layout
How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

Categories

Resources