Expandable layout - can't with change layout params - android

I have problem with changing Exapandable layout params. I'm using layout from this library:
https://github.com/AAkira/ExpandableLayout
I put ImageView inside this layout and than when I download picture into ImageView I want my layout to wrap this image.
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="#+id/expandable_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/event_accepted_people_scrollview"
android:layout_alignParentStart="true">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/card_map_icon"
android:layout_width="70dp"
android:layout_height="70dp"
app:srcCompat="#drawable/vector_drawable_ic_map_black___px"
android:tint="#color/skilltrade_grey"
android:scaleType="center"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:background="#drawable/ripple_effect"/>
<TextView
android:id="#+id/no_map_snapshot_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_map_snapshot"
android:gravity="center_horizontal"
android:layout_marginBottom="15dp"
android:layout_below="#id/card_map_icon"/>
<ImageView
android:id="#+id/carditem_map_snapshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/details_map_textview"
android:scaleType="center"
android:layout_marginTop="5dp" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
Problem is that layout wraps only AppCompatImageView and TextView because ImageView source is not set and it's height is 0. After downloading picture layout is not wraping content correctlly...
That's what I'm doing now:
-I download picture in size 640x480.
-Than I scale it to match my screen
-After that when I know height of my image I change params of my Expandable layout like this:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mapSnapshot.getLayoutParams();
params.width = width;//scaled picture width
params.height = height;//scaled picture height
mapSnapshot.setLayoutParams(params);
expandableArea.updateViewLayout(mapSnapshot, params);
Unfortunately it's not working and layouts height is still not matching ImageView...
I would be grateful for any help :)

Related

How to auto width and height?

I am making app with Android Studio and want to auto set width and height of image that I am showing on front page. my code is like this
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView"
android:layout_width="375dp"
android:layout_height="220dp"
android:layout_marginBottom="5sp"
android:adjustViewBounds="true"
android:background="#drawable/japuji"
android:clickable="true"
android:focusable="true"
android:linksClickable="true"
android:scaleType="fitXY"
android:textStyle="bold" />
this is the scrollview and when i set fill_parent, wrap_content in TextView its not working either, when i test it on big screen images are small ,
i have tried all like changing width to fill_parent and wrap_content
You should not display an image as a background of a view, instead, you should use ImageView
<ImageView
android:contentDescription="#string/imagedesc"
android:id="#+id/my_image"
android:src="#drawable/your_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
src : sets the image drawable
scaleType : sets the desired scale type, in this case fitCenter with adjustViewBounds set to true and width or height (one of them) to wrap_content will show the full image.
EDIT :
if you have a fixed view size and you want to cover it all with that image drawable, you should use centerCrop as a scaleType instead of fitCenter
You're apparently displaying the image (android:background="#drawable/japuji") in the TextView, but the TextView has its height and width hard-coded: android:layout_width="375dp" android:layout_height="220dp"
Instead set the TextView's height and width to match_parent.

Wrap Content ImageView display full size of image

I am using imageview in android and when I put image in imageview using attributes wrap_content for both height and width. Images appear to be so much big as they are of real size or if i took small images they got blur.
code for it
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/showhide"
android:layout_marginRight="20sp"
android:layout_marginLeft="20sp"
android:layout_marginTop="10sp"
android:padding="5sp"
android:weightSum="4"
>
<!-- delivery intimation-->
<LinearLayout android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/layout_d_i"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1"
android:weightSum="2"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.5"
android:src="#drawable/delivintimation"
android:id="#+id/delivery_intimation"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginBottom="10sp"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delivery Intimation"
android:gravity="center"
android:maxLines="100"
android:singleLine="false"
android:layout_below="#id/delivery_intimation"
android:textSize="12sp"
android:textColor="#android:color/black"
/>
</LinearLayout>
</LinearLayout>
Use
android:scaleType="centerInside"
Did you try using android:scaleType attribute?
Example:
android:scaleType="centerCrop"
This attribute uses many other values like, 'fitXY', 'center', 'centerInside' etc. Try this
You can manually give width and height programatically
<ImageView android:layout_width="100dp"
android:layout_height="200dp"
android:src="#drawable/notes"
android:id="#+id/iv_notes"
android:scaleType="fitXY"/>
scaleType-- fitXY will compress and fit all image inside imageview
scaleType-- centerCrop will crop the image to the image view size
If you dont want to hard code the Width and height you have to create different drawable folders and place your different resolution images in different folder , Examples of drawable folders are
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.3 or 0.5 etc, then the ImageView will be according to your size.
Here is an example for you showing how to set the image half the screen size
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#ff0000"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_image"
android:adjustViewBounds="true"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#00ff00"
/>
</LinearLayout>
If you want to put your image in center of the screen, you can use the same idea and set proper weight to achieve your goal
use src instead of background in xml
please give the xml to see how you implemented it
Use fixed size width and height on imageview.
<ImageView
android:layout_width="200dp"
android:layout_height="180dp"
android:id="#+id/imageView"
android:src="#drawable/image"
android:scaleType="fitXY"/>
The code which you have used gave you the correct result: you have used LinearLayout and used weights. weights will divide according to the screen resolution. just remove LinearLayout and use Relativelayout instead. Next remove all the weights for images and set its attributes to wrap_content. If you want to give images that fits exactly to your screen width do it programmatically.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int desiredWidth=width/2;
yourView.setHeight(desiredWidth);
if this is not your requirement then in your RelativeLayout where you have these images. give attributes . android:alignParentRight="true" for one and android:alignParentLeft="true" for another image and set margins.

