Adding Views to Layout overrides View id - android

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

Related

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

Programmatically change View properies Android

In general, I'm trying to understand if it's possible in Android to arbitrarily change the properties of a view programmatically.
I understand that there are many properties that can be changed via methods (e.g. TextView.setBackgroundColor() among many others) but there aren't methods for every possible property.
Specifically, I'm interested in instantiating a custom View and then changing the layout_weight. I'm interested in learning how to do this, but in general I want to know how I'm supposed to create a custom View if I can't change it's properties programmatically. I understand I can change all its properties in xml (including custom xml properties) but I want to be able to instantiate the view at run-time.
layout_ attributes are actually slightly different than most other things as explained in this pro-tip: they're instructions to the parent ViewGroup and are stored in their LayoutParams.
For example, layout_weight in a LinearLayout would be found in LinearLayout.LayoutParams. This means you can change them by doing
LinearLayout.LayoutParams params =
(LinearLayout.LayoutParams) yourCustomView.getLayoutParams();
// Set the weight however you like
params.weight = 4.0f;
Creating the view allows you to do this as well:
LinearLayout parentLayout = ...;
YourCustomView yourCustomView = ...;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT, // height
1.0f); // weight
parentLayout.addView(yourCustomView, params);

Create a RelativeLayout dynamically and positioning views inside

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.

dynamically adding a view to activity layout

I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file).
The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance of my custom view, then use addView on the RelativeLayout to add the new view.
I'm not getting any errors, but when I click my button to add the new view, nothing is happening (view isn't being added). Do I need to set additional properties on my custom view (layout width, layout height for example) in order for it to be shown?
EDIT: adding code
// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Please post your code where you add the view.
But yes, you might be missing the params for width and height. Try something like
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
txtView.setLayoutParams(p);
or what you would like the width and height to be. Also in xml layout, layout_width and layout_height are required attributes.

Android button in arbitrary position over RelativeLayout

I'm trying to build an android application that features a graphical display drawn within a RelativeLayout. I want to place "+" and "-" buttons next to several of the parameters, which are drawn at various points on the canvas. The positions are free-form don't seem to conform to any of the standard XML layouts.
I know how to create the buttons programmatically, but I don't know how to place them over the canvas where I need them to be. I'm assuming that this would be done in the view thread's doDraw() method, after all the graphics have been drawn, but how?
I struggled with the same problem, and found out great solution.
RelativeLayout rules like "leftOf" or "rightOf" can be implemented programmatically like this:
RelativeLayout container = new RelativeLayout(getApplicationContext());
Button weight = new Button(getApplicationContext());
final int WEIGHT_ID = 0;
weight.setId(WEIGHT_ID);
weight.setText("0.0");
LayoutParams wrapBoth =
new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(weight, wrapBoth);
Button increaseWeight = new Button(getApplicationContext());
increaseWeight.setText("+");
// Note the difference: RelativeLayout.LayoutParams in spite of LayoutParams
RelativeLayout.LayoutParams toBeRightOfWeight =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(parameter,wrapBoth);
// Sweet part
clearAirParams.addRule(RelativeLayout.RIGHT_OF, WEIGHT_ID);
container.addView(increaseWeight, toBeRightOfWeight);
So, in code you can create a 'container' RelativeLayout, then add several Views with unique ID's and, finally, create RelativeLayout.LayoutParams object to achieve sweet-like-sugar methods for alignment, like in XML.

Categories

Resources