Button Click inside Fragment to new fragment - android

I'm new to android I got stuck at navigating from one fragment to other fragment onclick of a button. In my fragment_chat.xml there are two button one is for chatManager and other is for navigate to other fragment.The problem is when I click navigate button(Button) the app is not responding. Here is my code
ChatFragment.java
package com.example.android.wifidirect.discovery;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* This fragment handles chat related UI which includes a list view for messages
* and a message entry field with send button.
*/
public class WiFiChatFragment extends Fragment {
private android.support.v4.app.FragmentActivity context;
private View view;
private ChatManager chatManager;
private TextView chatLine;
private ListView listView;
ChatMessageAdapter adapter = null;
private List<String> items = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_chat, container, false);
chatLine = (TextView) view.findViewById(R.id.txtChatLine);
listView = (ListView) view.findViewById(android.R.id.list);
adapter = new ChatMessageAdapter(getActivity(), android.R.id.text1,
items);
listView.setAdapter(adapter);
view.findViewById(R.id.button1).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (chatManager != null) {
chatManager.write(chatLine.getText().toString()
.getBytes());
pushMessage("Me: " + chatLine.getText().toString());
chatLine.setText("");
chatLine.clearFocus();
}
}
});
view.findViewById(R.id.Button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
android.support.v4.app.Fragment fragment = new TopRatedFragment();
android.support.v4.app.FragmentManager fragmentManager = context.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.infotraffic, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();``
}
});
return view;
}
chat_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dip"
android:layout_marginTop="50dp"
android:transcriptMode="alwaysScroll" >
<!-- Preview: listitem=#android:layout/simple_list_item_1 -->
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_gravity="bottom" >
<EditText
android:id="#+id/txtChatLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.90"
android:focusableInTouchMode="true"
android:hint="#string/txt_hint"
android:visibility="visible" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_weight="0.10"
android:text="#string/btn_send" />
</LinearLayout>
<Button
android:id="#+id/Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/gps_location" />
</FrameLayout>

Try to replace this line of your code:
android.support.v4.app.Fragment fragment = new TopRatedFragment();
android.support.v4.app.FragmentManager fragmentManager = context.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.infotraffic, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
With this code:
TopRatedFragment fragment = new TopRatedFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.infotraffic, fragment);
transaction.addToBackStack(null);
transaction.commit();

Related

Fragment won't show up with onButtonClick

The fragment shows up by default but I wanted it to be hidden/invisible until I click this button. The problem that I am experiencing is that when I click this button, the fragment won't show up. I'm a complete beginner when it comes to fragments.
My codes in my Main Activity:
package com.example.julian.tutorial_fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View frag = findViewById(R.id.fragment3);
frag.setVisibility(View.GONE);
}
//This gets called by the TopFragment when the user clicks the button
#Override
public void createMeme(String top, String bottom) {
BottomPictureFragment bottomFragment = (BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setMemeText(top, bottom);
}
//this is the onButtonClick
public void sample(View view) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_Search hello = new Fragment_Search();
fragmentTransaction.add(R.id.fragment3, hello, "HELLO");
fragmentTransaction.commit();
}
}
My codes in Fragment_Search:
package com.example.julian.tutorial_fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Julian on 8/17/2017.
*/
public class Fragment_Search extends Fragment {
public interface Fragment_SearchInterface {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_layout, container, false);
return view;
}
}
My 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="com.example.julian.tutorial_fragment.MainActivity">
<RelativeLayout
android:layout_width="377dp"
android:layout_height="498dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<fragment
android:id="#+id/fragment2"
android:name="com.example.julian.tutorial_fragment.BottomPictureFragment"
android:layout_width="250dp"
android:layout_height="250dp"
tools:layout="#layout/bottom_picture_fragment"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="223dp"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<fragment
android:id="#+id/fragment"
android:name="com.example.julian.tutorial_fragment.TopSectionFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/top_section_fragment"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fragment2"
android:layout_alignStart="#+id/fragment"
android:text="Button"
android:onClick="sample"/>
<fragment
android:id="#+id/fragment3"
android:name="com.example.julian.tutorial_fragment.Fragment_Search"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="#+id/button2"
android:layout_marginTop="92dp"
android:layout_toStartOf="#+id/button2" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
change your sample function and add this
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.your_layout, new Fragment_Search())
.addToBackStack(null)
.commit();

Error in Fragment Transaction

