Showing two fragments when I intend to show one - android

I have a view that consist of two buttons and a fragment. If you press button1 then fragment one (which is a textview) should be shown. If you press button two then fragment two (which is a textview) is shown. The problem is that I only want one of the fragments to show. If I press the buttons forth and back fragment one is always shown on to while it switches between fragment 1 and 2 below.
Why is that? How can I solve this problem so only one fragment will be shown?
Here is the java class:
package com.example.fragmentstest;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
Here is the XML-class:
<?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="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.example.fragmentstest.FragmentOne"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

You cannot replace fragments specified via XML layout. You can remove/replace fragments added via code only. So, what you need to do - is to specify container (FrameLayout for instance) and add/replace fragments in this container from the code:
<?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="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
}

Related

Why am I not able to switch between two Fragments in Android

I have tried everything but still, I am not able to change Fragments from CalculatorP1 to Calculator_P2. When I click the button only one Fragment is shown but when I click the button again the second Fragment does not show itself in the APP. I tried everything and I also tried to use TWO BUTTONS bt only one Fragment Shows itself. The second Fragment doesn't show itself. If I switch the fragments only the first Fragment shows bt the 2nd fragment doesn't show itself.
Please help.
My App... See how the button text changes but the Fragment remains the same
My XML codes are given Below:
<?xml version="1.0" encoding="utf-8"?>
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">
<LinearLayout
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:orientation="vertical">
<Button
android:id="#+id/bn_f1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 1" />
<Button
android:id="#+id/bn_f2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="276dp"
android:layout_height="350sp"
android:layout_alignParentBottom="true"
android:orientation="vertical"></LinearLayout>
My Activity.java is given below:
package com.example.sudeepsarker.sudeepcalculatorvv;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bn_fragment;
private boolean status= false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn_fragment = findViewById(R.id.bn_f1);
bn_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(!status){
CalculatorP1 p1 = new CalculatorP1();
fragmentTransaction.add(R.id.fragment_container,p1);
fragmentTransaction.commit();
bn_fragment.setText("Fragment 2");
status=true;
}
else{
Calculator_P2 p2= new Calculator_P2();
fragmentTransaction.add(R.id.fragment_container,p2);
fragmentTransaction.commit();
bn_fragment.setText("Fragment 1");
status=false;
}
}
});
}
}
My Fragment one xml file:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#8F3A1E"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My first Part of calculator"
android:textColor="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
My Fragment Two XML File:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#077715"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Second Part of calculator"
android:textColor="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>

App crashes when i open it with Fragments

I have this problem:
I'm trying to create an App with 2 buttons on a Layout and this buttons open 2 different Fragments, but the problem is that when I open the app it crashes.
Here's my code:
MainActivity:
package com.example.akroma.feina_final;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button boton1,boton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton1 = (Button) findViewById(R.id.button2);
boton1.setOnClickListener(this);
boton2 = (Button) findViewById(R.id.button);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Fragment fragment;
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.button:
fragment = new FragmentOne();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
case R.id.button2:
fragment = new FragmentTwo();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
}
}
}
I created the fragments standard (Blank).
Name of fragments:
FragmentOne
FragmentTwo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
tools:context="com.example.akroma.feina_final.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mapa" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Negocis" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Name of fragments on the tree:
FragmentOne
FragmentTwo
Edit: error: Error inflating class fragment
Solution to the inflating:
Thanks to: #svkaka
Change the fragment for:
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
New error:
must implement OnFragmentInteractionListener
Any ideas? I don't know what I'm doing bad.
Thanks in advance
Replace this
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
with this
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
U are mixing dynamic and static fragments

In a fragment if I press back button go to SettingsActivity.class

I have a activity_settings.xml file like below in this file I have 3 text views if I click one it will take me to a fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.crowderia.chat.SettingsActivity"
android:background="#color/black">
<TextView
android:id="#+id/tv_added_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Added Me" />
<TextView
android:id="#+id/tv_add_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Friends" />
<TextView
android:id="#+id/tv_my_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Friends" />
<FrameLayout
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
My SettingsActivity.class like below
onclick event for each text view
tv_added_me.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setFragment(new AddedMeFragment());
}
});
set fragment method
public void setFragment(Fragment f) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.replace(R.id.settings_fragment, f);
fragmentTransaction.commit();
}
If I click each text view it will take me to each fragment but after I click back its not going to go to Settings Activity how can I make this work
Please help me Im new to these stuff
You should remove all the fragments from FragmentManager on the click of back button.
FragmentManager fm = getSupportFragmentManager();
for (Fragment f : fm.getFragments()) {
fm.beginTransaction().remove(f).commit();
}

I simply replacing with one fragment with other, but unable to solve why it's not working

I am simply writing code for transaction of fragment. But I am getting this error. I am new to android, I am unable to resolve this error.
However it was working with my old android studio. I think problem with my imports. Could you please help me? I don't think there is need of my fragment files. it's simply contaion onCreateView method only.
It's not replacing fragment.
Here my
MainActivity.java
package com.example.pkkeshar.tallymaster;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView transaction;
TextView summary;
TextView details;
FragmentManager fm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
transaction = (TextView) findViewById(R.id.transaction);
summary = (TextView) findViewById(R.id.details);
details = (TextView) findViewById(R.id.summary);
transaction.setOnClickListener(this);
summary.setOnClickListener(this);
details.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==findViewById(R.id.details)){
Fragment fragment = new details();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
if(v==findViewById(R.id.summary)){
Fragment fragment = new summary();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
if(v==findViewById(R.id.transaction)){
Fragment fragment = new transaction();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
}
}
Here 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: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="com.example.pkkeshar.tallymaster.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:paddingBottom="5dp"
android:id="#+id/linearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Transaction"
android:id="#+id/transaction"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Details"
android:id="#+id/details"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Summary"
android:id="#+id/summary"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout"
android:id="#+id/linearLayout1">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.pkkeshar.tallymaster.transaction"
android:id="#+id/fragment"
tools:layout="#layout/fragment_transaction" />
</LinearLayout>

Replace fragments in Android

I can't replace the fragments using the codes below. I mean that the second fragment appeared under the first fragment. The first fragmnent didn't hide as I expected. What is wrong here?
Thanks a lot!
My activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmenttwo = new FragmentTwo();
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1, fragmenttwo);
ft.commit();
}
});
};
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<fragment
android:id="#+id/fragment1"
android:name="com.example.fragementex.FragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragmnetone.xml
<?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="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment One" >
</EditText>
</LinearLayout>
fragmnettwo.xml
<?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="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="clip_horizontal"
android:ems="10"
android:gravity="center_vertical|center_horizontal|top|fill_vertical"
android:text="This is Fragment Two" >
</EditText>
</LinearLayout>
You cannot replace a fragment attached to the layout, you need to create a host container as FrameLayout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/fmchangebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment" />
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, you will able to - first - add and display the first fragment and replace it with the second as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout which contains the framelayout
setContentView(R.layout.activity_main);
final FragmentManager fm = this.getFragmentManager();
final Fragment fragmentone = new FragmentOne();
final Fragment fragmenttwo = new FragmentTwo();
// if first initialization
if(savedInstanceState == null) {
// display with "add" the first fragment
this.getFragmentManager().beginTransaction()
.add(R.id.frame_layout, fragmentone).commit();
}
Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
fmchangebutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
// "replace" the fragment inside the framelayout
ft.replace(R.id.frame_layout, fragmenttwo);
ft.commit();
}
});
}

Categories

Resources