Fragment doesn't show up when using Navigation component - android

I'm trying to build an application based on Fragments and the Navigation component. The sample code below is what I belief to be the absolute minimum.
My activity's layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<fragment
android:id="#+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</FrameLayout>
The corresponding activity class looks like the following. Note that as of yet I'm not using any toolbar, nor bottom navigation, nor a menu.
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The start fragment to display is just a ConstraintLayout defining some regular buttons and a floating action button.
The fragment's implementation looks like this:
public class FragmentMainScreen extends Fragment
{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_main_screen, container, false);
}
}
Lastly, the navigation graph is this:
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="#id/destination_main">
<fragment
android:id="#+id/destination_main"
android:name="com.stmoebius.zz.ui.FragmentMainScreen"
android:label="#string/main_screen_title"
tools:layout="#layout/fragment_main_screen" />
</navigation>
Everything builds and executes just fine, but the fragment doesn't show up (except for its label). What am I missing?

You gave fragment width and height as 0dp
<FrameLayout
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">
<fragment
android:id="#+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</FrameLayout>

Related

Layout doesn't inflate in Android Studio MainActivity when it contains a Fragment Container View

I have the same problem like in this post, but in other way:
I use a button from inside a fragment to inflate a different layout in MainActivity using the interface communication. It works when the layout contains only a TextView, but when I replace it with a Fragment Container View, it crashes. I used #ianhanniballake answer changing the context to (this) and it removed the crash, but it still doesn't inflate anything..
Here is my code:
public class MainActivity extends AppCompatActivity implements FragmentB.FragmentBListener {
ViewGroup main_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_layout = findViewById(R.id.main_layout);
}
//fullScreen method comes from a button click listener from FragmentB
#Override
public void fullScreen() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.fullscreen, main_layout,false);
main_layout.removeAllViews();
main_layout.addView(view);
}
}
this is my activity_main.xml:
<?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:id="#+id/main_layout"
tools:context=".MainActivity">
//some basic initial code
</RelativeLayout>
and this is the layout I want to inflate - fullscreen.xml:
<?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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerViewB"
android:name="com.example.testglide.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
tools:layout="#layout/fragment_b" />
</RelativeLayout>
Thanks a lot for any help! :)
try this:
activity_main.xml:
<?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:id="#+id/main_layout"
tools:context=".MainActivity">
<include android:id="#+id/layout_fullscreen"
layout="#layout/fullscreen"/>
</RelativeLayout>
java class
View view = findViewById(R.id.layout_fullscreen);
main_layout.removeAllViews();
main_layout.addView(view);

Listview in the ListViewFragment is not appearing in the activity

I have the activity_main.xml with one FrameLayout relative to a ListViewFragment. I want to show this fragment in this activity. But its not working the ListView is not appearing. Do you know where is the issue?
main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_list"
/>
</android.support.constraint.ConstraintLayout>
Main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here Im trying to add the fragment A to the activity but the listview is not appearing
getSupportFragmentManager().
beginTransaction().add(R.id.fragment_list,new ListViewFragment()).commit();
}
}
listview xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
ListViewFragment:
public class ListViewFragment extends Fragment {
private ListView editor=null;
ArrayList<String> items = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_list, container, false);
editor=(ListView) (result.findViewById(R.id.listView));
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items);
editor.setAdapter(arrayAdapter);
return result;
}
}
Add height and width to the framelayout.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_list" />
Also your listView Fragment cannot have listview as parent layout.So wrap it with a layout like frame layout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

changing view to a view with fragment in android