layout_height should be a function of layout_width

Assuming portrait mode, I have an ImageView who's image will be of variable size (yet always square). I would like the ImageView's width to always be equal to the layout's width (fill_parent) and as you can see the scaleType is "centerCrop" which will scale.
The problem is that if I set the *layout_height* also to "fill_parent" then, being that centerCrop retains the original aspect ratio, the height takes precedence and the image is cropped on the left and right. (this of course assumes a screen that is taller than it is wide). However, if I set the layout_height to "wrap_content", then the height is no longer scaled and I end up with a cropped rectangle. It does this because it respects the original height of the image.
Is there a layout_height setting that will give precedence to a layout_width of "fill_parent"?
<ImageView
android:id="#+id/iv_image"
android:layout_width="fill_parent"
android:layout_height="?"
android:layout_below="#id/header"
android:layout_centerHorizontal="true"
android:contentDescription="#string/cd_image"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="#drawable/blank_album" />
Removing the android:layout_height attribute causes a crash:
java.lang.RuntimeException: Binary XML file line #16: You must supply a layout_height attribute.
Setting android:layout_height="0dp" crushes the image vertically.
Answer: Using a combination of responses from other users, I was able to find a simple solution that works well for me.
First, the image view has this configuration (note the fitStart):
<ImageView
android:id="#+id/iv_album"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/header"
android:layout_gravity="top"
android:background="#FF00FF00"
android:contentDescription="#string/app_name"
android:padding="0dp"
android:scaleType="fitStart"
android:src="#drawable/blank_album" />
Then programmatically, a small modification to the height of the image to match the width (within Activity.onResume())
ImageView iv = (ImageView)findViewById(R.id.iv_album);
boolean bPost = iv.post(
new Runnable()
{
public void run()
{
ImageView iv = (ImageView)findViewById(R.id.iv_album);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv.getLayoutParams();
params.width = ASong.this.findViewById(R.id.song_view_layout_top).getWidth();
params.height = params.width;
iv.setLayoutParams(params);
}
});
if (bPost == false)
{
throw new RuntimeException("runnable not posted");
}
I had to deal with this issue yesterday. This is the XML that solved my problem:
<ImageView
android:id="#+id/character"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/ma_un1_001" />
see thread here: Center drawable and scale height
To cut a long story short:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
Alternatively, you can do it programmatically:
Surround the ImageView with a LinearLayout
<LinearLayout
android:id="#+id/container"
android:layout_below="#id/header"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/iv_image"
android:layout_width="fill_parent"
android:layout_height="?"
android:contentDescription="#string/cd_image"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="#drawable/blank_album" />
</LinearLayout>
Then, in your onCreate(), insert
findViewById(R.id.container).post(
new Runnable() {
public void run() {
LinearLayout container = (LinearLayout)findViewById(R.id.container);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)container.getLayoutParams();
params.height = params.width;
container.setLayoutParams(params);
}}
);
The code must be into post() because the main process can't directly modify the layout structure.

ImageView increases and decreases dynamically after the size of a textfield

I have a layout like this one:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#272727"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#null"
android:src="#drawable/lin" />
<TextView
android:id="#+id/textView1"
android:layout_width="275dp"
android:layout_height="50dp"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="100dp"
android:text="HEADER"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="2.2dp"
android:src="#drawable/logo" />
The ImageView is placed to the right of the screen. But I'm facing an issue, the ImageView increases and decreases in height and width depending on the length of the TextView, how can I make this work properly, without the ImageView changing size?
The problem here is that your ImageView matches the size of your parent, that is your LinearLayout (with id tabBar).
That LinearLayout wraps the content on his height, so, it sums all "heights" of his contents. The LinearLayout will take the height of TextView + the height of the ImageButton. Then, the ImageView will match that height.
A solution depends on what you are trying to do, but you can try to:
Set a predefined height in your ImageView
Use a RelativeLayout instead of a LinearLayout and align all your views depending on each other
My suggestion is to set layout_weight to all the components so that their size is fixed regardless of their contents:) See here for more details

How can i set my image to the bottom of the screen in android using xml code

hi i have displayed a PNG image on screen in android by using XML code:
android:src="#drawable/grass"
but it is showing image at top of the screen ,now i want that image at bottom of the screen.
make sure your layout file is taking full of your screen by setting match_parent or fill_parent for both layout_height and layout_width.
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/grass"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/grass"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
EDIT
Programmatically
ImageView pic;
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
pic = (ImageView) findViewById(R.id.pic);
pic.setLayoutParams(param);

Categories

Resources