minHeight not working with weight=“1” in Linearlayout - android

I want a View that can have a minimum height or wrap_content if screen size is small and if screen size is large then occupy the rest of the space. This view is middle child of its parent.
Currently, I am using weight with LinearLayout. It's working fine with large screen but on a small screen its not showing the view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/speaker_detail_background"
android:clickable="true"
android:paddingLeft="#dimen/padding_7dp"
android:paddingRight="#dimen/padding_7dp"
android:orientation="vertical"
>
<ImageView
android:id="#+id/down_arrow_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:padding="#dimen/padding_15dp"
android:src="#drawable/down_arrow_icon" />
<RelativeLayout
android:id="#+id/lytSpeakerDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/down_arrow_image_view"
android:minHeight="200dp"
android:layout_weight="1"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<RelativeLayout
android:id="#+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:visibility="visible"
>
<com.harman.jblconnectplus.ui.customviews.CustomFontTextView
android:id="#+id/speaker_name_title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/edit_name"
android:drawablePadding="#dimen/padding_5dp"
android:ellipsize="end"
android:maxLength="16"
android:text="#string/speaker_name"
android:textColor="#android:color/white"
android:textSize="#dimen/textSize_18sp"
app:font="#string/font_name_opensans_semi"
/>
<com.harman.jblconnectplus.ui.customviews.CustomFontTextView
android:id="#+id/setting_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/speaker_name_title_textview"
android:layout_centerHorizontal="true"
android:text="#string/setting"
android:visibility="invisible"
android:textColor="#color/white"
android:textSize="14sp"
app:font="#string/font_name_opensans_semi"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/lytSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/title_bar"
android:layout_marginLeft="#dimen/brightness_margin"
android:layout_marginRight="#dimen/brightness_margin"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/darker"
android:layout_gravity="center_vertical"
/>
<SeekBar
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/seekBar"
android:thumb="#drawable/knob"
android:progressDrawable="#drawable/seekbar_progressbar"
android:indeterminate="false"
android:splitTrack="false"
android:maxHeight="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:progress="0"
android:secondaryProgress="0"
android:max="255"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/brighter"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/bottom_details_layout"
android:layout_height="wrap_content"
android:background="#drawable/round_border_white_bg"
android:orientation="vertical"
android:layout_marginTop="#dimen/margin_20dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_5dp"
android:gravity="bottom">
<include layout="#layout/speaker_detail_firmware_layout" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider_view" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/speaker_detail_voice_layout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please suggest.

Your View is being constrained. minHeight is not guaranteed to be honored if the View is constrained. See the documentation.
android:minHeight
Defines the minimum height of the view. It is not guaranteed the view will be able to achieve this minimum height (for example, if its parent layout constrains it with less available height).
Maybe a ScrollView is something that you can use since it gives you virtually unlimited space. The question is what you want to happen when space becomes an issue and everything just can't fit.

i had solve this problem. set weight "1" of layout in XML File(in my
case XML is same as Given above), it will work in case of if Screen
size is large and also check grammatically if size of view is less
than a minimum size then set height of view grammatically. sample
code:-
Set ViewTreeObserver to View:-
lytSpeakerDetails = (RelativeLayout) view.findViewById(R.id.lytSpeakerDetails);
ViewTreeObserver viewTreeObserver = lytSpeakerDetails.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
and in Listener's callback check if layout height is less than minimum then set height to minimum:-
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (!isAdded())
return;
lytSpeakerDetails.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = lytSpeakerDetails.getMeasuredHeight();
LinearLayout.LayoutParams imgViewParams = (LinearLayout.LayoutParams) lytSpeakerDetails.getLayoutParams();
/* imgViewParams.setMargins(0, (int) ( x * 1.5f) + (int) getResources().getDimension(R.dimen.card_shadow_y), 0, (int) x );
lytSpeakerDetails.setLayoutParams(imgViewParams);*/
if(height < (int)getResources().getDimension(R.dimen.brightness_minimum_height)) {
LinearLayout.LayoutParams rel_btn = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, (int) getResources().getDimension(R.dimen.brightness_minimum_height));
lytSpeakerDetails.setLayoutParams(rel_btn);
}
}
};

Related

Android set max width in percentage of a TextView in LinearLayout of vertical orientation

