TabHost with Fragment not work - android

I have my FragmentActivity with a NavigationDrawer, and in that i call my fragment, for example:
case 3:
fragment = new Edit();
break;
now, in Edit fragment i want, (if possible), have a tab view with 2 fragment inside. So i set my Edit fragment with:
public class Edit extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
EditSource.class, savedInstanceState);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
EditSource.class, savedInstanceState);
return mTabHost;
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
EditSource is another fragment.
The app work, but when i go in Edit fragment i see the Tab view but i don't see the EditSource fragment inside that only black..sorry for my english.

Related

replace fragment with another fragment along with remove view

I have one activity and two Fragments. FragmentA and FragmentB.
I need to implement like, Initially FragmentA should be there.
and After Button click FragmentB should be there.
in my condition when activity is being displayed,fragmentA is showing and when click to button,fragmentB placed at the bottom of fragmentA and do not replace FragmentA
My activity Code is:
public class FragmentActivity extends AppCompatActivity {
FrameLayout activityFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
activityFragment = (FrameLayout) findViewById(R.id.activityFragment);
Fragment fragment = new FragmentA();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.activityFragment, fragment);
transaction.commit();
}
public void goToFrag(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = new FragmentB();
transaction.add(R.id.fragmentRoot, fragment);
transaction.commit();
}
}
my first fragment code:
public class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.fragment_a,container,false);
return view;
}
}
my second fragment code:
public class FragmentB extends Fragment {
public FragmentB() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.fragment_b,container,false);
return view;
}
}
First of all, you should always have a FrameLayout in xml to replace your fragments there, then you use different containers to add fragments activityFragment and fragmentRoot .You should replace fragments in one container. You can use either add or replace for FragmentManager , so it activityFragment is your container (which should be a framelayout as a wrapper as the docs suggest) you use
public void goToFrag(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = new FragmentB();
transaction.replace(R.id.activityFragment, fragment); // use the same container where you switch A and B
transaction.commit();
}

Dynamically add elements to fragment

Hi I'm new to android and was trying to dynamically add content to a Fragment.
I've two Fragments inside an Activity. The first point to the second like this:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_welcome,
container, false);
Button searchActivityBtn = (Button) rootView.findViewById(R.id.SearchActivityBtn);
searchActivityBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("##SecondActivity", "search btn click");
getFragmentManager().beginTransaction()
.replace(R.id.welcome_fragment, new searchAnnouncementsFragment()).addToBackStack(null).commit();
}
});
return rootView;
}
}
the second Fragment class is
public static class searchAnnouncementsFragment extends Fragment {
public searchAnnouncementsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search,
container, false);
return rootView;
}
//public void onStart(){
// super.onStart();
// RelativeLayout root = (RelativeLayout) getActivity().findViewById(R.id.annunciContainer);
// Log.d("##onstart", "root:" + (root == null));
//}
}
As you can see i tried to do something in onStart method but I've always a bunch of errors, the R.id.annunciContainer is the id of the fragment_search.xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="#+id/annunciContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
What I'm trying to achieve is to dynamically add elements like other FrameLayout to my Fragment ( represented by the above RelativeLayout ).
Any thought?
You didn't mention the name of the activity, lets say the name as Activity1,
First create a linear layout or frame layout in the Activity1
Second add the following lines of code to your Activity1 for inflating PlaceholderFragment into your layout in Activity1
PlaceholderFragment pf = new PlaceholderFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.annunciContainer, pf,"place_holder");
ft.commit();
For inflating searchAnnouncementsFragment replace PlaceholderFragment with searchAnnouncementsFragment.
What I was searching for are ListView

Detecting event on a FragmentTabHost present in Fragment

What I have:
I have a fragment
I am using SherlockActionBar library
I have a FragmentTabHost in Fragment
What i want to know: I want to detect onClick event each tab and perform some action based on it
Fragment1.java:
public class Fragment1 extends SherlockFragment{
private FragmentTabHost mTabHost;
//Mandatory Constructor
public Fragment1() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),
RatingAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),
PriceAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),
DistanceAscending.class, null);
return rootView;
}
}
I'm assuming what you want to do is detect when a specific tab has been clicked in your FragmentTabHost. This can be accomplished quite simply by adding a setOnTabChangedListener.
Here is an example:
FragmentTabHost t = new FragmentTabHost(getActivity());
t.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO your actions go here
}
});
}

How can I switch from the old tabhost to swipe tabs in Android?

I am currentley using the old tabHost to hold a hand full of different fragments as tabs. How can I easily switch this over to the newer swipe tabs?
public class StatisticsTab extends Fragment {
private FragmentTabHost mTabHost;
//Mandatory Constructor
public StatisticsTab() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Basic").setIndicator("Basic"),
StatisticsPage.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Brewery").setIndicator("Brewery"),
BreweryStatistics.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Style").setIndicator("Style"),
StyleStatistics.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Taste").setIndicator("Taste"),
TasteStatisticsPage.class, null);
return rootView;
}
}
Try PagerSlidingTabStrip(https://github.com/astuetz/PagerSlidingTabStrip). An interactive indicator to navigate between the different pages of a ViewPager. It works awesome for me.

Android fragment nested in another fragment: getParentFragment() returns 0

Basically the situation is like this: I have an Activity, which displays a three Fragments within a FragmentPagerAdapter, and within one of these Fragment, I nest another Fragment. I added the fragments like this:
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
fragmentTransaction.commit();
When I try to call getParentFragment() inside of MainPageFragment(), it returns null.
How can that be? I checked multiple posts here on stackoverflow as well as on other blogs, and they all came to the conclusion that if you nest one fragment within another one, calling getParentFragment should return its parent fragment. Returning null means, that the parent is an activity as stated here. Which is not the case. Please tell me I'm making some kind of silly mistakes :\
This is the Fragment that is a child to the main Activity
public class MainPage extends Fragment {
private FragmentTransaction fragmentTransaction;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
global_var = new Global();
rootView = inflater.inflate(R.layout.main_page, container, false);
return rootView;
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
fragmentTransaction.commit();
}
}
This is the Fragment which is a child to the Main Page Fragment
public class MainPageFragment extends Fragment{
private View rootView;
private Fragment parentFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.main_page_list, container, false);
if (getParentFragment() == null) Log.i("damn", "it");
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
}
Please tell me if I need to provide more code, I extracted things like ListView and so on because I thought it would be irrelevant.
Edit: I'm using android.support.v4.app
Check that you have pass FragmentManager reference to all of your Fragments from your FragmentPagerAdapter and then use getParentFragment(). May be this will help you.

Categories

Resources