LinearLayout doesn't work properly - android

How can I put the TextView exactly below the ImageView?
I write the code below, but it put the TextView with margin below the image,
I also try different scaleType for ImageView.
<?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="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/list_row_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#drawable/nature_bg" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello" />
</LinearLayout>

Add android:adjustViewBounds="true" attribute for ImageView. Set this to true will adjust its bounds to preserve the aspect ratio of its drawable.
<ImageView
android:id="#+id/list_row_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:adjustViewBounds="true"
android:src="#drawable/beekeeper_logo" />

You can use a relativeLayout instead, and place the textview below the image like that :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/list_row_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#drawable/nature_bg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:layout_below="#id/list_row_logo"/>
</RelativeLayout>

Related

Android XML: Vertical Seekbar in Android with match parent layout isn't working properly

I have seen a different post related to this topic already before posting this question so I can definitely say that this question is not a duplicate. I have a different problem which I couldn't fix or find a solution to it.
Problem: I have created a Vertical Seekbar using new <SeekBar> </SeekBar> view. It does look fine when I see the preview in Android Studio but on the device, the SeekBar doesn't fill the height of the screen. I have rotated the SeekBar view with 90deg and try to set the height:match_parent and width:wrap_content but it isn't working at all as I expected. So I set the height to some hard-coded value and it looked fine. The only problem is, I will have to check the height of the view in which it is getting populated and set the height of the SeekBar problematically, which I want to avoid.
slider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/roundcorner"
android:backgroundTint="#color/design_default_color_primary"
android:gravity="center"
android:layout_gravity="center"
android:elevation="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#fff"
android:text="#string/RangeValue"
android:paddingTop="16dp"
android:paddingBottom="16dp"/>
<FrameLayout
android:layout_weight="1"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:id="#+id/seeker"
android:layout_width="wrap_content" <!-- Set this to 400dp and it looks fine -->
android:layout_height="match_parent"
android:rotation="270"
android:layout_gravity="center"/>
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_round_done_24px"
android:layout_gravity="center"
android:padding="16dp" />
</LinearLayout>
Update
Thank you Reaz for your answer
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
android:background="#fff"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/maingrid"
android:background="#color/colorAccent"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:id="#+id/radarCanvas"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical">
</LinearLayout>
<include
android:layout_alignParentBottom="true"
layout="#layout/biosignals"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--- this is where i am including slider layout -->
<include
android:layout_alignParentRight="true"
layout="#layout/slider"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="24dp" />
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
You are setting weight inside the Framelayout when the orientation is vertical and at the same time the height of FrameLayout is set to match_parent. If you are setting like this there will be no effect of the weight. Try setting the height to 0dp it will work for sure.
<FrameLayout
android:layout_weight="1"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="0dp">
<SeekBar
android:id="#+id/seeker"
android:layout_width="wrap_content" // set this to 400dp and it looks fine
android:layout_height="match_parent"
android:rotation="270"
android:layout_gravity="center"/>
</FrameLayout>
This is my result
You need to get rid of your FrameLayout which is limiting the width of the SeekBar. I tried to implement the layout like the following and I think it should fit in your case as well.
Another important thing you should notice that, because of your rotation, the width and height are interchanged in the view for your SeekBar. Hence I made the layout_width="match_parent". Hope that helps!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:elevation="5dp">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:padding="16dp"
android:text="100"
android:textAlignment="center" />
<SeekBar
android:id="#+id/seeker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:rotation="270" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:padding="16dp"
android:src="#android:drawable/star_on" />
</RelativeLayout>
Here is the result that I have got.
<RelativeLayout
android:layout_width="400dp"
android:layout_height="200dp"
android:layout_gravity="center">
<SeekBar
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:maxHeight="4dp"
android:minHeight="4dp"
android:progress="50"
android:progressDrawable="#drawable/custom_seek_bar"
android:rotation="270"
android:thumb="#drawable/seekbar_thumb" />
</RelativeLayout>
Use this code, you will get desired result

