I am adding few image views inside a horizontal LinearLayout which is again inside a HorizontalScrollView. But the linear layout is not scrolling. Below is my code. Can someone guide me where exactly my xml formation is going wrong?
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:layout_marginBottom="2dp"
android:fillViewport="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/lyt_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_icon1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#drawable/default_ic" />
<ImageView
android:id="#+id/iv_icon2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#drawable/default_ic" />
<ImageView
android:id="#+id/iv_icon3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#drawable/default_ic" />
</LinearLayout>
</HorizontalScrollView>
I would suggest use Constraint Layout As Parent and Inside it Use Nested Scroll View With Linear Layout
Here is a sample code, modify according to your need.
<?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"
tools:context="suraj.iitgandhinagar.Login">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:layout_editor_absoluteX="72dp"
tools:layout_editor_absoluteY="0dp"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="36dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="272dp"
android:layout_height="237dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:src="#drawable/unnamed"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="59dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
Take a look at this situation. if you use the linear layout as match_parent then don't get the layout width. Use wrap_content. If its don't work then set the fixed size for your image.
<HorizontalScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:layout_marginBottom="2dp"
android:fillViewport="true"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/lyt_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_icon1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/iv_icon2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/iv_icon3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:layout_marginRight="1dp"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</HorizontalScrollView>
Related
I just started using android studio.. I am trying to align ImageButtons inside a scroll view side by side, but i cannot put them side by side, neither can i resize them from the left side. How do i align them side by side?
I already tried the solution given in one of the answers: Aligning TextView Side By Side Inside A ScrollView Inside a Linear Layout but it didn't work
<?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"
tools:context=".StoryIndex">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="#+id/titlee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
app:srcCompat="#drawable/alami1" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="375dp"
android:layout_height="116dp"
android:background="#android:color/black"
app:srcCompat="#drawable/alami2" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="195dp"
android:layout_height="169dp"
app:srcCompat="#drawable/alami3" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
I want the Image buttons to be side by side, but they are in rows, on top of each other.. but it shows like the one in picture:
Image of the design view
Thanks.
You have to change the orientation in LinearLayout from vertical to horizontal.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
Try this
<LinearLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
//The question you mentioned can be obtained by using weightSum
android:weightSum="15">
//replace src ic_launcher_background by your drawable source.
<ImageButton
android:id="#+id/titlee"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:src="#drawable/ic_launcher_background" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:src="#drawable/ic_launcher_background" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="0dp"
android:layout_weight="5"
android:background="#android:color/black"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Try this and let me know. Happy coding.
<LinearLayout 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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="15">
<ImageButton
android:id="#+id/imageButtonOne"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:src="#drawable/ic_launcher_background" />
<ImageButton
android:id="#+id/imageButtonTwo"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:src="#drawable/ic_launcher_background" />
<ImageButton
android:id="#+id/imageButtonThree"
android:layout_width="0dp"
android:layout_weight="5"
android:background="#android:color/black"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Try this if you still find any issue, let me know.
Show the Image buttons to be side by side, and in rows.
Change the orientation of LinearLayout from vertical to horizontal.
Please try below code:
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/titlee"
android:layout_weight=".3"
android:layout_width="0dp"
android:layout_height="116dp"
android:background="#android:color/black"
app:srcCompat="#drawable/stars_one"
android:layout_margin="4dp"/>
<ImageButton
android:layout_weight=".4"
android:id="#+id/imageButton8"
android:layout_width="0dp"
android:layout_height="116dp"
android:background="#android:color/black"
app:srcCompat="#drawable/stars_one"
android:layout_margin="4dp"/>
<ImageButton
android:layout_weight=".3"
android:id="#+id/imageButton9"
android:layout_width="0dp"
android:layout_height="116dp"
app:srcCompat="#drawable/stars_one"
android:layout_margin="4dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
The output of above code is like:
I have tried the below code but it doesn't scroll when I rotate my application it will cover most of screen but I need to scroll there it doesn't working there. Is it right way to use scroll view in constraint layout. I have seen this type of example. There it was in working condition I have tried the below code but it doesn't scroll when I rotate my application it will cover most of screen but I need to scroll there it doesn't working there. Is it right way to use scroll view in constraint layout. I have seen this type of example.
<?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"
tools:context=".MainActivity">
<include layout="#layout/tool_bar" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="611dp"
android:layout_marginTop="70dp"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/main_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/bmi" />
<ImageView
android:id="#+id/bmr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/bmr" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/ideal_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ideal" />
<ImageView
android:id="#+id/water_intake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/water" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/calorie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/calories" />
<ImageView
android:id="#+id/nutrition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/nutrition" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
follow this layer it might work.put appbarlayout inside ConstraintLayout layout
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--add your scrollview or nestedscrollview here-->
</LinearLayout>
</android.support.design.widget.AppBarLayout>
Your ScrollView height is fixed to 611dp.
If you use a fixed size, then the content should be greater than 611dp to be able to scroll.
You should use
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
....>
Currently i have this layout:
https://imgur.com/a/Y2a67
this is the code for this layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="#drawable/img_docu_placeholder"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="#drawable/img_docu_placeholder"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="#drawable/img_docu_placeholder"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="#drawable/img_docu_placeholder"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="#drawable/img_docu_placeholder"/>
</LinearLayout>
</FrameLayout>
</HorizontalScrollView>
What i'm trying to achieve is:
https://imgur.com/a/MYxkh
where i need to add additional 'X' button on the upper left side of the imageview
You can use FrameLayout. Your icon must have android:layout_gravity attribute.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:src="#mipmap/ic_launcher"/>
<ImageView
android:id="#+id/imgX"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="top|left" //This line
android:src="#mipmap/ic_launcher_round" />
</FrameLayout>
Hope it helps.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="108dp"
android:background="#ffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="#e6e6e6" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/ic_cancel_black" />
Output:
Support multiple resolution:
Using this library: https://github.com/intuit/sdp
Create multiple layouts for each screen and using dp
I am trying to learn about Animation and am using following layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="16dp">
<ImageButton
android:id="#+id/thumb_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/description_image_1" />
</LinearLayout>
</FrameLayout>
However, this will not center the ImageButton vertically. It does it only horizontally. I tried layout_gravity "center", "center_vertical", "center_horizontal" without success. The best I got is this:
but I need to get this:
Much appreciated
Try android:gravity="center" to your LinearLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="16dp"
android:gravity="center">
<ImageButton
android:id="#+id/thumb_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/description_image_1" />
</LinearLayout>
difference between gravity and layout_gravity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="16dp">
<ImageButton
android:id="#+id/thumb_button_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/description_image_1" />
</LinearLayout>
There is problem in wrap_content in layout height and width in your imagebutton widget
![I had coded it in linear layout (code given below) but want to know how to do the same using relative layout, or is that just a bad idea given the layout?
linear layout -
<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:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
/>
<TextView
android:text="You're invited!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="45sp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:background="#009688"/>
<TextView
android:text="Bonfire at the beach"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:paddingTop="8dp"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:textSize="24sp"
android:background="#009688"/>
][1]When using relative layout my ImageView does not appear on the screen:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"
/>
</RelativeLayout>
Its because you are using android:layout_height="0dp"
Please use android:layout_height="wrap_content"
Change your ImageView's height like this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="#drawable/ocean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"
/>
You can only use layout_weight in LinearLayout.
Try this.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertizal">
<ImageView
android:src="#drawable/ocean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="#+id/ocean"/>
android:layout_height="0dp" This is wrong in your ImageView
Change this to
android:layout_height="match_parent"
or
android:layout_height="wrap_content"
Basically you set your height as 0. So nothing to show.
Edit :
Also you can remove this android:layout_weight="1". No need for weight in RelativeLayout