Center multiple Framelayouts within LinearLayout - java

I have a LinearLayout that contains 6 FrameLayout blocks.
Is there any way to center all of the FrameLayout blocks within the parent LinearLayout? Currently they are further to the left of the screen rather than centered.

You can use android:layout_gravity="center" in LinearLayout and android:gravity="center" in FrameLayouts to set them in center. There are other options see link

Try this way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="2dp" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>

Related

Bottom Navigation Bar not shown in activity

I have an activity with a title, a fragment with a recyclerview full of cards and a bottom navigaton bar.
I cannot manage to show all 3 together without problems, the closest I have been is showing all 3, but with the bar taking space from the cards space.
Right now this is the XML which I thought should be working:
<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"
android:orientation="vertical"
tools:context=".habits.HabitsActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="313dp"
android:layout_height="166dp"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/goodreasons" />
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
</LinearLayout>
But this gives this output:
This is the xml of the fragment, in case you think it is relevant too.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="5dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_gravity="center"
/>
</LinearLayout>
Here's your layout, stripped down:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="313dp"
android:layout_height="166dp"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Your problem is that the CoordinatorLayout has a match_parent height, so it will take up all of the space that the ImageView doesn't. That leaves no space at all for the FrameLayout, so you won't be able to see anything inside of it.
Probably the best thing to do would be to use layout_weight on the CoordinatorLayout to make it take up as much space as it can while still leaving room for other views. That would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="313dp"
android:layout_height="166dp"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The only changes I made were to set the CoordinatorLayout's height to 0dp and the FrameLayout's height to wrap_content, and to add the weight attribute to the CoordinatorLayout.

Multiple LinearLayout in ScrollView

I'm stucked trying to insert multiple LinearLayout or FrameLayout (whatever..) in a ScrollView.
My structure is:
Activity
...FragmentA
...FragmentB
...FragmentC
activity_layout.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"/>
<LinearLayout
android:id="#+id/container_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"/>
<LinearLayout
android:id="#+id/container_video"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"/>
</LinearLayout>
One of my Fragments for example, has this layout:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.arbiec.conex.activities.MainActivity" />
The problem!!
I can only see the 2 first fragments.
BUT when i hardcode the layout_height of the LinearLayout in my main layout (The one from the Activity) then it works just fine! But i dont want to hardcode it, because i do not know how much space will it take, it should be dynamic with wrap_content.
Any ideas?? Why could this be?
Thanks!
Try this code...
Set below property in Scrollview
android:fillViewport="true"
Try this modified layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
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">
<FrameLayout
android:id="#+id/container_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
/>
<FrameLayout
android:id="#+id/container_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
/>
<FrameLayout
android:id="#+id/container_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
/>
</LinearLayout>
</ScrollView>
Hope this help!

RecyclerView takes up all screenspace

So I'm having some trouble with a RecyclerView in a layout file
Here it is:
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:orientation="horizontal"
>
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"
/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"
/>
</LinearLayout>
The RecyclerView is visible and scrollable but the LinearLayout and the Views inside are not visible... I tried quite a few things but nothing has worked so far.
Can any of you please point me into the right direction?
Many thanks in advance!
When RecyclerView is drawn, it calculates all the remaining size on screen to itself before drawing the next elements and don't recalculate after the other elements are drawn, leaving them outside the screen.
The trick is to draw all other elements first, and leave RecyclerView for last. FrameLayout does not work anymore so use a relative layout and put the RecyclerView last on the XML layout file.
Example to add a bar with buttons below the RecyclerView:
<?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"
android:padding="16dp"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/pagination_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/previous_btn_label"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/next_btn_label"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/items_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_above="#id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR
</RelativeLayout>
Change the root view to a FrameLayout. Set the gravity of the LinearLayout to bottom
<FrameLayout 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.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"/>
</LinearLayout>
</FrameLayout>
Alternatively, you can wrap the linear layout in a FrameLayout
and set it to align_parentBottom = true if it is a relative layout or a linear layout at your root and you have other things in your view.

Android ImageView getting over linearlayout

I'm developing an android application, The layout is shown in the image below :
Screenshot http://www.lebanon-trading-center.com/daymanager/layout.PNG
The layout contains 2 main linearlayouts : a horizontal layout that takes 90% of the height of the device and a vertical layout that takes 10% of it. Inside the horizontal linearlayout there is three linearlayout ( two red and one containing an imageview). Each red layout is 10% of the width of the screen and the one containing the ImageView is 80%. Here is the xml code :
<?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"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.9"
android:weightSum="1"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.1"
android:background="#ff0000" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="0.8" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.1"
android:background="#ff0000" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.1"
android:background="#000000" >
</LinearLayout>
</LinearLayout>
The problem is, when I set a PNG image (1036px X 730px ) as a source to the ImageView, the ImageView will get over the bottom linearlayout ( the black one ) and the two red layouts will be squeezed.
Screenshot http://www.lebanon-trading-center.com/daymanager/layout2.PNG
My question is how can I solve the problem and have the same layouts functional on all kind of tablets ?
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.90">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.10"
android:background="#ff0000" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_weight="0.80" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.10"
android:background="#ff0000" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.10"
android:background="#000000" >
</LinearLayout>
</LinearLayout>

Stretch FrameLayout to visible screen

My interface looks like this:
http://i.stack.imgur.com/zADUL.png
Is there any simple solution which helps me to stretch FrameLayout to visible screen? I tried to set fillViewport and wrap_content to tabcontent children, but it didn't help.
Try this :
<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=".Form" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#cccccc" >
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:layout_below="#+id/header"
android:background="#ff0000" >
</FrameLayout>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#cccccc" >
</RelativeLayout>
</RelativeLayout>
Thanks.

Categories