The submit button in the activity layout floats to the top left of the phone screen even after the fragment has been added resulting in it obscuring some of the fragment's content. should it not be pushed down when the following code is called:
currentFragment = MyFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.my_fragment, currentFragment).commit();
Activity Layout:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.mycompany.myapp.MyFragment"
android:id="#+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/my_fragment_layout"/>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_button_text"
/>
</FrameLayout>
Fragment layout (my_fragment_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"/>
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_buttons"></RadioGroup>
</LinearLayout>
What am I missing about fragments and layouts here?
As per your requirement , your Activity parent layout should be LinearLayout or RelativeLayout not FrameLayout. Also set a layout weight for Fragment layout, so that it will occupy the remaining space, used by the Button
Change your activity layout like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/my_fragment_layout"
android:layout_weight="100"/>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit_button_text"/>
</LinearLayout>
One more suggestion here. If your adding your Fragment using Fragment Manager programmatically you can just define the Fragment container as FrameLayout in XML (Any way your creating the instance of fragment in code).
<FrameLayout
android:id="#+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100"/>
Hope this helps to fix your issue.
Related
So I will have my main-activity layout file below so you can understand what is happening
<?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">
<include
android:id="#+id/toolbar"
layout="#menu/toolbar"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#android:color/white"
android:background="#39b54a"
app:labelVisibilityMode="unlabeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
The problem with this layout is that the toolbar on the top and the bottom navigation bar cover content from my fragments. Also one of my fragments has got a recyclerview with many items and I canĀ“t scroll down for some reason ? Is there a way to make the fragments fit exactly between the toolbar and bottomnavigation like resizing itself to fit into that area without cutting content and still make it scrollable ?
Do it like this ...
<?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">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout"/> <!--include your layout file here -->
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu"
app:itemIconTint="#android:color/white"
android:background="#39b54a"
app:labelVisibilityMode="unlabeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
And by the way, you are supposed to set a layout resource file into the toolbar layout while including but you are passing the menu resource file. Change that also accordingly...
I'm trying to add an Imageview above my recycler view but the recycler view ends up taking the whole screen and doesn't display my image view.
<?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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".AccountFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/blogListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/imageView">
</android.support.v7.widget.RecyclerView>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/acc"
tools:layout_editor_absoluteX="146dp"
tools:layout_editor_absoluteY="86dp" />
</RelativeLayout>
Add this attribute to the ImageView:
android:layout_alignParentTop="true"
so it is at the top of the parent layout.
If you think that these attributes:
tools:layout_editor_absoluteX="146dp"
tools:layout_editor_absoluteY="86dp"
affect the position of the ImageView, then you are wrong.
Any attribute from the tools namespace is valid only at design time.
So you might as well remove them.
Also you might want to center the ImageView horizontally:
android:layout_centerHorizontal="true"
hi try to set this as i checked its working
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_play" />
<android.support.v7.widget.RecyclerView
android:id="#+id/blogListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
A RelativeLayout places every element at the start of the screen (0,0). In your case the ImageView is behind the RecyclerView. If you need to display RecyclerView below ImageView, opt for LinearLayout instead, with orientation set to vertical.
<?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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical"
tools:context=".AccountFragment">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/acc" />
<android.support.v7.widget.RecyclerView
android:id="#+id/blogListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageView">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Try the above code and let me know if it works.
Having problem getting fragment showing correctly on device. I have a frame on an activity and a Frame on a Fragment. Trying to do a replace frame on activity with frame from Fragment. however what I am getting is Frame on activity being smushed together and fragment frame underneath on display.
Here is code calling the Fragment.
public void setupMultiItems(String mScan){
Timber.d("Multiple matching Items");
Bundle bundle = new Bundle();
bundle.putString("valuesArray",mScan);
SelectItemFragment fragobj = new SelectItemFragment();
fragobj.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.FragmentFrame, fragobj);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Here is XML for the activity.
<?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:background="#color/colorBackgroundGray"
tools:layout="#layout/fragment_select_item"
tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/include2"
layout="#layout/frm1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="left"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/FragmentFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/layout_numpad5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="220dp"
android:layout_weight="1"
android:layout_gravity="right"
/>
</LinearLayout>
</RelativeLayout>
Here is XML for the fragment.
<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"
tools:context="com.procatdt.stockright.activities.SelectItemFragment">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/newFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<Button
android:id="#+id/btnSelectItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Select Item" />
</FrameLayout>
</RelativeLayout>
</FrameLayout>
all the controls on activity are being shrunk and jammed together with the button from the fragment showing underneath.
Any help on what I am missing.
My background has been in VB for last 18 years. This is my first Android Studio Java project and have been muddling by getting hung up on fragments.
I need to anchor floating action button in android, but have the trouble:
fab will be in navigationview
Here is my activity_main layout.
It includes navigation drawer.
<android.support.v4.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="#color/colorBackground">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<include
layout="#layout/nav_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Navigation drawer layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/clDrawer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.NavigationView
android:id="#+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/menu_drawer_view"
app:headerLayout="#layout/anav_header"
android:background="#color/colorPrimary2"
app:itemIconTint="#color/colorIcon"
app:itemTextColor="#color/colorIcon"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/navFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
android:clickable="true"
android:src="#drawable/plus_icon"
app:backgroundTint="#color/colorIcon"
app:layout_anchor="#id/headerLayout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
And my anav_header for navigation view contains only the few text view.
I need fab to be anchored to the botom of the header, right between header and main content of ND.
The problem is layout_anchor works only if header and nav view are in the same layout. if i use navigationview property headerLayout , layout_anchor doesnt work. Also if i use NavigationView.addHeaderView(), it doesnt work too.
If i include header in navigation view layout by that way
<include
layout="#layout/anav_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
it works, BUT header and navigation view move like different parts. And i need them to act like one navigation drawer.
I need navigation drawer to lookes smth like that.
I have found my way.
First i've removed NavigationView and replaced it with drawer_header.xml with is simple LinearLayout with header image, and with drawer_list just after it with is a LinearLayout too with static menu items.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary2">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#color/colorPrimary2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- <android.support.design.widget.NavigationView
android:id="#+id/navView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary2"
app:headerLayout="#layout/drawer_header"
app:itemIconTint="#color/colorIcon"
app:itemTextColor="#color/colorIcon"
app:menu="#menu/menu_drawer_view" />-->
<include
layout="#layout/drawer_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin"/>
<include
layout="#layout/layout_drawer_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/navFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:clickable="true"
android:src="#drawable/plus_icon"
app:backgroundTint="#color/colorIcon"
app:layout_anchor="#id/headerLayout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
So, i made header and menu as LinearLayout, otherwise they both have scrolled, but i need em to scroll together. To add elements in header or menu you can use addView(View v).
I'm working on an Android application with 4 tabs.
Here is my activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
And here is one of my tab (they are all the same for the moment)
<?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"
android:background="#ff8400" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Top Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
How could I add a footer that would be visible on each tab?
It's to add a player that stays visible while scrolling the tab.
You're going to need to make a change to your main activity layout. You can do it with a LinearLayout or RelativeLayout. Because a LinearLayout is less expensive I will show you that here.
<?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.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<View
android:id="#+id/music_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The view could been anything, I would recommend using a framelayout there and putting in a fragment if it is going to be as complicated as a music player. How this works is you set the height of the musicplayer (wrap_content) and you tell the pager to take up the rest of the space with layout_height="0dp" and layout_weight="1".
If you want to do a RelativeLayout then the musicplayer would alignParentBottom="true" and the pager would have layout_above="#+id/music_player with either layout_height="matchparent" or alignParentTop="true".