How to set ImageView in CardView properly - android

So I'm making a gallery like app and it needs some ScrollView and CardView inside it.
This is the screenshot when I put some background in the ImageView
This is how it suppose to look, I have removed the background image here
I have tried to put an ImageView to inside the CardView with a Linear Layout but the Image filled the ScrollView Instead.
fun CreateCardView(ItemCount: Int){
for (i in 1 until ItemCount){
val card_view = CardView(this)
val param = GridLayout.LayoutParams()
param.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)
param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)
card_view.layoutParams = param
val param2 = card_view.layoutParams as GridLayout.LayoutParams
card_view.layoutParams = param2
subLayoutGrid?.addView(card_view)
val linearLayout = LinearLayout(this)
val layoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, // CardView width
WindowManager.LayoutParams.WRAP_CONTENT // CardView height
)
linearLayout.layoutParams = layoutParams
linearLayout.setBackgroundColor(Color.parseColor("#135517"))
card_view.addView(linearLayout)
val imageView = ImageView(this)
val imageParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT
)
imageView.layoutParams = imageParams
imageView.setBackgroundResource(R.drawable.animaleight)
val layout = findViewById<LinearLayout>(finallinearId)
layout.addView(imageView)
}
}
How can i achieve something like on the image 2 using ImageView

I beleive it happens because u set width and height of your cardView as WRAP_CONTENT, and images that u put here is very big, so you should either change the size of images or use fixed size of cardView like 100dp height and 100dp-width, but that is not the best option or you can try to change your xml file, if you will put your xml file I will help you with that.
Also as long as it is LinearLayout try just put to every item "weight" parametr "1"

Related

Val cannot be reassigned while I try to create LinearLayout dynamically

I'm trying to create new LinearLayout with kotlin inside MainActivity.kt
for (i in 0 until tileArr.size){
var tileLayout: LinearLayout = LinearLayout(this)
tileLayout.marginBottom = 10
}
while it throws error Val cannot be reassigned on line: tileLayout.marginBottom = 10
You cannot modify those properties directly, you need to use LayoutParams.
for (i in 0 until tileArr.size){
var tileLayout: ViewGroup = LinearLayout(this)
val params = <Parent ViewGroup Type>.LayoutParams( // if the parent is FrameLayout, this should be FrameLayout.LayoutParams
LinearLayout.LayoutParams.WRAP_CONTENT, // modify this if its not wrap_content
LinearLayout.LayoutParams.WRAP_CONTENT // modify this if its not wrap_content
)
params.setMargins(0, 0, 0, 10) // last argument here is the bottom margin
tileLayout.layoutParams = params
}

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

Set CardView height programmatically

I am new to Android dev and I'm having trouble trying to set the min height of a it.gmariotti.cardslib.library.view.CardView programmatically. I create a new instance of a CardView and set all the xml elements in code, but it does not affect the cardView. I do not inflate the cardview from xml.
CardView catCard = new CardView(getActivity());
catCard.setBackgroundColor(Color.green(0));
catCard.setMinimumHeight(10);
catCard.setBottom(0);
catCard.setExpanded(false);
CardView extends FrameLayout so you should be able to set LayoutParams. Try something like this :
CardView.LayoutParams layoutParams = (CardView.LayoutParams)
catCard.getLayoutParams();
layoutParams.height = 10;
, dont forget that setting Width is also required.
Or create new LayoutParams like this (not tested) :
CardView catCard = new CardView(getApplicationContext());
// sets width to wrap content and height to 10 dp ->
catCard.setLayoutParams(new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT, 10));
catCard.setMinimumHeight(10);
myCardView.setLayoutParams(new RelativeLayout.LayoutParams(20, 20));
it depends that your cardview is a child of which layout, in this case it is under Relative Layout
My Solution with margin and it worked perfectly with me
val resources = context.resources // get resources from context
val width: Int = resources.displayMetrics.widthPixels / 3 //get width
val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, resources.displayMetrics).toInt() //set margin
val layoutParams = ViewGroup.MarginLayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.marginStart = px
layoutParams.marginEnd = px
cardView.layoutParams = layoutParams

How to programmatically set the width of the LinearLayout?

Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

Image inside FrameLayout not shrinking though scaletype set to fitcenter

I have an ImageView that must be wrapped inside Framelayout. But when I use FrameLayout, the image is clipped within the bounds of the ImageView and not made "fit" even if I use setScaleType(ScaleType.FitCenter); but when I use otherViewGroupsuch asRelativeLayout`, the result is ok. why? what can I do? Here is my code:
FrameLayout profileimage_fl = new FrameLayout(
getSherlockActivity());
layoutWidth = WindowSize.convertedHeight(150);
layoutHeight = WindowSize.convertedHeight(150);
p_ll = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
leftMargin = WindowSize.convertedWidth(1);
topMargin = WindowSize.convertedHeight(1);
rightMargin = WindowSize.convertedWidth(1);
bottomMargin = WindowSize.convertedHeight(1);
p_ll.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
profileimage_fl.setLayoutParams(p_ll);
profileimage_fl.setBackgroundColor(Color.LTGRAY);
ImageView profileimage = new ImageView(getSherlockActivity());
layoutWidth = WindowSize.convertedHeight(150);
layoutHeight = WindowSize.convertedHeight(150);
p_fl = new FrameLayout.LayoutParams(layoutWidth, layoutHeight);
profileimage.setLayoutParams(p_fl);
profileimage.setScaleType(ScaleType.FIT_CENTER);
profileimage.setPadding(WindowSize.convertedWidth(0),
WindowSize.convertedHeight(0), WindowSize.convertedHeight(0),
WindowSize.convertedWidth(0));
profileimage_fl.addView(profileimage);
then I add profileimage_fl to a viewgroup that I inflated in a layout. That layout xml contains only that viewgroup. You see, I don't usually use xml.

Categories

Resources