im trying to use setContentView to switch to a layout with a fragment inside it
but it crashes every time
what should i do?
this is my entire code:
(it must switch to fragment_layout when i click the button in the activity_main)
(fragmant_layout has a fragment and the layout for the frgment is te layout1)
(it crashs when i cick the button to switch)
main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void switchlayout(View view) {//when the button in clicked
setContentView(R.layout.fragment_layout);
}
}
the fragment class:
public class frgment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout1,container,false);
}
}
activity_main.xml:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".fragmenttest.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="switchlayout"
android:layout_marginTop="74dp"
android:id="#+id/button" />
</RelativeLayout>
fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="alirezamellat.fragmenttest.frgment"
></fragment>
</LinearLayout>
layout1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"></View>
</LinearLayout>
Please consider the following points::
On your parent layout (activity_main.xml), inside the Relative layout, add another layout called FrameLayout.
On your fragment layout(fragment_layout.xml), remove the fragment and add the layouts of layout1.xml to fragment_layout.xml
On your fragment class (MyFragment), inflate the fragment_layout.xml to the container and return the view (class name should start with uppercase letter, so name it like MyFragment)
On Activity class, place the following code on onclick listener.
//onClick on OnClickListener
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_layout, fragment)
.commit();
Once the layout of an Activity has been set it can not be changed at a later time.
There are a number of ways you can achieve this from which the easiest would be to have a wrapper layout and then injecting the fragment inside it using the FragmentManager class.
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragmenttest.MainActivity">
<FrameLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="switchlayout"
android:layout_marginTop="74dp"
android:id="#+id/button" />
</RelativeLayout>
And then on your button click add your fragment.
getSupportFragmentManager().beginTransaction().add(R.id.wrapper, MyFragment.newInstance(), "FragTag").commit();
Remember that you have to take care of your button since it will still be visible on top of you fragment.

Google's BottomSheet appears at top of screen

I'm using android's new BottomSheet inside Design Library.
Problem is that I'm using it inside a Fragment and It cause that it appear at top of screen instead of appearing at bottom.
This is my Activity xml:
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="ir.aidinsoft.quicktaxi.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/tlbr_acMain"
android:elevation="5dp"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.CoordinatorLayout
android:id="#+id/crdl_acMain"
android:layout_below="#+id/tlbr_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nscv_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This Layout Is Replacement Layout For Fragment -->
<RelativeLayout
android:id="#+id/frml_acMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
And This Is My Fragment xml:
<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_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.aidinsoft.quicktaxi.MyFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.codetail.widget.RevealFrameLayout
android:id="#+id/rvfl_frTaxi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<fragment
android:id="#+id/frag_frTaxi_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</io.codetail.widget.RevealFrameLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/facb_frTaxi_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- My Bottom Sheet Layout -->
<FrameLayout
android:id="#+id/frml_frTaxi_bottomSheet"
android:layout_width="match_parent"
android:layout_height="360dp"
android:background="#color/colorPrimaryLight"
app:behavior_hideable="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />
</android.support.design.widget.CoordinatorLayout>
So after Replacing Fragment in Layout with this code:
getSupportFragmentManager().beginTransaction().replace(R.id.frml_acMain, taxiFragment).commit();
I adde this code to reveal BottomSheet on FAB clicked state.
Java Code for Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
fab = (FloatingActionButton) view.findViewById(R.id.facb_frTaxi_request);
bottomSheet = view.findViewById(R.id.frml_frTaxi_bottomSheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
return view;
}
#Override
public void onResume() {
super.onResume();
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
}
What is the problem? what I'm doing wrong?
TnQ
If you are using animateLayoutChanges inside your layout, removing it can fix your problem.

Navigating through a fragment

I have a fragment inside my main activity. I want to update the contents of the fragment dynamically a run time. Specifically, I want to populate the contents of fragment with new data on button back press. Please help me out in the same and suggest me how to achieve such a functionality.
//main_activity.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical"
android:id="#+id/container">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<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">
<FrameLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_recyclerview"
android:name="com.example.RecyclerViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout="#layout/fragment_recyclerview" />
</FrameLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.example.NavigationDrawerFragment"
android:layout_width="#dimen/drawer_dimen"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
The first fragment is the one that needs to be updated
#Override
public void onBackPressed() {
//Find if fragment by tag
Fragment fr = fragmentManager.findFragmentByTag("home");
if (fr.isVisible()) {
//do ur stuff
} else {
fragmentManager.beginTransaction().show(fr).commit();
}
}
Ok! I will give you an example of what I did in one of my projects...
My main activity xml contained a simple linear layout among other things:
<LinearLayout
android:id="#+id/layout_images_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:orientation="horizontal" >
</LinearLayout>
In my activity OnCreate, I added the following:
FragmentImages fragmentImages = new FragmentImages();
getFragmentManager().beginTransaction().add(R.id.layout_images_fragment, fragmentImages).commit();
where FragmentImages is a class that extends Fragment
public class FragmentImages extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_images, container, false);
//and here is where I wrote my codes for this fragment
return v;
}
}
Hope this will help !!

Categories

Resources