What should I do to use custom checkboxes if I add my checkboxes dynamically in my code? (On the java code not on the XML files.) . Like I want to add two checkbox in a single line in linear layout based on the response from server dynamically. ![This image explain my requirement][1].
[1] :https://stackoverflow.com/66778d9f-ae68-408a-a0f9-bc0af3e99c7a
You can do it with the help of your Linear Layout Id. Just assign id to your Linear Layout and use the following code:
LinearLayout ll= (LinearLayout)findViewById(R.id.linearLayout1);
CheckBox cb1 = new CheckBox(this);
CheckBox cb2 = new CheckBox(this);
ll.addView(cb1);
ll.addView(cb2);
You can also use ll.addview(Context, LayoutParams) . For this, you have to configure LayoutParams . It contains the values like wrap_content, match_parent, gravity etc.
Related
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);
I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?
I have tried:
TextView tv1 = new TextView(this);
TextView tv2 = (TextView) findViewById(R.id.basetext);;
// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());
But it doesn't copy any of the layout parameters...
Any ideas?
You can do One thing.
If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:
First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)
Now, do add that layout_textView.xml dynamically to your main view as per your requirement.
How it will Solve your issue.
If any query then let me know.
Did you call requestLayout() after adding the textview?
In my a LinearLayout I have set android:layout_marginBottom in my XML. I just want to retrieve that value.
Just LayoutParams doesn't give me access to bottomMargin . I tried to typecast :
row1 = (LinearLayout) findViewById(R.id.mainrow1);
LinearLayout.LayoutParams row1Params = (android.widget.LinearLayout.LayoutParams) row1.getLayoutParams();
but this gives me Runtime exception ClassCastException.
The main aim is I got buttons in nested LinearLayouts & I want to set height/width of LinearLayout programmatically that will inturn auto set height of buttons. I have added bottomMargin after 1st layout - so there is space between 2 rows.
Change Your layout of Button according to following logic,
set Layout_Width to 0dip
set Layout_Weight to 1
set Layout_Height to Wrap_Content
try this. When you get layout, R.layout. is best option. It is working for me.
row1 = (LinearLayout) findViewById(R.layout.mainrow1);
Usually the activity has a predefined layout which is described in the xml file. What if I know the exact number and types of UI elements only during the runtime?(for example, I need to display as many TextBoxes as user defined) Is it possible to create an activity with a layout defined during runtime and if it is, how?
First set an identifier to a view, where you want to insert your views at runtime :
<LinearLayout
android:id="#+id/linear_layout"
... >
Then you can add child views to this LinearLayout programatically, whenever you want :
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
linearLayout.removeAllViews();
// Add a TextView (it could be any kind of View)
TextView textView = new TextView(this);
textView.setText("...");
linearLayout.addView(textView);
setContentView(layout);
This layout you can define during runtime
I am developping an android app which downloads an xml and displays a layout with a number of edittexts, checkboxes, spinners, etc. added dynamically like this:
LinearLayout ll = new LinearLayout(this);
EditText nameField = new EditText(this);
ll.addView(nameField);
ScrollView sv = new ScrollView(this);
sv.addView(ll);
setContentView(sv);
I'm having trouble with setting some properties to an EditText added this way. For examle android:maxLength attribute can easily be set in an xml layout but I found no method to do the same in the java code.
How can I do it when hawing to add dynamically?
Thanks,
Zoltán from Hungary
If you look at the XML attributes in the docs, it lists the corresponding method you can call in your java code for each attribute
So for example setting the maxLength attribute can be accomplished through the setFilters(InputFilter) method.