how to fit layout width inside of a horizontalscrollview? - android

I want to fit width of the LinearLayout horizontally but there is some blank space in end of it(see the picture)Picture
this is my code :
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shahin.testing2.MainActivity">
<HorizontalScrollView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40px"
android:id="#+id/layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/Linear"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
how can i change this code that there would be no blank space at the end?

finally i got the answer , i must declare the gravity attribute to center_horizontal in parent linear_layout.

Related

Andorid: How do I align all the items in Relative Layout to the bottom of the screen

I have put a linear layout inside a relative layout and I am trying to align the parent relative layout to the bottom of the screen.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:id="#+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fruits"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Message: " />
<EditText
android:id="#+id/message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Im basically trying to center a Text and there is a Text and Edit Text underneath it and align them all to the bottom of the screen
Fruits
Message: ___________________________
use android:layout_alignParentBottom="true" check running the solution below in your project.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:id="#+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fruits"
android:layout_alignTop="#+id/linearLayout2"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/linearLayout2">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Message: " />
<EditText
android:id="#+id/message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Change This:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
to This:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
You need to use
android:layout_alignParentBottom="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" // add this to you layout
android:layout_margin="#dimen/margin_10"
android:background="#drawable/button_selector"/>
</RelativeLayout>
Simple! You have to change the Parent RelativeLayout's height from wrap_content to match_parent. So that it will take whole layout's height and it will append to the bottom of the screen.
set layout_above property to fruits TextView to bottom LinearLayout
Try this
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fruits"
android:layout_above="#+id/bottomLayout"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:id="#+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Message: " />
<EditText
android:id="#+id/message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

Android:relative layout center things in their own space

I'm new to android and I would like to achieve something like this, where all thins are centered in their own space.
representative image
I've this code... How can I change it, and why?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
tools:context="se.avatarcontrol.MainActivity">
<TextView
android:id="#+id/weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/light"
android:layout_marginTop="15dp"
android:text="RAINY" />
<Button
android:id="#+id/light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Light OFF" />
<TextView
android:id="#+id/frontText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/sideText"
android:text="FrontView"/>
<TextView
android:id="#+id/sideText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/light"
android:layout_marginTop="15dp"
android:layout_alignParentRight="true"
android:text="SideView" />
<ImageView
android:id="#+id/frontImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: android:layout_below="#id/frontText"
android:src="**somesource**" />
<ImageView
android:id="#+id/sideImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/sideText"
android:layout_alignParentRight="true"
android:src="**othersource**"/>
</RelativeLayout>
Another thing would be how can my ImageViews fill all the space whithout overlapping each other?
please use FrameLayout or LinearLayout because it's lighter than RelativeLayout.
To achieve what you want, you should do something like :
<?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">
<!-- Weather light Layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:src="#mipmap/ic_launcher_round" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<!-- Front + Image Layout-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Front" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Image" />
</LinearLayout>
<!-- Side + Image Layout-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Side" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Image" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
This is the structure. Just adapt your padding / margin / ids and layout_height and width. For your case, i used layout_weight which is useful to specify a size ratio between multiple views.

How to put buttons below scrollview and always show button without being overlapped by scrollview in Android?

My layout is like this
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nextChar_btn" />
</LinearLayout>
</LinearLayout>
However, when there are little or no text, the buttons are pulled up, when there are too many text in search_results TextView, the buttons won't show up because scrollview takes the space all the way to the bottom. Any suggestion to always keep the buttons at the bottom and always visible?
First of all you have to set the ScrollView's height to 0dp.
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/search_results"
android:textSize="#dimen/text_font_size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
You have to do this because the weight param is going to control the size of the view depending on how much space is available. If you set it to wrap content, it will take how much space it needs to wrap everything within.
Then you need to remove your LinearLayout's weight param:
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
And the reason for doing that is just the opposite of the previous explanation: in this case the wrap content is going to take the space necessary to always display the buttons within.
Here is some sample code I put together for you. You basically need to take out the linear layout you have wrapping you xml layout and use a relative layout instead. Once you do that you can add ids to the linear layout with the buttons and then have the edit text align to the parent top. Then you can put the scrollview below the edittext and then put the scrollview above to the linear layout containing the buttons. This should work!
<?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="match_parent"
tools:context="com.example.thegamefan93.loginregister.LoginActivity">
<EditText
android:id="#+id/searchKey"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:hint="xx" />
<ScrollView
android:layout_weight="1"
android:fillViewport="true"
android:layout_below="#id/searchKey"
android:layout_width="match_parent"
android:layout_above="#+id/LL1"
android:layout_height="wrap_content">
<TextView
android:id="#+id/search_results"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center|bottom"
android:layout_weight="1"
android:id="#+id/LL1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/lastCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last_Char" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next_char" />
</LinearLayout>
</RelativeLayout>
Try this,
<?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">
<EditText
android:id="#+id/searchKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="xx" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_below="#+id/searchKey"
android:layout_weight="1"
android:fillViewport="true">
<TextView
android:id="#+id/search_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="#dimen/text_font_size" />
</ScrollView>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/lastCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/lastChar_btn" />
<Button
android:id="#+id/nextCharacterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/nextChar_btn" />
</LinearLayout>
</RelativeLayout>

How to get this layout in a scrollview?

So i want to have a layout like on the image :
An image to the left then the title and then below of those the text.
I can't seem to do it while having a scrollview!
So this is my code while NOT using the scrollview :
<?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">
<ImageView
android:id="#+id/imageViewH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/header" />
<TextView
android:id="#+id/textViewIntroText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:text="#string/intro" />
<ImageView
android:id="#+id/imageViewLOGO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/klein" />
<ImageView
android:id="#+id/imageViewBlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/imageView3"
android:src="#drawable/block" />
<TextView
android:id="#+id/textViewBMWText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView3"
android:text="#string/bmwtext" />
<TextView
android:id="#+id/textViewBMWTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewIntroText"
android:layout_toRightOf="#+id/imageView2"
android:text="#string/bmw" />
How do i achieve the same results but with a scrollview?
Anyway, thanks for your time! I appreciate every answer/comment!
I dont have my AS near to me but you can try with this please:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_height="match_parent"
android:layout_height="match_parent">
//all your xml here
</RelativeLayout>
</ScrollView>
Set the RelativeLayout to wrap_content. If the child view is the same size as the ScrollView, obviously it will not scroll.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>

Set bottom gravity to a single view Android

I have a horizontal linear layout, with 3 buttons, on the display. I want this layout to be displayed in the bottom of the display.
Here is the code I wrote:
<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="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context="flowepower.tetris.Game"
android:background="#drawable/Main_screen">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal"
</LinearLayout> </RelativeLayout>
I haven't put the buttons code, they are simple ones.
With this code, it is still displayed on the top. Can you suggest me other posibilities? Thank you!
You can try this:Layout
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />
</LinearLayout>
</RelativeLayout>
Hope this is what you want...
Hope this code works for you.
<?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:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
As you are using RelativeLayout write android:alignParentBottom="true" in your LinearLayout tag.
<RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:alignParentBottom="true">
</LinearLayout>
</RelativeLayout>

Categories

Resources