I'd like to set the layout_weight of the TextView with the tv_long_text to 80% in the following LinearLayout of vertical orientation.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
tools:text="a pretty long text" />
</LinearLayout>
The above is not working because the orientation of the textview's parent is vertical.
So, I tried to set the android:layout_width="match_parent" in the xml and then set the width at run time by getting the measured width and then sets the width to 80% but the getMeasuredWidth is giving me 0.
int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);
I also tried to set the layout_weight at run time but it didn't work either and it's probably because it's parent view is in vertical orientation.
longTextView.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT,
0.8f)
);
The one that worked for me is by adding some extra views for the long text view. But it's 2 more extra views added for just trying to set the width of this view in percentage. Is there any other efficient way to do this?
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
android:textStyle="bold"
tools:text="a pretty long text" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
</LinearLayout>
</LinearLayout>
I would not try to fiddle with run time measurements. Your solution with a second horizontal Layout is perfectly fine since you have two TextViews expanding horizontally.
Another option would be PercentRelativeLayout from the support library com.android.support:percent:25.1.0 See here
This is from the docs
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
use android:layout_weight
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"/>
</LinearLayout>
TRY This structure to let TextView flow only upto 75% of the screen width.
you can adjust SPACER width to achieve, what you want like 2 for 50%.
You can use android:weightSum, and achieve the same result without additional View like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="8"
android:textStyle="bold"
tools:text="a pretty long text a pretty long text a pretty long text" />
</LinearLayout>
</LinearLayout>
Solution with ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_rect_white_gray">
<TextView
android:id="#+id/tv_short_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="short text" />
<TextView
android:id="#+id/tv_long_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv_short_text"
app:layout_constraintWidth_percent=".8"
tools:text="very long text - 80% of screen width exact" />
</androidx.constraintlayout.widget.ConstraintLayout>

HorizontalScrollView display the views of the same size that the screen

I have a HorizontalScrollView with 3 ImageView. I need to have the images of the same width that the screen.
Can you help me please ?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:src="#drawable/placeholder"/>
<ImageView android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/placeholder"/>
<ImageView android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/placeholder"/>
</LinearLayout>
</HorizontalScrollView>
Change to android:layout_width="match_parent" in every ImageView and your LinearLayout, HorizontalScrollView
Yes, I'm also satisfy with 'Stanislav Parkhomenko'. If you need to have the images of same width of the screen, then change the value with android:layout_width="match_parent" in HorizontalScrollView as well as in every ImageView. That's all.
Since, screen width will vary across different devices, one of the ways you can achieve this is setting the width at runtime. For getting the screen width appropriately, we can use a ViewTreeObserver. Following is the xml file for the test that I derived from your layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#android:color/holo_red_light"/>
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#android:color/holo_blue_bright"/>
<ImageView
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Notice that I gave the android:id="#+id/ll" property to the layout containing the ImageViews. Now for the Java code, we can implement the ViewTreeObserver and set width accordingly at runtime:
final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
final ViewTreeObserver viewTreeObserver = ll.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
ll.getViewTreeObserver().removeOnPreDrawListener(this);
int width = ll.getWidth();
View child;
LinearLayout.LayoutParams params;
for (int i = 0; i < ll.getChildCount(); i++) {
child = ll.getChildAt(i);
params = (LinearLayout.LayoutParams) child.getLayoutParams();
params.width = width;
child.setLayoutParams(params);
}
return false;
}
});
This should do it. Let me know if you run into any issues.

View width and height is always zero in RelativeLayout in Android

I am developing an Android app. In my app, I am trying to set overlay on an image. But the size of the view in a relative layout is always zero in with and height even I set match_parent to both width and height.
This is my XML layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:id="#+id/di_card_container"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="#+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="#color/blue"
android:id="#+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="#color/white"
android:id="#+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is the result
As you can see, there is no blue color background for name because width and height of view in relative layout is zero. But if I set width and height explicitly like below, it is working.
<View
android:layout_centerInParent="true"
android:background="#color/blue"
android:id="#+id/destination_item_overlay"
android:layout_width="300dp"
android:layout_height="30dp"/>
But it is not compatible with all devices. Why with and height is always zero? I am adding that view because I need to programmatically control that view. How can I set width and height of that background view to match the parent?
Thats because you have a circular dependency. The <RelativeLayout> is asking its child to tell the height and the <View> is telling its parent to make it as tall as himself. If we fix the height, we can fix the issue immediately.
Try this here:
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="#+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="#color/blue"
android:id="#+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_alightTop="#id/di_tv_name"
android:layout_alightBottom="#id/di_tv_name"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="#color/white"
android:id="#+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Android layout_height to wrap smallest child

