This is a known problem, when I change fragments they overlap.
I followed the examples on the internet with a brand new project to test it out, but no success.
What am I missing here?
I started a simple project with the Navigation Drawer Activity
I've given the fragment_home and fragment_gallery an as id
<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:id="#+id/fragment_home"
android:background="#android:color/background_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="333dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I setup fragment manager with the transaction:
public void replaceFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_home, GalleryFragment.class, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Created a button and set the method as the onClick activity:
public class HomeFragment extends Fragment {
private View root;
private FragmentHomeBinding binding;
private Button button;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
root = binding.getRoot();
button = root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Change Fragment
((MainActivity)getActivity()).replaceFragment();
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
I run the app and press the button, the fragments overlap. What is going on? I've already added a background to the fragments,
did not work. It has something to do with the BackStack, but how? Why is this so difficult? Why can't I simply add and remove a fragment?
Related
I am trying to change TextView value in Fragment but it not clearing old value.
Value is overlapping on old value.
Code from Fragment -
public class MainFragment extends Fragment {
TextView t1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
t1 = view.findViewById(R.id.textview1);
t1.setText(s1);
return view;
}
}
Code from Activity -
public static String s1;
#Override
public void onOptionClicked(int position, Object objectClicked) {
// Set the toolbar title
setTitle(mTitles.get(position));
// Set the right options selected
mMenuAdapter.setViewSelected(position, true);
s1 = mTitles.get(position);
// Navigate to the right fragment
switch (position) {
default:
goToFragment(new MainFragment(), false);
break;
}
// Close the drawer
mViewHolder.mDuoDrawerLayout.closeDrawer();
}
Fragment xml file -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
Tried by using below code but not working....
public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.detailsText);
textView.setText(textview1);
}
Please help.... Thanks in Advance...!
If you are using .add in your goToFragment() code, then you should replace with .replace
fragmentTransaction.add(R.id.container,
currentFragment).commit()
to
fragmentTransaction.replace(R.id.fragment_container,
currentFragment).commit()
You can put below code in onCreateView method of your fragment and it will remove this overlapping issue
View view = inflater.inflate(R.layout.your_layout, container, false);
view.setBackgroundColor(Color.WHITE);
fragment.xml add (android:background="#color/white") in your Layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
As Ranjan said it is not good practice, why do not you create MainFragment mf = new MainFragment(); inside onCreate() and use that in that method. In this way you will have only one instance. I do not use fragments like this so I do not have good idea about your code otherwise.
I'm trying to make an app using fragments, based on this docs: Fragments.
As far as I can understand, I need:
Host Activity [DONE]:
This is my host activity (.java)
public class SphereSurfaceArea extends AppCompatActivity
{
RadioGroup rg_sa;
RadioButton rb_grad, rb_gvol, rb_gcirc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sphere_surface_area);
rg_sa = (RadioGroup) findViewById(R.id.rg_sphere_sa);
rb_grad = (RadioButton) findViewById(R.id.rb_given_rad);
rb_gvol = (RadioButton) findViewById(R.id.rb_given_vol);
rb_gcirc = (RadioButton) findViewById(R.id.rb_given_circ);
rb_grad.setChecked(true); //Here the radio button is checked
rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.rb_given_rad:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new GivenRad());
fragmentTransaction.commit();
break;
}
}
});
}
}
This is my host XML layout:
<?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:id="#+id/activity_sphere_surface_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dev.indie.nayir55.geometrycompanion.SolidShapesOperations.SphereOperations.SphereSurfaceArea">
<RadioGroup
android:id="#+id/rg_sphere_sa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!--This is the button checked by default-->
<RadioButton
android:id="#+id/rb_given_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given radious"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
/>
<RadioButton
android:id="#+id/rb_given_vol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given volume"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"/>
<RadioButton
android:id="#+id/rb_given_circ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given circumference"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"/>
</RadioGroup>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rg_sphere_sa">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Fragment(s) [DONE]:
This is my fragment .java code:
public class GivenRad extends Fragment
{
public GivenRad()
{
// Required empty public constructor
}
public static GivenRad newInstance()
{
return new GivenRad();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_given_rad, container, false);
return rootView;
}
}
This is my fragment XML layout:
<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"
tools:context="dev.indie.nayir55.geometrycompanion.Fragments.SolidGeometry.FragSphere.GivenRad">
<TextView
android:text="Radious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView9"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"/>
</android.support.constraint.ConstraintLayout>
ID to the container [DONE]:
Inside my host XML layout you'll find the container:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rg_sphere_sa">
</FrameLayout>
A way to dynamically use fragments [DONE]:
Here I'm trying to use fragments dynamically using the radio buttons and a switch:
rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.rb_given_rad:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new GivenRad());
fragmentTransaction.commit();
break;
}
}
});
So... my problem is this:
Using a radio group and radio buttons I set a radio button checked by default, and when I open the host activity containing the radio buttons and the fragment, the radio button is checked but it is not showing the fragment until I check again the button, also if I use if statement is the same result what am I doing wrong?
At the moment, you are only loading a fragment in response to an event in onCheckedChanged(). You also need to load the appropriate fragment in onCreate(). To start, you might want to just copy and paste the code which loads a fragment directly into onCreate(). Eventually you can reduce duplicated code by putting the fragment-loading code in a method (i.e. loadFragment()) which is called from onCreate() and from onCheckedChanged().
What is happening is that you are checking the radio by default, and only loading fragments if the radio is changed or pressed. I would suggest loading the default fragment at runtime to avoid having to hit the button to get it to show up.
I will answer my own question
Here is another approach of the desired behavior I was looking for
Change a fragment
Hope this helps
My requirement is, I've a main activity and I want to display an image and text instead of progress bar in a separate fragment.
I've a separate fragment and I want to display the fragment when user clicks on a button in MainActivity.
My issue is, when I start the app, the fragment gets displayed by default. I want the fragment to be displayed only when user clicks on button. When I start the app I want the activity to be displayed.
I've an activity_main xml as below
<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"
android:background="#drawable/background" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip" >
<TextView
android:id="#+id/text1"
style="#style/textForLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="#string/text1" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dip"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:prompt="#string/spinner1"
android:spinnerMode="dialog" >
</Spinner>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text2"
style="#style/textForLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="#string/label2" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dip"
android:layout_span="6"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:prompt="#string/spinner2"
android:spinnerMode="dropdown" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/red"
android:textSize="#dimen/text_medium"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<fragment
android:id="#+id/progress_bar_fragment"
android:name="com.test.pushnotificationeclipse.ProgressBarFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The fragemnt_progress_bar.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/NoActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_all" >
<ImageView
android:id="#+id/imageView_frag_pgbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text_frag_pgbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView_frag_pgbar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="74dp"
android:textColor="#color/basic_color"
android:textSize="#dimen/text_medium_large" />
</RelativeLayout>
My ProgressBarFragment.java is as below
public class ProgressBarFragment extends Fragment {
#InjectView(R.id.text_frag_pgbar)
TextView textView;
#InjectView(R.id.imageView_frag_pgbar)
ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_progress_bar, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onDetach() {
super.onDetach();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onResume() {
super.onResume();
}
}
My MainActivity.java is as below: When I pass true tohideAndShowFragment(), the fragment should be hidden and when I pass false the fragment should be shown.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
context = getApplicationContext();
hideAndShowFragment(true);
}
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ProgressBarFragment pf = new ProgressBarFragment();
if (hide) {
ft.hide(pf).commit();
} else {
ft.show(pf).commit();
}
}
}
The issue here is your hideAndShowFragment method. You are creating a new instance of ProgressBarFragment, but the FragmentTransaction hide and show methods only work on an attached fragment.
Instead, you should get the fragment you already defined in your XML by id.
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
ProgressBarFragment pf =
(ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
FragmentTransaction ft = fm.beginTransaction();
if (hide) {
ft.hide(pf).commit();
} else {
ft.show(pf).commit();
}
}
Additionally, this line is concerning:
context = getApplicationContext();
Your Activity is also a Context object, so unless you need the application specific context for something, you generally want to be using this where you need a context inside your activity.
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(yourfragment)
.commit();
Ps:-Dont Don't mess with the visibility flags of the container - FragmentTransaction.hide/show does that internally for you.
Src:- "https://stackoverflow.com/a/16490344/1632286"
You might be better of getting an instance of the fragment in code and using the technique from the official docs as shown below and here (search for 'newInstance'). Add the following static method to your fragment.
public static ProgressFragment newInstance() {
DetailsFragment f = new DetailsFragment();
// Supply args here if needed.
/*Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);*/
return f;
}
Once you have that you can add the fragment to the top view layout with the following fragment transaction code which is inside the button click method.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.add(android.R.id.content, ProgressFragment.newInstance(), "ProgressFragmentTag");
tr.commit();
You can then use the following code to remove the fragment if needed;
FragmentManager fm = getSupportFragmentManager();
ProgressFragment frag = fm.findFragmentByTag("ProgressFragmentTag")
if (frag!=null)
{
FragmentTransaction tr = fm.beginTransaction();
tr.remove(frag).commit();
}
Hope this helps!
I resolved it by adding the fragment instead of having it shown:
public void hideAndShowFragment(boolean hide) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ProgressBarFragment pf = (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
if (hide) {
ft.hide(pf).commit();
} else {
ft.add(android.R.id.content, pf, "ProgressFragmentTag").commit();
}
}
int count =1;
if (count == 1) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction =
fm.beginTransaction();
fragmentTransaction.replace(R.id.labelframe, new
labelfregaments());
fragmentTransaction.commit();
count =0;
}
else if (count == 0) {
FragmentManager fm = getFragmentManager();
labelfregaments fs = (labelfregaments)fm.findFragmentById(R.id.labelframe);
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.hide(fs);
fragmentTransaction.commit();
count=1;
}
I'm trying to transit between 2 fragment from games.xml to levels.xml
its like angry bird game menu:
games: http://i.stack.imgur.com/9S7Ar.png
levels:http://i.stack.imgur.com/uB8rP.jpg
games.java:
public class Games extends Fragment implements OnClickListener{
public Games(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.games, container, false);
return rootView;
}
#Override
public void onClick(View v) {
Fragment f = null;
switch (v.getId()) {
case R.id.s4:{
f = new Levels();
break;
default:
break;
}
if (f != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.lvl_container, f);
transaction.addToBackStack(null);
transaction.commit();
}
}
level.java:
public class Levels extends Fragment {
public Levels(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.levels, container, false);
return rootView;
}
}
levels.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lvl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/s4"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLabel"
android:src="#drawable/lvlgray_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
games.xml
<?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">
<TextView
android:id="#+id/lastgame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="#string/games"/>
<ImageButton
android:id="#+id/s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lastgame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="#drawable/s4" />
the problem is when I click on imagebotton nothing happens (should go to levels), I think there is problem with these line
transaction.replace(R.id.lvl_container, f);
but I don't know what is it. Any ideas?
I've already seen this (developer.android.com/guide/components/fragments) but no help! What did I miss?
Is there any other way to transition between 2 fragments?
Thank you
Add this line in onCreateView at Games.java:
ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
im.setOnClickListener(this);
This make refrence to your imagebutton and set onClickListener on it to call your onClick method.
I have a FragmentActivity which has a button, on clicking the button I am showing a dailog
public class FragMainActivity extends FragmentActivity implements android.view.View.OnClickListener {
Button shoBut;
public String DailogTag="dialog";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shoBut= (Button) findViewById(R.id.button1);
shoBut.setOnClickListener(this);
}
#Override
public void onClick(View v) {
MyDialogFragment newFragment = MyDialogFragment.newInstance();
FragmentManager fm ;
fm=getSupportFragmentManager();
newFragment.show(fm, DailogTag);
}
}
My dialog Class
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inView= inflater.inflate(R.layout.dialog_layout, container, false);
return inView;
}
}
this is my dialog_layout xml file
<?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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="36dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="36dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Hi, i am the dailog BOX...Boom"
android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>
I am getting the dailog properly
But I have a Fragment class, ExampleFragment like this
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_content, container, false);
}
}
Whose layout is
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Fragment Conttent"
android:textAppearance="?android:textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
When i try to put this fragment in my dailog layout , my application is crashing.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="You really want to read this text?"
android:textAppearance="?android:textAppearanceMedium" />
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.addfragment.ExampleFragment"
android:id="#+id/example_fragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
When I put it in my activity_main layout it shows the fragment, but the same does not work in dailogFragment.
I am getting
08-28 15:44:09.969: E/AndroidRuntime(438): android.view.InflateException: Binary XML file line #22: Error inflating class fragment
Anyone have any suggestion for this ? Why cant a fragment be included in dailogFragment layout ?
Why can't we add a fragment in a daiologFragment ?
, why cant a fragment be included in
dailogFragment layout ?
Because nested fragment can't be added to a parent fragment through inflation of a layout file containing them. Below is a note from the docs:
Note: You cannot inflate a layout into a fragment when that layout includes a < fragment >. Nested fragments are only supported when added to a fragment dynamically.
Any have any suggestion for this ?
Add the ExampleFragment in code: in the onCreateView() method of the dialog fragment create a instance of ExampleFragment and using getChildFragmentManager() add it to a container from dialog_layout.