How to add 2 fragments dynamically in a single layout? - android

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

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

How to use fragments correctly

I am trying to understand how to use fragments but they are not working for some reason. Can you please explain how to use them or at least tell me what is wrong with my code.
Main activity:
package test.testing;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import layout.BlankFragment;
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.main, fragment);
fragmentTransaction.commit();
}
}
Mainactivity 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: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"
android:orientation="vertical"
tools:context="test.testing.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
</LinearLayout>
BlankFragment:
package layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import test.testing.R;
/**
* A simple {#link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
BlankFragment 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"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingBottom="20dp"
android:background="#000000"
tools:context="layout.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/graycircle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:text="Event 1"/>
</LinearLayout>
</LinearLayout>
Thank you
In your mainactivity.xml
Change this :
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
to
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
This will work because when you are using fragment tag then you need to set the class there in xml itself but when you are creating a fragment programatically like you are doing in this case, XML can have only a container in which fragment is inflated.
-
Update
Your code in MainActivity for adding fragment is incorrect. You are using support fragment hence you should use getSupportFragmentManager()
Make the following changes in your code.
Change in import statement of MainActivity.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
Change in code for adding fragment:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment done = new BlankFragment();
fragmentTransaction.add(R.id.main, done, "frag");
fragmentTransaction.commit();
I have modified your code in here.
http://pastebin.com/BEqExkbN

Android - Sliding Fragments

I'm trying to implement the sliding animation to transit from fragment1 to fragment2.
I'm using setCustomAnimations method. And I understand I need to use the frame method in order to replace fragments.
My code:
package com.example.fragmentss;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity implements MyListFragment.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_rsslist_overview);
}
// if the wizard generated an onCreateOptionsMenu you can delete
// it, not needed for this tutorial
#Override
public void onRssItemSelected(String link) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.fragmentss.MyListFragment" ></fragment>
<FrameLayout android:id="#+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
MyListFragment class
package com.example.fragmentss;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview,
container, false);
return view;
}
public void onClick2(View view) {
switch (view.getId()) {
case R.id.button2:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
DetailFragment newFragment = DetailFragment.newInstance();
ft.replace(R.id.details, newFragment, "mylistFragment");
// Start the animated transition.
ft.commit();
break;
}
}
} ~
fragment_rssitem_detail.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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto 2"
android:onClick="onClick1"/>
<TextView
android:id="#+id/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Frag 1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</LinearLayout>
fragment_rsslist_overview.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto 1"
android:onClick="onClick2" />
<TextView
android:id="#+id/textView1"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:text="Frag 2"
android:textSize="30dip"/>
</LinearLayout>
I guess what you are trying to implement can very easily be implemented by a ViewPager
ViewPager is a custom component that was introduced to implement easy and smooth switching between fragments . Android Documentation has a very good and easy to understand example to understand it's use Take a look at http://developer.android.com/training/animation/screen-slide.html .You can also see similar questions that have been answered
Example/Tutorial for ViewPager and Fragments and
How to use Android ViewPager?

Android Fragment behaving weird

The activity_main.xml is like this
<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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>
The main activity class is like this
package com.example.testfragmentshoneycomb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
first_fragment.xml is like this
<?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:orientation="vertical"
android:background="#color/grey" >"
<TextView
android:id="#+id/text_view_one_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View one" />
<TextView
android:id="#+id/text_view_two_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View two" />
<TextView
android:id="#+id/text_view_three_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View three" />
</LinearLayout>
FirstFragment class is like this
package fragments;
import com.example.testfragmentshoneycomb.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
return view;
}
}
It shows only first button and nothing else on the screen. If I remove the first button from activity_main.xml it shows the fragment but doesn't show the second button.
Min SDK version is 11 and build target is android 4.1
set android:orientation="vertical" in your activity layout.
Its because by default LinearLayout's orientation is horizontal. Therefore the whole screens width is captured by the First Button and Fragment.
Are you sure you want to see it like?
First_Button Fragment Second_Button
If Yes the use layout_weight. If No then give orientation=vertical to LinearLayout which will show your layout output as
First_Button
Fragment
Second_Button
set LinearLayout orientation to vertical, it's horizontal by default.
read docs carefully
Use the following layout :
<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"
android:orientation="vertical" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>

Categories

Resources