layout does not fit the screen

I have created a layout. But at the bottom there is a white space left. I want the content of the relative layout at the extreme bottom of the screen. i.e seekbar and button at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/images">
<com.example.acer.myapplication.ZoomView
android:id="#+id/iop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="#+id/seekBar7"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button61"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/seekBar7"
android:layout_centerInParent="true"
android:background="#drawable/play" />
</RelativeLayout>
</LinearLayout>
I want the white space at the bottom should be replaced with the content of relative layout.
I have uploaded the image
use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/images">
<com.example.acer.myapplication.ZoomView
android:id="#+id/iop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<SeekBar
android:id="#+id/seekBar7"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button61"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/seekBar7"
android:layout_centerInParent="true"
android:background="#drawable/play" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
instead of wrap_content You should put match_parent.
That would help it out. I hope you know about wrap_content and match_parent.If you don't look at this
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

How to put Text View above the Image and at the bottom of Layout

I have tried almost everything from Gravity to alignParentTop and layout_above but nothing seems to be working here.
I have an ImageView and a TextView I am using ViewPager to display different images and their respective text.
The Problem is that, its displaying Text at the top and image after it.
I want it to display Image and beneath it display TextView.
This is what i am getting after adding following xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Image Text"
/>
</RelativeLayout>
Kindly guide me how to put Text after the Image
Try this it may help you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="300sp"
android:src="#drawable/wallpaper"
android:scaleType="centerCrop"
android:id="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is test"
android:textColor="#000"
android:textSize="20dp"
android:gravity="center"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Using the android:layout_below attribute should solve your problem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_below="#+id/image_view"
android:text="Image Text"
/>
</RelativeLayout>
Add android:layout_below="#+id/image_view" in your TextView
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/image_view"
android:paddingBottom="8dp"
android:text="Image Text"
/>
</RelativeLayout>
Hope it helps you!!
You can define this structure tow way, first one define Textview property android:layout_below as ImageView id like bellow
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_view"
android:layout_gravity="bottom"
android:text="Image Text" />
</RelativeLayout>
And other one use LineLinearLayout and define orientation vertical like following
<?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="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Text" />
</LinearLayout>

Overlap Layout with ImageView

Im trying to create the following Layout:
The border of the RelativeLayout is realized with a rectangle shape as background, but I cannot get the ImageView overlap the RelativeLayout with half of it in and half of it out of the border. Is this even possible with RelativeLayout (And if not: What alternative do I have)?
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/imageresource"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:padding="#dimen/item_inner_margin"
android:background="#drawable/item_border"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relativeLayout"
android:layout_below="#+id/imageView">
<!-- Stuff in the layout -->
</RelativeLayout>
</RelativeLayout>
Thanks in advance
danijoo
This might be what you're looking for:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#fff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:paddingTop="30dp">
<!-- add content here -->
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageview1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:background="#android:color/holo_blue_bright"
android:elevation="4dp"
/>
</RelativeLayout>

Android. ImageView wrap in FrameLayout

I want imageView wrap in FrameLayout. And ImageView show fullscreen.
1 variant:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00" >
<FrameLayout
android:id="#+id/fl_draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00ff00">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
</FrameLayout>
</RelativeLayout>
(source: cs412821.vk.me)
2 variant:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00" >
<FrameLayout
android:id="#+id/fl_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00ff00">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
</FrameLayout>
</RelativeLayout>
(source: cs412821.vk.me)
AdjustViewBounds not working.
What I'm trying to achieve:
(source: cs412821.vk.me)
How do I handle this? Thank you very much. Sorry for my english.
Change your imageview attributes from :
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
To:
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
It also depends on how tall your image is.. If you know the width and height, you can adjust it accordingly.
Also, you can use gravity attribute if you want the want the image to be in the center position.

Categories

Resources