LinearLayoutParams are causing a casting issue - android

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.

Related

What LayoutParams do or pass in Android?

I read often something like this:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
What exactly LayoutParams do?
I've read the Documentary but I wasn't smarter after reading!
Hope someone can explain me what LayoutParams do or pass!
Kind Regards!
LayoutParams are the Java Object representation of all the params you give to your View in the .xml layout file, like layout_width, layout_height and so on. Getting this object from a View allows you to look up those params on runtime, but also to change them in your Java code, when you need to move the View, change it's size etc.
LayoutParams are used by views to tell their parents how they want to be laid out.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content.
That's all folks.
LayoutParams is use for the dynamically change the layout width and height. and also use the create custom view without the xml by using the directly by use of the LayoutParams for Relative or Linear type layout.
Basically when you set an xml with 'match_parent' or anything like layout_something
the android inflator will set the layout param for the child with the appropriate Layout params with the type matching the parent control, you could also do this in code and if you forget or set the wrong type you will get an exception in runtime.
the parent control needs this information to layout the child control correctly and to his liking.
Please see the following: Android Developer site - Layout Params
I think this picture says it all

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);

android explaination addview & layoutparams during runtime

just want to know what happens when I set new layout params to a new instance of an ImageView and then add it on an empty layout. It seems like doing it reset the parent layout hosting the new ImageView and also reset position and size of every sibling ImageView.
So what happens? Parent layout takes the layoutparams of the child?
thanks
Layout params have direct affect to views which use that params. But that also affects other views under same parent. Think about width, height and weight attrs of linear layout params. For example, if you set a layout params which has a weight attr defined, this will all change the visual of your parent.
From the doc:
LayoutParams are used by views to tell their parents how they want to
be laid out.
Note:
A parent is a view group, a container for child views. There are some types of view groups, ie LinearLayout, RelativeLayout etc(subclasses of ViewGroup can be googled), and they have specific layout behaviours and LayoutParams. While adding views to view groups layout params are used (both on xml and programmatically) for setting position, width, height inside that view group(container).

android: retain attributes children in RelativeLayout dynamically adding ImageViews

How can I retain attributes of the children in a RelativeLayout when dynamically adding ImageViews?
I have a custom ImageView I want to add at runtime to an empty RelativeLayout (nothing inside in XML), I can add the first, then move, scale and rotate them, it works fine.
When I add another ImageView all previously added instances loose their position and their size, but when I touch them they get back just their size, not the position.
In my ImageView I'm overriding onDraw and onTouch, do I need to override something else?
Maybe I have to write my own RelativeLayout implementation? I wouldn't!
This is the pseudocode for adding new ImageView:
create new instance of ImageView
set bitmap
set scaletype
add imageview to the RelativeLayout container
I even tried to set a standard layout parameter for the brand new added ImageView with the same result.
I tried to get the margins for every children, based on its position, and the re set the layout with ImgeView.setLayout(l, t, r, b);
no success...
Here's the XML RelativeLayout container, pretty simple, maybe too much?
<RelativeLayout
android:id="#+id/face_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
Do you need more details to help me? Please ask, I can post the code too, but I have to clean it up a bit before, I can do it tomorrow, in the meantime please give me some advice.
In a RelativeLayout all view positions are related to the others views. So, as you do in xml, you should provide for each view the relative parameters.
Before add imageView to the RelativeLayout call setLayoutParams(layoutParams):
// Create a new RelativeLayout.LayoutParams instance
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// add roules
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layoutParams.addRule(.........
layoutParams.addRule(.........
//add params to view
imageView.setLayoutParams(layoutParams);
//add imageview to the RelativeLayout container
yourRelativeLayout.addView(imageView);
Look here for all rules.

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.

Categories

Resources