Using Fragments - java

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.

Related

navigation between fragments in Java

I'm working with Fragments and Android studio and I need navegate in fragments from Activities. Always I get the same error "No view found for id 0x7f0800a2".
Main Activity
Fragment newFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_hotels, newFragment);
transaction.addToBackStack(null);
transaction.commit();
mobile_navigation
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.example.freepapp.ui.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home">
</fragment>
<fragment
android:id="#+id/nav_hotels"
android:name="com.example.freepapp.ui.hotels.HotelsFragment"
android:label="#string/menu_hotels"
tools:layout="#layout/activity_liked_quotes" />
Fragment Home
<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"
android:id="#+id/fragment_home"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="#+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Hotels
<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"
android:id="#+id/fragment_hotels"
tools:context=".ui.hotels.HotelsFragment">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error
java.lang.IllegalArgumentException: No view found for id 0x7f0800a2 (com.example.freepapp:id/nav_hotels) for fragment HomeFragment{f896362 (40055ce2-7ab7-41dd-be0b-be0edc89f01a) id=0x7f0800a2}
I found the answer. The container is id of layout main activity.
transaction.replace(R.id.id_layout_main, newFragment);

Why does the UI become a fullscreen, when I add a fragment in Android Studio?

I have created a new project with "Bottom Navigation Activity" as template. If I add a fragment in my fragment_home.xml file a white bar at the bottom appears and the android taskbar disappears.
Here is a screenshot and the code without the fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
And here a screenshot with the fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/ux_fragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
I want the green bar at the top and under this, the fragment.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

Fragment content in activity not properly sized

I am using fragments in an activity with a BottomNavigationView and out of the box I noticed the fragment content goes below the BottomNavigationView
This is the fragment in Android studio before inflating it in the activity:
The app after the fragment has been inflating:
Here is my fragment XML I read on other posts on this site that I needed to use the CoordinatorLayout as parent and add app:layout_behavior="#string/appbar_scrolling_view_behavior" to the first child but it didn't work.
<android.support.design.widget.CoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_margin="16dp"
tools:context="com.brigafrica.koyako.fragments.SendFragment">
<TextView
android:id="#+id/textView"
style="#android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:text="#string/amount_to_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edtAmountToSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:digits="0123456789.,"
android:inputType="numberDecimal"
app:layout_constraintBottom_toTopOf="#+id/appCompatButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.095" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/appCompatButton"
style="#style/AppTheme.RoundedCornerMaterialButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="10dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="#string/send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
What is necessary to identify the problem is your activity XML content and how you are managing the fragments in your activity.
The simplest way to think is that the fragment is just a content that will be displayed in a piece of your activity, and you should provide the correct information about the dimensions of this piece in your activity.
Explaining with my example
An activity called MainActivity that has one ViewPager and one BottomNavigationView.
<?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="newidea.com.br.bottomnavigationexample.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/main.viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:menu="#menu/bottom_navigation_main" />
</RelativeLayout>
The ViewPager is the piece where the fragment will be displayed in MainActivity, so I have to tell the activity that the ViewPager can not invade the BottomNavigationView space, if not the fragment content that will be displayed in the ViewPager will goes below the BottomNavigationView.
How to do that ?
In my example I configured the ViewPager with this:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
It means that the ViewPager width and height matchs the parent's width and height, the ViewPager top is aligned with the parent's top, and it is a layout above the BottomNavigationView, so the ViewPager can not invade the BottomNavigationView space.
What you need to understand is that doesn't matter what is in your fragment, what really matter here is the dimensions configuration of the ViewPager, because the ViewPager will display the fragment.
Full example with 3 fragments, each one with a Button in the Bottom
Fragment in Android Studio before inflating
The app after the fragment has been inflated

Image icon on tabs appear very small even though I'm using the correct dimensions (hdpi, mdpi, xxhdpi ...)

I followed the guidelines on the Google developer page to creat these 5 folders and filled them with icons of the correct dimensions:
I created my tabs using a TabHost like so:
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1 = tabHost.newTabSpec("tab1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("", getResources().getDrawable(R.drawable.homeicon));
TabHost.TabSpec spec2 = tabHost.newTabSpec("tab2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("", getResources().getDrawable(R.drawable.startachainwhite48dp));
TabHost.TabSpec spec3 = tabHost.newTabSpec("tab3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("", getResources().getDrawable(R.drawable.joinachainwhite48dp));
TabHost.TabSpec spec4 = tabHost.newTabSpec("tab4");
spec4.setContent(R.id.tab4);
spec4.setIndicator("", getResources().getDrawable(R.drawable.viewchainswhite48dp));
TabHost.TabSpec spec5 = tabHost.newTabSpec("tab5");
spec5.setContent(R.id.tab5);
spec5.setIndicator("", getResources().getDrawable(R.drawable.profilewhite48dp));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
tabHost.addTab(spec5);
But for some reason the icons on my tabs (at the bottom) show up tiny
Does anybody have an explanation/solution for this? I feel like I have made a silly mistake somewhere - will post xml layout if needed. Thanks in advance.
EDIT: It may be worth mentioning that the getDrawable() method I'm using in the above code is deprecated (API 22)
EDIT2: Full XML layout
<?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:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tool_bar_test"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF86B39A"
android:fitsSystemWindows="true"
android:gravity="top"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="Test"
android:textColor="#ffffff"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sv_laptop03.myapp.Profile">
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:id="#+id/tab5"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/headerview"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
To make sure that every thing will looks as you expected, just add images into the xxxhdpi folder and be sure that they are 192x192 px.
By this method you let the OpenGL convert the image to what device the app is running on and that also give you the ability to resize your images without losing any details.
Hope this will help you.
Reference here:
https://developer.android.com/guide/practices/screens_support.html

Floating Submit Button

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.

Categories

Resources