Programmatically change View properies Android - 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);

Related

LinearLayoutParams are causing a casting issue

I must be missing something really stupid.
I have a LinearLayout in my view:
_editTextViews = (LinearLayout)findViewById(R.id.editDesAndLocViews);
I am trying to dynamically change the height of that view by setting the LayoutParams
I have two LinearLayout.LayoutParams
LinearLayout.LayoutParams collapsedParams;
LinearLayout.LayoutParams openParams;
that are set in the constructor like so:
collapsedParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
openParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
But as soon as i try to apply one of those params to my LinearLayout, like so:
_editTextViews.setLayoutParams(collapsedParams);
I get the following FATAL EXCEPTION:
At first i thought my project needed to be cleaned as it seems to think i am trying to force a relative layout to use linear layout params, but everything is LinearLayout. The LinearLayout in question does contain a couple RelativeLayouts. I don't know if / why that would be the problem.
please help
Change your LayoutParamss' type to either ViewGroup.LayoutParams or RelativeLayout.LayoutParams.
The LayoutParams you apply have to match the parent layout of the view. LayoutParams define how a View is layed out in its parent view (layout). Because each layout type takes different parametres for laying out a view (e.g. only LinearLayout has weight, only RelativeLayout can use anchors), they all have their own LayoutParams.

How can i set margin to a view in another class?

I do a constructer and set some view inside programmatically and add some rule. so When i call constructer i see default view.
Now i want to reach someof this view and change set margin.It is basic i know but i dosent work for me.
Can anybody help?
Part of my constructer:
_photoImgVw = new ImageView(getContext());
_photoImgVw.setImageResource(R.drawable.pic);
_photoImgVw.setAdjustViewBounds(true);
_photoImgVw.setMaxWidth(100);
_photoImgVw.setId(2);
lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
LayoutParams lp3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF,_photoImgVw.getId());
lp3.setMargins(0,0,0,0);
addView(_nameTxt,lp3);
Call in the activity for change:
facebookPersonView.lp2.setMargins(10,300,100,20);
Make sure that the layout params lp2 are an instance of the class RelativeLayout.LayoutParams instead of MarginLayoutParams, or any other subclass of it named LayoutParams (there are quite a few, see the top of the documentation).
If the params are an instance of the latter, they won't contain any rules for the RelativeLayout. Which means the parent layout doesn't know where to place the view¹ and therefore can't make any sense out of the margins.
It's best to declare that explicitly in a way like this:
RelativeLayout.LayoutParams lp2
= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
¹ It will still make it visible in the top-left corner, but it's kind of an undefined state.

Android, how to add to parameters to LayoutParams

I want to add two parameters to LayoutParams:
View bar1 = new View(this);
bar1.setBackgroundResource(R.drawable.blue_bar);
RelativeLayout.LayoutParams relativeParams1 = new RelativeLayout.LayoutParams(68, (int(aValue));
relativeParams1.bottomMargin=50;
Log.i(TAB, relativeParams1.debug(OUTPUT));
linearLayout.addView(bar1, relativeParams1);
but only width and height is changing. The included logcat show me that
ViewGroup.LayoutParams={ width=68, height=300 }
How do I change the margins too?
What is the type of layout you are adding bar1 to, is it a LinearLayout ?
In that case, you can't use RelativeLayout.LayoutParams, you have to use LinearLayout.LayoutParams and, if I remember correctly, you don't have margins there.
So change the containing 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