Issue with Llinear layout hierarchy and alignment - java

I've some line of code in parent XML as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/header_parent_layout"
layout="#layout/common_header_layout" />
<include
android:id="#+id/title_header_layout"
layout="#layout/title_header_layout" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
then i have an another xml that i want to add in in LinearLayout with id:content layout in first xml inflated by below xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customviews="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/agenday_account_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<!-- More Code -->
</FrameLayout>
I already set the center_vertical gravity in inflated xml layout but it's always displaying in top. Could anybody help how to treat with this.
Below code is how i add it:
View view = getLayoutInflater().inflate(R.layout.second, null);
if (view != null) {
mContentContainer.addView(view);
}
mContentContainer is parent one.

Just add a gravity attribute to your id:content LinearLayout,so the content inside it stays centered. Like this:
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" />

I have this Idea:
Change content LinearLayout attributes to:
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:gravity="center_vertical"
android:orientation="vertical" />

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.

Issue while setting Footer layout in ListView

Below is my Footer Layout for setting it for my ListView :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<TextView
android:text="footer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
It's working fine. But, What if I want to set an emptyview with some background color in my ListView's footer ?
If I am doing it as below, ListView not showing footer view. :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
</RelativeLayout>
I am setting footer as below :
View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_for_gym_list, null);
listGymSuggestions.addFooterView(footerView);
Please guide me How can I set empty footer with some height and background color in ListView ?
The View is there but it has NO height so you must add your own height value may be 24dp or something and set it as a footer you can even add a width if you want. For instance here I am adding a height of 24dp:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#color/gray_cell_bg">
</RelativeLayout>
If there is no child in that layout, It will not be shown. If you have problem with setting specific height(when you are adding child dynamically), you can use padding like,
android:padding="20dp"
I added a button as a footer in one of my layout.
Here is my code. You may add anything you want.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/search_toolbar"
layout="#layout/search_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/emp_button"
android:layout_below="#+id/search_toolbar" />
<TextView
android:id="#+id/tvsample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_toolbar"
android:layout_marginTop="200dp"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/emp_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/Red"
android:src="#drawable/ic_add_employee"
android:text="#string/emp_button"
android:textSize="14sp" />
Issue is solved by adding an ImageView in Footer view as below :
footer.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/com_facebook_profilepictureview_preset_size_normal"
android:scaleType="fitXY"
android:src="#color/gray_cell_bg" />
</RelativeLayout>

Adding a CardView to a FrameLayout ignores LayoutParams defined in the CardView's XML file

In my MainActivity I'm using a FloatingActionButton to add CardViews to an existing container (a FrameLayout) programatically.
But when I add the CardView all LayoutParams (e.g. width, height, margin, etc) are ignored. Is there a way to use the params specified in the XML File?
Here is my code:
card_view.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="#color/cardViewBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="example Text" />
</android.support.v7.widget.CardView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:id="#+id/textViewDate"
android:text="#string/default_main_activity_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textAlignment="center"
android:padding="16dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_fab_add"
android:layout_gravity="end"
android:onClick="addCard"
android:visibility="gone"/>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.life_hacks.liftnotes.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
and finally the code I use to add the CardView:
public void addCard(View v){
View cardView = View.inflate(getApplicationContext(), R.layout.card_view, null);
FrameLayout container = (FrameLayout) findViewById(R.id.container_body);
if(container != null){
container.addView(card_view);
}
}
For the layout_* attributes of an XML-defined View to be applied correctly, the ViewGroup that will hold it must be supplied to the LayoutInflater. In this case, it means that you need to pass your container as the last argument in the View.inflate() call. Doing this will also cause the inflated View to be automatically added to the ViewGroup, so you must not call addView() with it, or you'll get an IllegalStateException.
Also, since you'll be adding multiple Views to your container, it would seem that a LinearLayout would be more appropriate than a FrameLayout. I would also mention that if you plan on adding a lot of additional Views, a ListView or a RecyclerView might end up being a better option.

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.

Categories