I have just started with android fragment and git stuck while implementing an example from internet.Attaching the following code below.It would be really grate to get some help.
I am getting a error at the line ft.add(R.id.fragmentone,fragment).
Regards.
MainActivity.java
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;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getfragment(View view) {
FragmentOne fragment = new FragmentOne();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.fragmentone,fragment);
}
}
FragmentOne.java
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container,false);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vighnesh.fragmentsexp.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonfragment"
android:layout_margin="20dp"
android:text="Click to open fragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentone"
android:layout_margin="20dp">
</fragment>
</LinearLayout>
fragment_fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vighnesh.fragmentsexp.FragmentOne">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
Replace this code in your activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getfragment();
}
public void getfragment() {
FragmentOne fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentone, fragment);
ft.commit();
}
change it.
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;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnFragOne = (Button)findViewById(R.id.buttonfragment);
btnFragOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentOne fragment = new FragmentOne();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.fragmentone,fragment); getfragment();
}
});
}
}
So use getSupportFragmentManager() instead of getFragmentManager()
Because the FragmentOne is from the support library (android.support.v4.app.Fragment) not from the framework (android.app.Fragment), you need to use android.support.v4.app.FragmentManager to do transaction with FragmentOne.

How to change from fragment to another fragment instantly control by one of the fragment?

