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.
Related
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.
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.
I am trying to add an editText dynamically to a relative layout. the layout contains an editText already. I need to add the new editText below the existing one.
EditText designation1 = new EditText(context);
designation1.setHint("designation");
designation1.setSingleLine(true);
and my layout is
layoutExp =
(RelativeLayout) childView.findViewById(R.id.relative_layout_edit_exp);
and my existing edit Text is
designation = (EditText)childView.findViewById(R.id.editTextDesignatn);
You need to use RelativeLayout.LayoutParams and specifiy the relative position with the addRule(RelativeLayout.BELOW, ...) (this is the programmatic equivalent for android:below XML attribute):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.editTextDesignatn);
layoutExp.addView(designation1, params);
It will be possible if You set for example a LinearLayout under Your existing EditText. Give an ID to this LinearLayout and then You could put Your dynamic EditText to this LinearLayout like
YourLinearLayout.addView(YourDynamicEditText);
ANDROID
I have a layout defined in xml and have a static textview, edittext and checkboxes which are all formatted as below:
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:textSize="14dp"
android:width="180dp"
I add textview, edittext and checkbox dynamically. I need the new added ones to have the same visual display as the ones already present(static ones) on the layout! Could someone guide or point me how to go about it?
You can add the layout by inflating each time you need textview, edittext and chekbox.This way your layout will have same look.Because you are reusing your static layout in xml.
See Layout Inflating for details
You have a layout with textview,edittext and checkbox.Now you will use the layout as a view.Like you said for each row you have to inflate the layout and add it to the row.So for every row you will have the copy of the layout.
TableLayout layout=(TableLayout)findViewById(R.id.tblLayout);
Now you can add the view after inflating the layout
View rowView = inflater.inflate(R.layout.row_layout, null, true);
TableRow tblrow=new TableRow(this);
tblRow.add(rowView);
layout.add(tblRow);
You can create any control Dynamic like this way.
Here i show for Edit Text same way you can do for others.
EditText et = new EditText(this);
et.setText("");
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Use the following code for dynamic configurations about EditText.
EditText my_edit = (EditText) findViewById(R.id.my_edit);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 2.0f;
my_edit.setLayoutParams(params);
my_edit.setTextSize(20);
my_edit.setWidth(180);
my_edit.setSingleLine(true);
For this you must need LinearLayout as a parent of your EditText.
I hope this helps you somehow.
Thanks.
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