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 !!
Related
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>
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.
So i was using SwipeRefreshLayout and Fragments for a simple app that i am working on and i have three files:
1.app_bar_main.xml which contains a FrameLayout which is a container for the fragments heres the code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.surafel.tryingfragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
2.fragment_main.xml which contains components of the main fragment and a SwipeRefreshLayout heres the code:
<android.support.v4.widget.SwipeRefreshLayout 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:id="#+id/refreshMainContent"
tools:context="com.example.surafel.tryingfragment.MainFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:padding="8dp"
android:textColor="#fff"
android:background="#color/colorPrimary"
android:textSize="28sp"
android:id="#+id/buttonmain"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
3.MainFragment.java this is the java code for the fragment_main.xml heres the code
//i have imported all the necessary classes
public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeView;
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main,container,false);
swipeView = (SwipeRefreshLayout) layout.findViewById(R.id.refreshMainContent);
swipeView.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_dark),getResources().getColor(android.R.color.holo_red_dark),getResources().getColor(android.R.color.holo_green_light),getResources().getColor(android.R.color.holo_orange_dark));
swipeView.setOnRefreshListener(this);
return inflater.inflate(R.layout.fragment_main, container, false);
}
public void onRefresh() {
Log.d("Refreshing","The refresh is working");
}
}
4.i also have MainActivity.java file which contains all the necessary codes
the problem is its not changing the color of the swipeView and its not even calling the onRefresh() method. when i run the
app and swipe down the refresher comes but its color is black and its not executing the onRefresh() method.
and also there are no compiletime and runtime errors.
So can anyone please help me,i posted all the code because i thought it could be usefull,Thanks.
It seems like you have called the code inflater.inflate(R.layout.fragment_main, container, false); twice.
Try returning the object layout instead of inflater.inflate(R.layout.fragment_main, container, false)
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.
I am working on an application in which i am getting a map in full screen and also action bar on the top. In action bar i have also included Navigation drawer. Everything is working fine but the Button is not getting displayed onto the MapView.
If I remove the navigation drawer functionality, the buttons are getting displayed. But my requirement is like i should be able to display the button on the bar as well as the Navigation bar.
The snippets are:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mobile.android.ui.MapActivity"
tools:ignore="MergeRootFrame" >
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:layout_gravity="left|top" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.mobile.android.ui.NavigationDrawerFragment" />
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
My project supports similar features.. But I'm using the ViewPager.
So my location_framgment.xml (see down below) is able show the map and I will display it by the ViewPager's default behaviour.
The xml file looks as follow:
<?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" >
<fragment
android:id="#+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
And here my LocationFragment, which extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (sView != null) {
ViewGroup parent = (ViewGroup) sView.getParent();
if (parent != null) {
parent.removeView(sView);
}
}
try {
sView = inflater.inflate(R.layout.location_fragment, container, false);
sMap = ((MapFragment) MainActivity.sFragmentManager.findFragmentById(R.id.location_map)).getMap();
} catch (InflateException e) {
Log.d(TAG, "InflateExc.: "+e);
}
return sView;
}