I have a navigation drawer which I would like to press the button in the list view and change the fragment on the right side instantly. How can I do that? Why my code doesn't work?
Here is my code:
DrawerFragment.class
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import java.util.ArrayList;
public class DrawerFragment extends Fragment {
private static ListView mSelectionListView;
private ImageView mUserIcon;
private TextView mUserName;
//region (Widgets) Declaration
NonScrollListView mNewsListView, mELeaveListView, mEClaimListView, mEAttendanceListView;
//endregion
//DrawerFragmentListener drawerFragmentListener;
//endregion
Fragment mFragment;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
ExpenseClaimPageFragment expenseClaimPageFragment;
ClaimFormPageFragment claimFormPageFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_drawer_fragment, container, false);
expenseClaimPageFragment = new ExpenseClaimPageFragment();
claimFormPageFragment = new ClaimFormPageFragment();
mEClaimListView = (NonScrollListView) view.findViewById(R.id.drawer_fragment_eClaim_selection);
ArrayList<DrawerSelectionEClaimResult> selectionEClaimResults = GetDrawerSelectionEClaimResult();
mEClaimListView.setAdapter(new CustomDrawerSelectionEClaimBaseAdapter(getActivity(), selectionEClaimResults));
mEClaimListView.setOnItemClickListener(mEClaimListViewOnItemClick);
return view;
}
public ArrayList<DrawerSelectionEClaimResult> GetDrawerSelectionEClaimResult() {
ArrayList<DrawerSelectionEClaimResult> results = new ArrayList<>();
DrawerSelectionEClaimResult dsecr = new DrawerSelectionEClaimResult();
dsecr.setSelection("Expenses Claim Entry");
dsecr.setDrawable(R.drawable.expenseclaim);
results.add(dsecr);
dsecr = new DrawerSelectionEClaimResult();
dsecr.setSelection("Expenses Claim Form");
dsecr.setDrawable(R.drawable.expenseclaim);
results.add(dsecr);
return results;
}
public NonScrollListView.OnItemClickListener mEClaimListViewOnItemClick = new NonScrollListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
mFragmentManager = getActivity().getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragment = new ExpenseClaimPageFragment();
mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
mFragmentTransaction.commit();
break;
case 1:
mFragmentManager = getActivity().getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragment = new ClaimFormPageFragment();
mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
mFragmentTransaction.commit();
break;
}
}
};
}
Main.xml
<!--Main Content View (Expense Claim Page Content)-->
<FrameLayout
android:id="#+id/MainPageFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.sample.flexsystem.expenseclaimapplication.ExpenseClaimPageFragment"
android:id="#+id/fragment2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/activity_expenseclaim_page" />
</FrameLayout>
<!--Navigation Drawer View-->
<LinearLayout
android:id="#+id/DrawerLin"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFFFFF"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:name="com.sample.flexsystem.expenseclaimapplication.DrawerFragment"
android:id="#+id/fragment"
tools:layout="#layout/activity_drawer_fragment" />
</LinearLayout>
I have no idea why my code doesn't work, what is the main code to replace the fragment to another? I know that I need a layout to save a fragment inside and use replace() method to handle it, but it didn't work. Is there anything wrong of my code?
I think I can not replace the fragment from another fragment since fragment doesn't communicate to each other right?
UPDATE 1
Main Activity class
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.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class ExpenseClaimPage extends AppCompatActivity implements DrawerFragment.DrawerFragmentListener{
DrawerLayout mDrawerLayout;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Fragment mFragment;
#Override
public void setPosition(int position)
{
switch(position)
{
case 0:
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragment = new ExpenseClaimPageFragment();
mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
break;
case 1:
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragment = new ClaimFormPageFragment();
mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
break;
}
}
//region onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
//endregion
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Looking at your main.xml, the main content:
Main Content View (Expense Claim Page Content)
<FrameLayout
android:id="#+id/MainPageFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="ExpenseClaimPageFragment"
android:id="#+id/fragment2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/activity_expenseclaim_page" />
</FrameLayout>
You cant replace fragment which is added by using xml. Therefore, I suggest that you should add your ExpenseClaimPageFragment manually when the activity is created. After that, you can replace it with another fragment easily.

java.lang.IllegalStateException: Activity has been destroyed at Calling fragment inside another fragment to replace on framelayout

I am new to android and learning fragment instead of multiple, i have problem. when i call planet fragment from friends "java.lang.IllegalStateException: Activity has been destroyed" android runtime FATAL exception occurs here is my code. i saw this page but couldn't figure out how to use it. I will be grateful for any help
fragment_planet.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
Friends.java
package com.app.hubara;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Friends extends Fragment {
public Friends() { }
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.content_friends, container, false);
MainActivity main = new MainActivity();
main.friends();
return rootView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="100dp"/>
<Button
android:id="#+id/display"
android:height="70dp"
android:width="300dp"
android:layout_marginTop="0dp"
android:text="Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity
package com.app.hubara;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import java.util.Locale;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.display);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Friends frag_friends = new Friends();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, frag_friends);
fragmentTransaction.commit();
}
});
}
public void friends(){
PlanetFragment planetFragment = new PlanetFragment();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, planetFragment);
fragmentTransaction.commit();
}
public static class PlanetFragment extends Fragment {
public PlanetFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt("Earth");
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", "Earth");
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
To access an Activity, you shouldn't do this:
MainActivity main = new MainActivity();
main.friends();
Because this destroys current Activity then instantiates another new Activity.
You can access it appropriately by calling getActivity():
MainActivity main = getActivity();
main.friends();
Note that actually you are recommended to define and use an interface to communicate between Fragment and Activity. If you want to learn further, please refer to the official training.

Android FragmentTransaction won't add or replace fragment to container

This question has been asked before, however, I have not found an appropriate answer for my problem. I am building a simple fragment activity that displays three buttons at the top of the screen in one container that when pressed update the fragment in the rest of the screen. Just like a tab host. I am using android.support.v4.app.Fragment in all code, but still cannot get the transaction manager to display the fragment in the container. I've been banging my head on the keyboard for hours and connot find the problem.
Here is the main activity extending FragmentActivity:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.menu_container, new MenuFragment()).commit();
}
}
}
Here is the MenuFragment class extending fragment for my tabs:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.*;
public class MenuFragment extends Fragment{
Fragment frag;
FragmentTransaction fragTransaction;
public MenuFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frament_menu, container, false);
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
System.out.println(fragTransaction.toString());
fragTransaction.commit();
Button btnString = (Button) view.findViewById(R.id.button_string);
Button btnSlider = (Button) view.findViewById(R.id.button_slider);
Button btnTouch = (Button) view.findViewById(R.id.button_touch);
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new SlidersFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new TouchFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
return view;
}
}
Here is one of the three fragments that I can't get to display:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class StringsFragment extends Fragment {
public StringsFragment() {
}
public View onCreatView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_strings, null);
return rootView;
}
}
activity_test_udpfragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/menu_container"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/container"
android:background="#aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Here is frament_menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="android.att/buttonBarStyle" >``
<Button
android:id="#+id/button_string"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="String" />
<Button
android:id="#+id/button_slider"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Slider" />
<Button
android:id="#+id/button_touch"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Touch" />
</LinearLayout>
And here is fragment_strings:
<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">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:scaleType = "centerCrop"
android:src="#drawable/me_hooded" />
</RelativeLayout>
Any help is greatly appreciated!
EDIT:
Here is new TestUDPActivityFragment with buttons to change fragment:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
Fragment frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
Button btnString = (Button)findViewById(R.id.btn_string);
Button btnSlider = (Button)findViewById(R.id.btn_slider);
Button btnTouch = (Button)findViewById(R.id.btn_touch);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new StringsFragment()).commit();
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new StringsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new SlidersFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new TouchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
}
}
}
I can see a couple of problems...
First, your FrameLayout containers are defined in your activity_test_udpfragment.xml layout file which is inflated by your TestUDPFragmentActivity. The problem with attempting to create / add your StringsFragment in your MenuFragment code using the following...
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
...is the MenuFragment inflates a layout from your frament_menu.xml which doesn't contain a FrameLayout with id of R.id.container.
That's your first problem but the second is with attempting to use a Fragment to create / add a Fragment in the first place. Your MenuFragment is not able to use the Framelayout in the TestUDPFragmentActivity.
In short, I suspect you want both Fragments to appear in your Activity layout (and not have the StringsFragment as a 'child' of MenuFragment) in which case you need to put all code for creating / adding both Fragments into your Activity.

Categories

Resources