So I'm creating a list view that grows and shrinks based off of the user input and I need four buttons but I don't know how to have buttons that are aligned with the bottom of the list view and are in a 2x2 grid fashion. I've already tried relative layout and it didn't seem to work. Thank you
I've created the XML according to what I believed you wanted to see.
Please check the following screen shot:
The following is the XML file:
<?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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON3"/>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON4"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#id/linearLayout1">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON1"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON2" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/linearLayout2">
</ListView>
</RelativeLayout>
As you can see that I am using the RelativeLayout as the base wrapper and within the RelativeLayout, I have 2 linearlayouts for the buttons (2 each) and finally the list view. The purpose for the LinearLayout is simply to add weight to the buttons so they would share the equal amount of horizontal space. If that does not fit your criteria, feel free to remove them. The main thing to learn is that relative layout allows you to use attributes such as alignParenBottom, layout_above, layout_below and so on. These attributes allow you to place your elements anywhere on the screen and adjust them according to these attributes.
Let me know if you have any questions.
Related
I want the list view to appear below the text view.
However, the text view is not visible and only the list view is displayed in full size. How do I fix it?
<?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:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/textView21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="채팅"
android:textColor="#000000"
android:textSize="20sp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
It happens because in the ListView you are using android:layout_height="match_parent.
You can do it in many ways, for example you can use the
android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
Something like:
<LinearLayout>
<TextView
../>
<ListView
android:layout_weight="1"
.../>
</LinearLayout>
you should set layout_height = "wrap_content" in listview:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I have such code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:padding="15dp"
android:background="#drawable/button"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFF" />
<pl.jawegiel.endlessblow.other.GameSurface
android:id="#+id/gameSurface"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
(...)
</RelativeLayout>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/left_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#22FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/right_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#99FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.drawerlayout.widget.DrawerLayout>
The problem is that my Button is not below GameSurface.
I put this Button as first one counting on that it will be below GameSurface but it does not work.
How to achieve that?
You need to change FrameLayout to another kind of ViewGroup. FrameLayout is a a type of ViewGroup where one View is on top of another one. Like Layers in a cake.
Putting one below another is not possible in FrameLayout
The easiest solution might be to change FrameLayout with LinearLayout and of course, remember to add android:orientation attribute to it.
NOTE: I suggest you to learn ConstraintLayout, which can replace nearly every type of nested ViewGroups including FrameLayout and RelativeLayout :-)
This you can't achieve with FrameLayout, this is because FrameLayout is usually used to show only one child. Even tho it can show more child views they are just placed on top of each other. This is explained in the answer above. Each ViewGroup has its own pros and cons and the best Layout for this is LinearLayout. LinearLayout can place your child's one below another or one next to another, based on android:orientation attribute you can use. Values are vertical and horizontal. You understand from that which one should be used here.
Therefore, what you need to do is simply change your FrameLayout to LinearLayout. So this is how it looks in my XML:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
After that, you just place your child views in order you want to show them. So, in my case I have an ImageView and a Button, placed in that order, like this:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/gameSurface"
android:src="#drawable/ic_user"
android:layout_width="200dp"
android:layout_gravity="center_horizontal"
android:layout_height="200dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:layout_marginTop="20dp"
android:padding="15dp"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#000" />
</LinearLayout>
Then I get this:
This is my current design in my main activity.
<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="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Subestacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/txtClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Clasificacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<ScrollView
android:id="#+id/scrollEventos"
android:layout_width="match_parent"
android:layout_height="350dp"
android:paddingTop="30dp"
android:paddingBottom="10dp">
<LinearLayout
android:id="#+id/linearEventos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:id="#+id/btnRegistros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Actualizar Registros" />
</LinearLayout>
</ScrollView>
I have a second scrollview inside the first one because this one is filled with multiple labels created from code. These labels can take up a lot of space, so a scrollview is suitable for proper navigation.
The first scrollview that contains everything is so that when changing the screen in landscape mode, you can continue browsing and access all the controls that are displayed.
The problem is that when changing the screen in landscape mode, only the first scrollview works, which allows you to see all the controls correctly. However, the second scrollview showing the records cannot be moved, and therefore the records are cut.
Is there a way to freeze the first scrollview when the user wants to slide the records and allow the second scrollview to be moved?
Or is it that I should create my design differently?
Note: Everything works correctly with the screen in portrait mode. All the controls are accessible at first instance and the second scrollview can be slid correctly allowing to see the records.
I've tried working with the layout_weight property but it didn't work for me or I just didn't know how to use it.
Use this:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- Other Layout or scrollView-->
</android.support.v4.widget.NestedScrollView>
Android site: NestedScroll
I managed to implement Cardviews in my app, but the cardview show an unnecessary padding in the top.
What i want to achieve is to get a header image like this :
Here's my cardview Layout file :
<android.support.v7.widget.CardView
android:id="#+id/programCardview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardUseCompatPadding="true"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/programHeader"
android:layout_width="match_parent"
android:layout_height="160dp"
android:src="#drawable/createdprogramviewcard"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Programme d'endurance"
android:textSize="20sp"
android:textAlignment="center"/>
<TextView
android:id="#+id/objectif"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Objectif : " />
<TextView
android:id="#+id/programObjectif"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="6 séances"
android:layout_toRightOf="#id/objectif"/>
<TextView
android:id="#+id/programWorkouts"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="textEnd"
android:layout_alignParentRight="true"
android:text="6 séances" />
</RelativeLayout>
</LinearLayout>
This is the code of the RecyclerView :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewPrograms"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
I manage to change the attribute cardUseCompatPadding but that not affect the internal form of the cardview, it's just there to separate them.
Thanks in advance.
I found a solution for my problem by changing the height of the imageView to 130dp. Apparently since i made the with of the image to match_parent, i had to find the exact height that will suit the image inside the cardview without giving it some extra padding.
The situation is due to two reasons, the first is that the height property of the CardView is in match_parent or in a larger size than the components occupy and at the same time the gravity property of the layout is in center.
To fix it just set the gravity of the LinearLayout to center | fill or just to fill.
I used these below attributes and it works for me.
card_view:cardElevation="0dp"
card_view:cardMaxElevation="0dp"
Or you can just use MaterialCardView instead of legacy CardView
I have some problem with multiple screen support, I work with dp(dpi) for specify the layout_heigth and layout_width and I hope that is the better way to support multiple screen, but when I tried with two smartphone I meet two different result.
I give an example, this is a layout I use:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/citieslist"
android:layout_width="wrap_content"
android:layout_height="320dip"
android:layout_gravity="center_vertical"
android:layout_below="#id/cities_main_layout"
/>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/citieslist"
android:layout_gravity="center_vertical">
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
</RelativeLayout>
The button are at the bottom of the layout, and I see two different result:
http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png
http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png
In the last smartphone I can see the buttons, instead in the first I cannot...what's wrong?
I have to write a layout for any set of screen??!!!
Your ListView has
android:layout_height="320dip"
Now if the phone screen is smaller, it will not fit.
Try doing this instead: (Edited due to comments. This is displayed correcty in eclipse)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:layout_above="#+id/linlay">
</ListView>
<LinearLayout
android:id="#+id/linlay"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="#00FF00"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
Thant should fix it I think.
Cheers
As others have indicated, your problem is that you hardwired in a size for the ListView. If you want a business rule of "have the buttons at the bottom and have the ListView fill up the rest", you actually need to write code that implements "have the buttons at the bottom and have the ListView fill up the rest".
There are two main approaches here:
Use a LinearLayout parent for the buttons and the ListView. Use
android:layout_height="0px" and android:layout_weight="1" for the
ListView. Use a regular android:layout_height for the buttons (presumably in their own LinearLayout) and no
android:layout_weight for for them
Use a RelativeLayout parent for the buttons and the ListView.
Define the buttons as having android:layout_alignParentBottom="true".
Define the ListView as having android:layout_alignParentTop="true"
and android:layout_above="...", where the ... is the ID of the buttons' LinearLayout.
I would say it's because you are specifically declaring a height for your ListView and then laying the LinearLayout that holds your buttons at the bottom. Try changing it instead of being at the bottom of the ListView to something like
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignparentbottom="true"
android:layout_gravity="center_vertical">
I'm not entirely sure if align_parent_bottom is the 100% correct spelling of that.
Well, others have beaten me to it while I was typing, haha, but yeah, you're hardwiring a lot of things that shouldn't be, both the ListView and the Buttons. Take a look at this:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
<ListView
android:id="#+id/citieslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/cities_button_layout"
/>
</RelativeLayout>
You have android:orientation on your RelativeLayout, which isn't actually an attribute that RelativeLayout contains.
You should use the layout_weight attribute rather than hardwiring sizes for the Buttons. In my example, all buttons have a width of fill_parent, and a weight of 1. This makes them distribute the space evenly.
List the fixed button layout first, setting it to alignParentBottom="true". Then set the ListView to fill_parent, and layout_above your button layout. This keeps the button layout at the bottom, and makes the ListView take all the space above your buttons.
Tada!