I am adding a fragment to activity, and then i am replacing that fragment with the second fragment.After replacing the second fragment action bar is slightly moving down like this
My Activity code
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meetinglist_activity);
Fragment first_fragment = new FirstFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, first_fragment).commit();
}
Replacing second fragment in the first fragment's button click
public class Firstfragment extends Fragment {
FloatingActionButton new_fab;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View list_view=inflater.inflate(R.layout.meetingslist_fragment,container,false);
new_fab=(FloatingActionButton)list_view.findViewById(R.id.meeting_new);
new_fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment second_fragment = new SecondFragment();
FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, second_fragment).commit();
}
Activity layout
<?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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
Fragment's Xml
<!--First Layout-->
<?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">
<android.support.design.widget.FloatingActionButton
android:id="#+id/meeting_new"
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>
<!--Second Layout-->
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:gravity="center"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I am using android.support.v4.app.Fragment;
I am using Android Studio version2.0 and latest API levels.
Can anybody help me? Thanks in advance.
Just change this line
android:fitsSystemWindows="true"
to
android:fitsSystemWindows="false"
I'm sure your problem will be solved
Problem Occurs Bcoz Coordinate Layout In First Fragment Only Put Coordinate Layout in second fragment so it will work same for both Layouts.
I guess to solve all issues related to actionbar you should use toolbar which is new replacement of old actionbar by Google. You can find tutorial of implementing toolbar at end of the link: http://javatechig.com/android/android-lollipop-toolbar-example
Related
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.
Hello im new android developer and i try to do the next things with no success
i have Activity, inside the activity i have fragment and inside it i have ** view pager image galley**.
the problem is when i run the app with the activity with the fragment and and gallery inside it the app crash
but when i tried to run the activity with the gallery and the fragment separately they works fine
whats the problem?
here my code:
Activity
public class infoActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_info);
}
}
Activity 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"
android:weightSum="1">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="co.checkit.mobile.checkit.Model.gallery_fragment"
tools:layout="#layout/imagegalleryfragment"
android:id="#+id/fragmentid" />
</LinearLayout>
ass you can see i set the fragment inside the activity
Fragment class
public class Hotelsgallery_fragment extends Fragment {
ImageAdapter adapter;
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.imagegalleryfragment, container, false);
viewPager = (ViewPager)myFragmentView.findViewById(R.id.viewpager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
return myFragmentView;
}
}
i set the gallery inside the fragment class
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/viewpager">
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
</LinearLayout>
instead of putting fragment inside xml, put frame layout
<?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"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/frame_layout_id" />
</LinearLayout>
create fragment inside a activity, inflate using frame layout id
YourFragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(R.id.frame_layout_id, fragment);
ft.commit();
I have a ListFragment with a floating action button. When the button is clicked, i want to replace the ListFragment with a CreateListFragment. Instead, the CreateListFragment is placed on top of the ListFragment and the button and textview from the ListFragment are still visible. I am not sure what went wrong. How do I completely replace the ListFragment with the CreateListFragment?
Here is the screenshot before the button is clicked
http://i.stack.imgur.com/k2HxP.png
Here is a screenshot after the button is clicked
http://i.stack.imgur.com/TYN47.png
list_fragment
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listFrame">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fab"
android:layout_gravity="bottom|end"
app:backgroundTint="#color/colorAccent"
android:src="#drawable/create"/>
<TextView
android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="No Shopping Lists Yet"
android:layout_gravity="center"
android:textSize="20sp"/>
</FrameLayout>``
createlist_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"
android:padding="10dp"
tools:context="com.lavineoluoch.helloworld.swiftpay.app.CreateListFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etListTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/title"/>
<EditText
android:id="#+id/etAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/items"/>
<RelativeLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnEditItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnEdit"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
</RelativeLayout>
</LinearLayout>
ListFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.list_fragment, container, false);
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateListFragment createListFragment = new CreateListFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.listFrame,createListFragment);
fragmentTransaction.commit();
}
});
return view;
}
In your root view for your Fragments insert this line of code:
android:background="?android:windowBackground"
As you have a see through background your view can still see the reminiscence of the replaced Fragment as the view hasn't yet updated.
The above answer should solve your problem, however I would recommend re-structuring your approach and instead launching a new Activity which contains the CreateListFragment when the FAB is pressed. Doing so would result in easier to maintain code which is also easier to read and would be best practice. It would also mean that the user can press their back button and navigate back to ListActivity/ListFragment without you needing to manually handle Fragment back stacks.
This is a really lame question, but I really cannot figure out why it happens, so please help me out.
I have a simple activity and I add a fragment to it dynamically. The problem is that once the fragment is added to the activity, the activity layout is also visible. Why does this happen?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<LinearLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
HelloFragment.java
public class HelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}
}
hello_fragment_layout.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="fragment 1"/>
</LinearLayout>
Convention is to have a FrameLayout that you use fragmentTransaction.replace()/add() on.
You shouldn't have views like TextView and Button in your MainActivity layout file. Instead, have those in another fragment and just have a FrameLayout in your MainActivity xml. Load the fragment with TextView/Button first, and call replace on button click with your new fragment.
Set a background color to your fragment linearLayout and make it clickable
The fragment is just another element on the layout. Adding it to the layout does not automatically remove other views. If you want the fragment to replace the content of rootView you need to remove rootview's children views first. Something like:
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
So your onClick method should look like this:
#Override
public void onClick(View v) {
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
your rootview work as container for both fragment and your views on activity. check it and make separate view for fragment.
I know it's very late to answer this question but for those who are still looking do the following in your activity layout:
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
</RelativeLayout>
And fragment layout to following:
<?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:background="#color/white" <!--Add this-->
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
activity_main.xml
<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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.fragmentContainer, hello, "HELLO");
fragmentTransaction.commit();
}
};
I have a solution for this to achieve what you can do is make two fragment one for textview and button your showing as default when activity is opened and another which you have already created as HelloFragment.
From Activity onCreate() method call the first fragment with default textview and button and from the click listener of that button call HelloFramgent.
Step1: Crate another fragment with any name MainFragment having a xml layout as follows:-
main_fragment_layout.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">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
MainFragment.java - Class
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.main_fragment_layout, container, false);
Button btnLoad = (Button) v.findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
return v;
}
}
Step2: Call the MainFragment from activity onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
fragmentTransaction.commit();
}
Build your user interface in Fragments (In most cases one Fragment equals one screen) and then use Activities to arrange and display those Fragments.
check this: click here to view detailed answer
You have two linear layouts, one inside another, so you need to do the following steps:
Set id to 1 linear layout rootView and next to the childview.
Find your child view:
View view = findViewById(R.id.child_view);
view.setVisibility(View.INVISIBLE);
Replace your rootView with your fragment:
getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
.addToBackStack(null).commit();
Then override:
#override
public void onBackPressed() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
view.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
The Activity views are not being masked against the Fragment's views, so both appear. One solution is to load the fragment into a frame in the main activity's layout which has a slight elevation (2 dp). When the fragment is loaded its base elevation will cause it to always draw over the activity. Set the fragments parent container with a color to hide the activity views.
MasterActivity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Activity Title"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<!-- Fragment holder, with elevation -->
<FrameLayout
android:translationZ="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragHolder"
/>
The Fragment layout has a background color:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test Background Task"
android:textSize="24sp" />
</RelativeLayout>
Make your First Layout invisible while calling fragment and again while returning from fragment make your main view visible and remove fragment at that time so it will not conflict with each other it worked for really well just check it once
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 !!