I have an HomeAcivity with 4 fragments. 3 of the fragments have no Toolbar of their own and I am showing the Activity's Toolbar in them. However, the 4th Fragment have Toolbar of its own.
Currently I am seeing 2 Toolbars in 4th Fragment, one its own and other that of Activity.How can I hide the Activity's Toolbar and just show its own Toolbar?
Please note that I don't want to update the content of Activity's Toolbar instead of showing new Toolbar inside Fragment. I want to show Fragments own Toolbar and hide that of Activity.
Please help me out.
You can hide activity's toolbar from your 4th fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.GONE);
return rootView;
}
and show activity's toolbar from other three fragments
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.VISIBLE);
return rootView;
}
Related
I just googled for "setsupportactionbar fragment", but all I found was "getsupportactionbar from fragment". Is that similar to each other, or is there something special?
Yes it is similar, but you have to keep in mind that you have to place setSupportActionBar() in onCreateView if you want to use elements of fragments layout-file:
..
public class FragmentOne extends Fragment {
..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return view;
}
..
}
you should not use setSupportActionBar() in fragment. because every time you add this fragment on activity a new instance of toolbar is created. so every time a new Toolbar is showing in screen. so it is not a good approach.
I’m doing a navigation drawer in my android app. On my onNavigationDrawerItemSelected function i try on item select to change the current fragment into a new one. The problem is that when I change the current fragment its take the initial status of it. maybe because i'm doing this way
fragmentManager.beginTransaction()
.replace(R.id.cont, new SessionsFragement())
.commit();
I declare a new instance of my fragment and i think it’s normal, its display me the initial status of the thing. On my fragment i've try to set a textView to see if i will say it when i call the fragement from tha navigation drawer but it's not the case.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return inflater.inflate(R.layout.fragment_spot, container, false);
}
So please can anyone help me to fix this.
In the snippet you posted you are inflating again the view. You should return the one you modified
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return v;
}
The app I'm currently developing has an action bar with 5 tabs (fragments). One of these fragment shows an alert dialog, but the layout is blank. I want to put a background image, so I created a layout for that fragment and used inflater.inflate(...) method to set the layout.
Problem is that line of code sets that layout to ALL fragments. How can I limit it just to the fragment I need? Here's my code:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//...
AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
//...
ad.show();
inflater.inflate(R.layout.fragment_my, container); //this is the layout I want to inflate to my fragment
return super.onCreateView(inflater, container, savedInstanceState);
}
Also tried to replace the last two lines with:
return inflater.inflate(R.layout.fragment_trovami, container);
but i'm getting this error:
07-27 16:19:46.768: E/AndroidRuntime(1998): java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
Answer :
return inflater.inflate(R.layout.fragment_trovami, container,false);
I have a FragmentActivity which sets a main layout with 3 tabs. Each tab has it's own fragment which inflates is own layout into main layout.
For example, one of the three Fragments:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.fragment1, container, false);
}
}
When I add all the fragments and connect them with tabs everything works. When I click on tab1, layout of the Fragment1 shows, when I click on Tab2, layout of the Fragment2 shows.
The problem is that when I want to change something in that inflated layout (for example execute setText to textView in fragment1 layout), I receive a NullPointerException in that line and application stops.
This doesn't work:
TextView test = (TextView) findViewById(R.id.fragmentOneText1);
test.setText("Test text!");
How to set data to inflated layout?
You have to inflate your fragment specific layout first by using LayoutInflater and map the control in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.R.layout.fragment1, container, false);
TextView text = (TextView) fragmentView.findViewById(R.id.fragmentOneText1);
mTextNoResult=(TextView)fragmentView.findViewById(R.id.text_noresult);
return fragmentView;
}
I made a program that uses FragmentTabs for implementing ActionBar Tabs.
I was wondering how it is possible to add a widget to a inflated Fragments in Tabs. It's more complicated than normal because the fragment is returning only an onCreateView to the TabListener, so how would I add in functioning widgets? I don't need specific code, just the logic please!
Here is a sample Fragment being inflated when a Tab is clicked (inside of which I need widgets):
public class IdFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.id_layout, container, false);
}
}