Changing background of LinearLayout changes it's width & height - android

I tried to change background of a LinearLayout to some wood texture, but it changes LinearLayout width and height depending on dimensions of the wooden background!
I need LinearLayout to disregard background dimensions!
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/wood"
android:orientation="vertical" >
some content here which I need to wrap height to them!
</LinearLayout>

Your LinearLayout's height is "wrap_content". Which means it takes height of the content(your background). You can change it to "match_parent" or give it a fixed size in dp. So that it remains same even with background.

If You want it to be of fixed either use fixed height and width in dp Or dynamically calculate height and width of old background and then apply new background with setting LinearLayout height and width to be of size you calculated programatically.

I simply replaced it with another container such as ConstraintLayout.. I don't have time to figure it out.

Related

wrap_content doesn`t work with background

There is Frame Layout, where width\height = wrap_content. But it increases to max size when I try to set a background, even if it doesn't contains any elements.How to fix it?
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android";
xmlns:app="schemas.android.com/apk/res-auto";
android:background="#drawable/oblakol"
adroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>
Your background is much bigger than you think. Check size of background!
Wrap content is working but your image size is large and hence you are not getting the expected results.
Try importing image as -> IMAGE ASSET so the size will be auto-Adjusted
or
set size manually!
I hope this will help you ...
wrap_content will put the background of it's original size.
Using large resolutions will cause lagging in your app while using. So, you should resize your background according to your needs : Image Resizer
When you set the dimensions(layout_width or layout_height) of a view to WRAP_CONTENT, you are giving freedom to the view to expand without any limit(as per its content ). Same is with your scenario as you've set the height and width to WRAP_CONTENT for your Frame Layout.
So whatever the space occupied on screen, its due to the large size of your android:background="#drawable/oblakol" drawable image. Try setting the height and width of the Frame Layout to some random values to test it.
android:layout_width="250dp"
android:layout_height="250dp"
I've attached some screenshots for your reference.
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 800x799(wxh)
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 256x256(wxh)
android:layout_width and android:layout_height set to 250dp. Actual dimension of the android Image is 800x799(wxh)
Hope it helps.

Get Height of LinearLayout or RelativeLayout in Android programmatically

I need to get the length of LinearLayout or RelativeLayout programmatically in Android like if I have a LinearLayout as follows
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
I need to increase height of the layout which is located in the mid of the screen by taking screen height and increase or decrease its height by taking calculations on the basis of screen height. So is there is a way of calculating the height of a Layout whose height is set as wrap_content. I tried to get Height from Layout Parameters but it gives me the value of -1 or -2 not height in pixels or density pixels.
Of course match_parent and wrap_content equals like -1 and -2.
In this case try to use viewTreeObserver.
Here a good link: http://www.sherif.mobi/2013/01/how-to-get-widthheight-of-view.html
Try the following:
myLayoutRef.post(new Runnable() {
int height = myLayoutRef.getHeight;
});
This runnable runs after your layout has been drawn and laid out on screen, so get height returns the real height and not 0

how to make listview height take all available space on the screen

How do I make listview height take all available space on screen, even if there is only one item on the list that is 100dpi tall?
Simply set the width and height of the ListView like this :
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
use match_parent as width and height, and also make sure your background image (if you're using one) is not limiting the height. In that case, if your bg image is larger, the height will go higher and same if smaller. If that's the case, then you should move your background to a parent view like a FrameLayout or a LinearLayout

Linearlayout background image

I have a problem with Linearlayout background. The issue is that I am setting LinearLayout background as image. Lets say image size is 300 px height. This LinearLayout holds other views lets say rows. One row is 100 px height. The problem is that when I am adding a row to this LinearLayout, row fill only 100 px of 300 px. The question would be is it possible to strech or scale LinearLayout depending on how much it's children views take place?
The code of LinearLayout:
<LinearLayout
android:id="#+id/companies_list_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="#drawable/firm_list_background"
android:orientation="vertical" />
Your question is not so clear so:
First of all when you set an image as the background of a layout it is scaled to fit or cover the whole size of the layout.
Another method to do the same think is to add an imageview in your layout with android:layout_width="match_parent"
android:layout_height="match_parent"
and set its background resource to android:scaleType:"fitXY"
So:
1: You have set your companies_list_holder height to wrap_content so if a row is 100px height. it also has 100px height so the background is scaled to fit your 100px height layout.
2: If you had set your layout to have a 300dp height it would be scaled to fit your 300dp layout etc etc. then you could add your 100dp height child rows.
Finally i think that if you want to add multiple and unknown number of rows in a layout, the best solution is a ListView like in this example:
http://www.mkyong.com/android/android-listview-example/
and your listView would handle many thinks like scrolling etc
You can first get the height of your inner row and then set this height as height of your LinearLayout using LayoutParams

Android wrap_content and fill_parent at the same time?

Is it possible to achieve something like this:
I have one picture to set as a background on layout in which is also TextView. So I want that layout to set his height to wrap content (which should be the height of TextView) and ImageView to has fill_parent as height and width.
I tried using:
LinearLayout with background and TextView
TextView with background
RelativeLayout with ImageView (set height to fill_parent) and TextView (set to wrap_content)
Is there a way to do this?
Sorry, if it's next question about the same, but I have no idea how to find a solution.
EDIT:
I'll try to be more specific. I want a TextView with background. But I want that background to be scaled to fit TextView which should be wrap_content width and wrap_content height. But I have image in higher resolution so there is a lot of unused space (background doesn't scale).
No, there is no way to set a view's width/height to both wrap_content and fill_parent simultaneously.
What exactly are you trying to achieve? Is this what you mean?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
...
android:background="#drawable/image" >
<TextView
android:layout_width="wrap_content"
android:layout_width="wrap_content"
... >
</TextView>
</LinearLayout>
EDIT
If you want to nicely scale a background image for a TextView, I think what you might want to look into is a 9-patch image. See this post for more info. Also this.

Categories

Resources