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
Related
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/mainLayout">
<android.support.constraint.ConstraintLayout
android:id="#+id/loginCl1"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/loginCl2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="3">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#id/loginCl1"
app:layout_constraintStart_toStartOf="#+id/loginCl1"
app:layout_constraintEnd_toEndOf="#+id/loginCl1"
app:layout_constraintBottom_toTopOf="#+id/id"
app:layout_constraintVertical_weight="4"
android:src="#drawable/logo" />
<com.fapa.brent.fapa.Common.BrentEditText
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="#string/enterid"
app:layout_constraintBottom_toTopOf="#+id/pw"
app:layout_constraintEnd_toEndOf="#+id/loginCl1"
app:layout_constraintStart_toStartOf="#id/loginCl1"
app:layout_constraintTop_toBottomOf="#+id/image"
app:layout_constraintVertical_weight="1"/>
<com.fapa.brent.fapa.Common.BrentEditText
android:id="#+id/pw"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="#string/enterpw"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="#id/loginCl1"
app:layout_constraintEnd_toEndOf="#+id/loginCl1"
app:layout_constraintStart_toStartOf="#id/loginCl1"
app:layout_constraintTop_toBottomOf="#+id/id"
app:layout_constraintVertical_weight="1"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/loginCl2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/loginCl1"
app:layout_constraintVertical_weight="2">
<CheckBox
android:id="#+id/saveId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/saveId"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/login"
app:layout_constraintTop_toBottomOf="#+id/saveId"
app:layout_constraintEnd_toStartOf="#id/signup"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/signup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/signup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/saveId"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="#id/login" />
<Button
android:id="#+id/findId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/findid"
app:layout_constraintEnd_toStartOf="#+id/findPw"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/login" />
<Button
android:id="#+id/findPw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/findpw"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/findId"
app:layout_constraintTop_toBottomOf="#+id/signup" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.fapa.brent.fapa;
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;
import android.widget.EditText;
import com.facebook.stetho.Stetho;
import com.fapa.brent.fapa.Common.BrentEditText;
import com.fapa.brent.fapa.Encryption.Sha512;
import com.fapa.brent.fapa.Form.FindIdFragment;
import com.fapa.brent.fapa.Form.FindPwFragment;
import com.fapa.brent.fapa.Form.SignUpFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* CREATED BY BRENT ON 2018-12-11
*/
public class MainActivity extends AppCompatActivity {
#BindView(R.id.id)
BrentEditText mId;
#BindView(R.id.pw)
BrentEditText mPw;
#BindView(R.id.login)
Button mBtnLogin;
#BindView(R.id.signup)
Button mBtnSignup;
#BindView(R.id.findId)
Button mBtnFindId;
#BindView(R.id.findPw)
Button mBtnFindPw;
Fragment mFragment;
#OnClick(R.id.login)
void loginValidatation() {
String id = mId.getText().toString();
String pw = Sha512.getSHA512(mPw.getText().toString());
}
#OnClick({R.id.signup, R.id.findPw, R.id.findId})
void openUpFragmentByButton(Button button){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch (button.getId()){
case R.id.signup:
mFragment = new SignUpFragment();
fragmentChangeOrAdd(fragmentTransaction, mFragment);
break;
case R.id.findId:
mFragment = new FindIdFragment();
fragmentChangeOrAdd(fragmentTransaction, mFragment);
break;
case R.id.findPw:
mFragment = new FindPwFragment();
fragmentChangeOrAdd(fragmentTransaction, mFragment);
break;
default:
break;
}
}
private void fragmentChangeOrAdd(FragmentTransaction fragmentTransaction, Fragment fragment) {
if(fragmentTransaction == null) {
fragmentTransaction.add(R.id.mainLayout, fragment);
fragmentTransaction.commit();
}else {
fragmentTransaction.replace(R.id.mainLayout, fragment);
fragmentTransaction.commit();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Stetho.initializeWithDefaults(this);
}
}
Please take look at my openUpFragmentById() method. What I want to do is when I click buttons on activity_main.xml, I want to inflate one of my fragments and fill up the whole activity_main and not show the original content.
I understand that the main purpose of fragment is to divide, or split activities, but I am wondering if I can replace (or change) all original contents in activity_main.xml with the contents in one of my fragments (I know that a fragment need a viewGroup in activity_main.xml (in my case) to inflate).
Besides, if it can`t, should I have additional activities to deal with this?
If it can, please provide me useful links or advice. Thanks in advance.
I don't see your fragment xml but i face this problem and i think you must set background to your fragment xml parent layout
For example :
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentParent"
android:background="#android:color/white"
>
<!--your fragment views-->
</LinearLayout>
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"
/>
I want to add 2 fragments in a single layout like this:
I was able to do it using static fragment method but not able to do it dynamically. I want to target API 14 and above.
Here is the code:
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">
<FrameLayout
android:id="#+id/new_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/my_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
fragment1.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"
android:background="#00ff00"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment first"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
</LinearLayout>
fragment2.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"
android:background="#0000ff">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment second"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
</LinearLayout>
fragment1.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Rahul on 07-08-2017.
*/
public class fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1,container, false);
}
}
fragment2.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Rahul on 07-08-2017.
*/
public class fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2,container, false);
}
}
MainActivity.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Create new fragment and transaction
Fragment newFragment = new fragment1();
Fragment myFragment = new fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.add(R.id.new_placeholder, newFragment);
transaction.add(R.id.my_placeholder, myFragment);
transaction.commit();
}
}
In the output I am getting only first fragment displayed. The second fragment is not getting added. Output:
Can you please tell me what is going wrong?
please added android:layout_weight="1"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/new_placeholder"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:id="#+id/my_placeholder"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
>
</LinearLayout>
</LinearLayout>
You need layout weight android:layout_weight for both your frame layout
try below XML changes
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/new_placeholder"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/my_placeholder"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
The problem is in your activity_main.xml.
Use android:layout_weight="1" for each of your fragments container to show both of them on the screen.
Also fill_parent is deprecated. Consider using match_parent instead
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>
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();
}