Set Layout Params for custom View - android

In XML I was creating a new Entity of SwipeDeck (customView) like this :
<com.daprlabs.aaron.swipedeck.SwipeDeck
android:id="#+id/swipe_deck"
android:layout_width="match_parent"
android:layout_height="20dp"
swipedeck:card_spacing="15dp"
swipedeck:max_visible="1"
swipedeck:render_above="true"
swipedeck:swipe_enabled="true">
Now I am trying to programmatically set the x and y position. this works fine:
cardStack = (SwipeDeck) findViewById(R.id.swipe_deck);
cardStack.setX(20);
cardStack.setY(10+actionBarHeight);
When I am trying to set the height and width dynamically, nothing happens:
cardStack = (SwipeDeck) findViewById(R.id.swipe_deck);
ViewGroup.LayoutParams params = cardStack.getLayoutParams();
params.height=500;
params.width = screen_width - 40;
cardStack.setLayoutParams(params);
Everything happens in onCreate. Any ideas?

You should be declaring "params"'s type with respect to the ViewGroup that contains it.
For example, replace the line
ViewGroup.LayoutParams params = cardStack.getLayoutParams();
with
LinearLayout.LayoutParams params = cardStack.getLayoutParams();
if your custom view is inside a LinearLayout

Related

Have an ImageView with a default height, change height when an image is put into it

So I am making an app that holds multiple user-chosen pictures. I have default ImageViews with pre-set heights of 300dp. I want this height to change to wrap_content once an image has been placed into the ImageView. The only way I know to do this is to remove the image from the layout and then re-add it with a new LayoutParams, but this messes up the order of the other views in my layout. Can I change the height without removing it?
Essentially:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
final float scale = getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams mTestImgParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(int) Math.ceil(scale * 300)
);
final ImageView createdView = new ImageView(this);
mainLayout.addView(createdView, mTestImgParams);
//onLongClick listener, get picture, set the picture into the imageview, etc.
I somehow want to change the
(int) Math.ceil(scale*300)
to
LinearLayout.LayoutParams.WRAP_CONTENT
without removing and re-adding the ImageView, and only after the image has been placed. Help please.
You could try to get the current layout params and change it.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) createdView.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
createdView.setLayoutParams(params);

Unwanted spacing between android viewgroups added programmatically

I'm adding a RelativeLayout in a FrameLayout ( added in xml ) and I attach a LinearLayout in this RelativeLayout.
The problem is I get an unwanted left/right spacing/padding between the RelativeLayout and its child LinearLayout as you can see here from the uiautomator dump.
The weird thing is that spacing is always 11 pixels from each side tested on emulator N5, N4, N7 images and Moto G and N4 devices.
Thank you
The code is :
rlBottomBarContainer = new RelativeLayout(this);
rlBottomBarContainer.setId(R.id.bottomBarContainer);
rlBottomBarContainer.setClipToPadding(false);
rlBottomBarContainer.setClipChildren(false);
rlBottomBarContainer.setBackground(getResources().getDrawable(R.drawable.bara));
int topMargin = whiteTowerContainerHeight /2 + tower_height/2 + 100;
int containerHeight = whiteTowerContainerHeight - topMargin;
FrameLayout.LayoutParams containerParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
containerHeight);
containerParams.topMargin = topMargin;
rlBottomBarContainer.setLayoutParams(containerParams);
frameLayoutRoot.addView(rlBottomBarContainer); // parentLayout
llBottomBarContainer = new LinearLayout(this);
llBottomBarContainer.setId(R.id.upper_bottombar_ID);
llBottomBarContainer.setClipChildren(false);
llBottomBarContainer.setClipToPadding(false);
llBottomBarContainer.setBackgroundColor(Color.parseColor("#00FFFFFF"));
llBottomBarContainer.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams bottomBarParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
getBottomBarTextSize());
llBottomBarContainer.setLayoutParams(bottomBarParams);
rlBottomBarContainer.addView(llBottomBarContainer);
As you can see from the code the LinearLayout is added with match_parent for the width. RelativeLayout width is from 0 to 720px but LinearLayout is from 11 to 709pixels.
First of all I do not understand why you use a FrameLayout and you do not use directly the RelativeLayout as your parent layout.
The error I see on your code is that you set a FrameLayout.LayoutParams to your RelativeLayout and a RelativeLayout.LayoutParams to your LinearLayout.
Probably this is why you get some extra spacing.
I would suggest that you do not create your layouts programmatically, but rather do it on xml and then if you need the layout objects on java you can get them with the findViewById(int id) method

just last column in Grid layout gets stretch

I want to stretch all columns in my grid layout equally , How can I do that ?
I am using this params for every cell
LayoutParams param2 =new LayoutParams();
param2.height = LayoutParams.WRAP_CONTENT;
param2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
param2.setGravity(Gravity.CENTER);
param2.columnSpec = GridLayout.spec(i,FILL);
param2.rowSpec = GridLayout.spec(rowCount,FILL);
param2.setGravity(Gravity.FILL);
Build Your Own GridLayout using nested LinearLayouts so you can customize it like you want, Or simply set the weight for each cell you add but you will need set you minimum sdk to 21
Setting width of child dynamically
GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin;
child.setLayoutParams(params);

Android - ImageView margins not appearing in LinearLayout

Right now, I'm struggling to accomplish something as simple as adding margin space between my child ImageViews within a custom LinearLayout (modified RadioGroup that is designed to take in a custom ImageView that implements Checkable, didn't override onMesarue). Long story short, these images are of a fixed dimension (60x60dip), and since they are dynamic (from the web), I had to add them dynamically like so:
for(int i = 0; i < num; i++){
ImageViewRadioButton childImage = new ImageViewRadioButton(mContext);
float imagehWidthHeight = getResources().getDimension(R.dimen.image_width_and_height);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int) imageWidthHeight, (int) imageWidthHeight);
int imageSpacing = Utils.dipsToPixels(10, mContext);
int innerPadding = Utils.dipsToPixels(5, mContext);
imageParams.leftMargin = imageSpacing;
imageParams.rightMargin = imageSpacing;
childImage.setAdjustViewBounds(true);
childImage.setLayoutParams(imageParams);
childImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_pressed));
childImage.setPadding(innerPadding, innerPadding, innerPadding, innerPadding);
childImage.setClickable(true);
//other non-image properties...
imageContainer.addView(childImage);
}
The only thing that does work is the padding, which it spaces it out properly. However, I am not seeing any space between the padding of each child (margins). Am I doing this correctly, or is there a better way of doing it short of overriding onMeasure to factor in each child's margins?
You had create imageParams but you are not using that parameters in your code instead of imageParams you are using swatchParams parameter. And you had not put a code of swatchParams parameter.

How to change size of the LinearLayout when it is runtime?

How to change the size of LinearLayout when it is runtime ?
I've set the size dynamically when it is runtime. But no luck. Didn't change the size of LinearLayout.
How to code it ?
Thanks in advance.
The size is determined by the LayoutParams connecting the LinearLayout with its parent.
This is how I do that:
// get element I want to resize
LinearLayout myBar = (LinearLayout) findViewById(R.id.infopanel_graph);
// get layout parameters for that element
ViewGroup.LayoutParams params = myBar.getLayoutParams();
// change width of the params e.g. 50dp
params.width = 50;
// initialize new parameters for my element
myBar.setLayoutParams(new LinearLayout.LayoutParams(params));

Categories

Resources