Create a RelativeLayout dynamically and positioning views inside - android

I would like to create a method which returns a RelativeLayout created dynamically. To be clear, let's use this simplified example:
private RelativeLayout createLayout() {
RelativeLayout layout = new RelativeLayout(activity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
TextView tv1 = new TextView(activity);
tv1.setText("Text 1");
TextView tv2 = new TextView(activity);
tv2.setText("Text 2");
TextView tv3 = new TextView(activity);
tv3.setText("Text 3");
layout.addView(tv1);
layout.addView(tv2);
layout.addView(tv3);
return layout;
}
Now I want to position these TextViews relatively to each other. For that I have the idea to use a LayoutParams with the addRule method.
But this method requires an ID, e.g. addRule(RelativeLayout.BELOW, tv2Id). It means that I have to set an ID for each TextViews.
My problem is that the createLayout method will be called several times, so the question is:
Do I have to set different IDs for the TextViews each time the method is called in order to avoid conflicts ? If so, how can I do that ?
Most generally, Is there a better solution for doing it ?
EDIT
The idea behind this is to have a kind of ListView, where each item contains a Map (that can be shown or hidden).
Problem: the Map can't be scroll if it is inside a ListView (at least I did not manage to do that).
For that, I have decided to use a ScrollView and a LinearLayout to copy the behaviour of a ListView. This way the Map can be scrolled correctly and now, all I have to do is to create the items dynamically

ID's don't have to be unique. As you can see from this extract
setId (int id)
Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
But like you said, if you want to avoid conflict then you have to find a way to generate unique identifiers for each view.
Frankly, IMO I don't think it matters much the value of the ID. You can use 10, 20, 30. Just make sure you can have access to them anytime you need it, possible using a static final variable.
You asked if there is a better solution, yes there is. The most preferred way is to inflate an xml layout.

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 do I access a textview or progressbar in relative layout programatically when multiple?

I am creating multiple relative layouts in an activity, programmatically. Each is identical and has a textview as well as a ProgressBar spinner.
I want to programmatically change them when needed but not sure how to access the appropriate one. I believe I need to add a unique SetId() to each item (or maybe the relativelayout itself) but not sure the best way to do so.
I also am not sure if I use findViewById to access the views once created to make the changes (SetText, SetVisibility, etc).
Here is the code
RelativeLayout.LayoutParams tvpName = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvpName.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams pbpSpinner = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
pbpSpinner.addRule(RelativeLayout.CENTER_IN_PARENT);
pbpSpinner.addRule(RelativeLayout.ALIGN_PARENT_TOP);
for (int i = 0; i < 5; i++) {
RelativeLayout acctrl = new RelativeLayout(this);
TextView tvName = new TextView(this);
ProgressBar pbSpinner = new ProgressBar(this);
pbSpinner.setVisibility(View.GONE);
// Add items to Account Interal Layout
acctrl.addView(tvName, tvpName);
acctrl.addView(pbSpinner, pbpSpinner);
}
Any recommendations / suggestions?
When you create a View programatically, it's a good practice to setId(). Additionally, you can go ahead and setTag() also. By setting a Tag, you can know what each RelativeLayout or any other view. This way you can get a hold of the view that you are looking for.
If you are not going to access or modify the RelativeLayout then you need not set ID for it, but setting an id for it makes this easier, so that you can just send the ID of the RelativeLayout as a parameter to a method and it will perform all operations on the Views it holds, since you said all RelativeLayouts are identical.
If you are using setId(), then you can use the findViewById() to get the view.
If you are using setTag(), the you can refer this on how to use it to get the view: https://stackoverflow.com/a/5291891/4747587

Optimize Performance for creating views at runtime

I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.

How do I add FrameLayout and ImageViews within it Programmatically?

There are plenty of similar questions asked in SO, but then also, please take time to read my question.
I need to create a UI programmatically that would have multiple ImageView, all positioned at different location of the screen and having their associated click events (its a game app). I found that FrameLayout is appropriate choice, where I can set margins on my own and have it positioned at desired location.
Now, I'm confused whether to have FrameLayout for every single ImageView I create, or to keep single FrameLayout and add all ImageViews within it, but set each imageview at different position.
In either of the case, how can I add FrameLayout, and ImageView within it, programmatically and also set its margin such that it can be placed anywhere on the screen.
Note that my main canvas, which will carry all these ImageViews has background, and the canvas is a LinearLayout set via XML, so my onCreate() already has setContentView(R.layout.game_canvas);, and I'd be using addContentView() to add additional views, but this method too accepts LayoutParams object as it second parameter, so what exactly should I set for this, when I add my FrameLayouts using this method?
My question might be confusing itself, so please let me know if I need to elaborate.
FrameLayouts are designed to only hold one View, so this isn't really the appropriate choice.
Use a RelativeLayout to hold all of your ImageViews. You can position each ImageView by setting the margins in their LayoutParams.
E.g. The following code would place an ImageView at coordinates 50,50:
RelativeLayout imgLayout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.an_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(50, 50, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imgLayout.addView(iv, lp);
You can then add this RelativeLayout to your main LinearLayout using its addView() method.

Adding Views to Layout overrides View id

i want to know if anybody experiments this, i have a Custom View, who draws a Radar, everything is fine, the draw, measurement, layout, but when i construct it i set the view id with an integer like this,
Radar radar = new Radar(context);
radar.setId(RADAR_COMPONENT.hashCode());
LayoutParams lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
this.addView( radar, lParams );
and later i add others views without set the id... like this
this.addView( getItemView(item), new LayoutParams(150, 100));
...
this.addView( getItemView(item), new LayoutParams(150, 100));
...
the getItemView(item) returns a inflated View from xml without id, and item is the object who holds the view info. My problem is when i add this views the fist view Radar... lost the id, and the id is -1, Anyone experiments this?
Thanks a lot
I don't see where you are setting the id, also why are you doing this the hard way you can still add your custom views via xml layouts by giving the full class plus package name for the view

Categories

Resources