As part of a larger project, I'm attempting to create a custom image gallery which will hold images from the web. I have no control over their size and their layout (order, total number [up to ten], and number per row [up to three]) is defined by the API I'm working with. I'm using nested LinearLayouts to build the loose grid programmatically but this is an XML proof of concept...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="#drawable/photoset_vert_divider"
android:showDividers="middle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="#drawable/photoset_horz_divider"
android:showDividers="middle">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/wide" />
</LinearLayout>
</LinearLayout>
This is 60% of what I'm looking for and gets me
…but this is my target:
What I want is a android:layout_height="wrap_smallest_content" sort of thing that scales all the ImageViews to the correct size and then crops them all down to match the "shortest" child.
Since you say you're going to eventually do this programmatically, you could always just subclass LinearLayout to find the smallest height of the containing ImageViews as they load and then set that as the layout's height. Then all you'd have to do with the images is set their height to match_parent.
It's really better to set the fixed layout_height to inner LinearLayout. You can just do this in XML
If you're focusing on variety of devices then give a id to the inner LinearLayout then set the LinearLayout size dynamically by measuring the screen size of the device. To measure the screen size programmatically
By using the DisplayMetrics you can get height and width of the screen of any device.
here is the code.
DisplayMetrics metrics;
int width = 0, height = 0;
In your onCreate Method
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
xheight = metrics.heightPixels;
xwidth = metrics.widthPixels;
Try this to get the Screen height.
Then set the inner LinearLayout programmatically like this
linearLayout.getLayoutParams().height = xheight - 50;
Depending upon the xheight change the - 50 value
the your xml will change to something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="#drawable/photoset_vert_divider"
android:showDividers="middle">
<LinearLayout
android:id="#"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="#drawable/photoset_horz_divider"
android:showDividers="middle">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/wide" />
</LinearLayout>
</LinearLayout>

Make image appear half of the screen [duplicate]

This question already has answers here:
Assign width to half available screen width declaratively
(6 answers)
Closed 5 years ago.
I am having trouble to make my imageview appear half of the screen can someone help. here is my xml file. It should take up half of the screen size and also the linear layout with the textview should be at the footer of the imageview.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fond" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" >
<WebView
android:id="#+id/webcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp" />
</RelativeLayout>
<ImageView
android:id="#+id/image_actualitedetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/six_yamaha" />
<LinearLayout
android:id="#+id/linearvideo"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_alignBottom="#+id/image_actualitedetails"
android:layout_alignParentLeft="true"
android:background="#88FFFFFF"
android:gravity="center"
android:paddingLeft="#dimen/fourdp"
android:paddingRight="#dimen/fourdp" >
<TextView
android:id="#+id/text_actualites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#color/tabDark"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/header" >
<Button
android:id="#+id/boutton_retour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:background="#drawable/back" />
</RelativeLayout>
Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.5, then the ImageView should be half the 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>
And this is the result
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. Good luck!
Just if someone wants landscape version its almost the same :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:baselineAligned="true"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#ff0000" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/adele" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#00ff00" />
</LinearLayout>
First you'll need to get the display dimensions at runtime
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
windowWidth = size.x;
windowHeight = size.y;
Then you can go ahead and set your imageView height and width parameters to half of those values
int orgWidth = yourImageView.getDrawable().getIntrinsicWidth();
int orgHeight = yourImageView.getDrawable().getIntrinsicHeight();
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
windowHeight / 2, windowWidth / 2);
yourImageView.setLayoutParams(params);
yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
You have to put your imageview inside a LinearLayout with horizontal alignment, so you can use
android:weight = 0.5// this attribute for the image view will force the image to fill half of the screen, counting of course with the linear layout as the top parent layout(filling the hole window)

Categories

Resources