In my activity, I have 5 different fragments. I want to change the title for only one Fragment other Fragment's title will be same. I've used following code, but it changes title for all Fragments. How can I change title for only a specific Fragment?
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Create Post")
I solved this problem by creating class named StatefulFragment (extends Fragment) and creating String fragmentTitle inside this class which contains your title (like this)
public class StatefulFragment extends Fragment {
public String fragmentTitle = "";
}
Use this class for initialize all your fragments (extend them from StatefulFragment).
Then I recommend to hide ActionBar from your activity and add Toolbar to your all fragment's views (and initialize it in fragment's class). And then just set fragment's title which we added to fragment's toolbar:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState){
Toolbar myToolbar = view.findViewById(R.id.myToolbar);
myToolbar.setTitle(fragmentTitle);
super.onViewCreated(view, savedInstanceState);
}
Hope it helps!
Related
How to Show activity element or view in it's fragment and also access from fragment without move from fragment to activity and also tell vice-versa of this scenario.
Here is My Problem:-
I am using Search Edit Box in Activity and I want to use this Search Box into my fragment but i don't want make any search box in fragment, I just want to show this Activity Search Box in Fragment When Ever User type in search box then by using textWatcher track the text and show the result of search box in fragment not in Activity.
Below is the image of UI that i want make it.
You can access activity elements and methods from fragment this way:
Activity template
public class ActivityMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content)
}
public void updateView(){
// update your views & vars here
}
}
Fragment template
public class BlankFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
public void updateActivityView(){
if (getActivity() != null){
((ActivityMain) getActivity()).updateView();
}
}
For vice versa you can access fragment items from activity this way:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.specific_function_name();
Where R.id.example_fragment is most likely the FrameLayout id inside your xml layout
In my project, I want to set visibility of fragments buttons from MainActivity. But the problem is, it gives NullPointerException(). I also maked listBtn & gridBtn as static. I used below code :
FirstFragment fragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id. <frameLayout Id>);
main_page_fragment.listBtn.setVisibility(View.GONE);
main_page_fragment.gridBtn.setVisibility(View.GONE);
You cannot access to your fragment view from Activity class because activity uses its own view (ex: R.layout.activity_main). Rather you can set visibility in your corresponding fragment class which will do the same job.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
Button listBtn = (Button)view.findviewById(R.id.listBrn);
Button gridBtn = (Button)view.findviewById(R.id.gridBrn);
listBtn.setVisibility(View.GONE);
gridBtn.setVisibility(View.GONE);
return view;
}
Fragment onCreateView callback is called after onCreate method of activity, so i think you have tried to get access from it. That views will be accessible only after onResumeFragments callback is called, you should perform your actions with fragments there.
Another tip is that you strongly should not call views of fragments directly like you did or via static reference to views that's the worst. You should avoid such dependencies on fragments inner implementation. Instead of it, better is create some method like setInitialState (the name depends on your business logic) and just call it from activity.
So result code:
In activity:
private FirstFragment fragment;
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init fragment here
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
fragment.setInitialState();
}
In fragment:
//this will be called on fragment #onResume step, so views will be ready here.
public void setInitialState() {
listBtn.setVisibility(View.GONE);
gridBtn.setVisibility(View.GONE);
}
If you add your fragments dynamically from MainActivity like so:
YourFragment fragment = new YourFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, fragment, YOUR_TAG)
.commit();
Then you can define method in your fragment like so:
public void hideButtons()
{
yourBtn.setVisibility(View.GONE);
}
And call it from activity:
fragment.hideButtons();
I struggle with this for several hours and I found a much simpler solution.
Inside the fragment, simply make a public function (outside the on create view method) with the behavior that you want.
fun hideElement() {
binding.button.visibility = View.GONE
}
And then in main activity access to the fragment and call the function.
binding.bottomNavigation.setOnNavigationItemSelectedListener {
when (it.itemId){
R.id.someFragment -> someFragment.hideElement()
}
}
I have two fragments, fragment_one (fragmentOne.class) and fragment_two (fragmentTwo.class). These fragments are being displayed inside ActivityMain.class.
When fragment_one is being displayed I want to set Image A as the background for ActivityMain.class.
When fragment_two is being displayed I want to set Image B as the background for ActivityMain.class
I can swap and set the background of ActivityMain.class when I am not using fragments (i use buttons as a test)....But, when I modify this for fragments I cannot get it to work.
Listed here you will see my ActivityMain.class
public class ActivityMain extends FragmentActivity {
//set variables for use of pageradapter and swipe screen
MyPageAdapter adapter;
ViewPager pager;
Context context = this;
LinearLayout swipeHomeScreen;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_activity_nav);
//Implement the adapater for the swipe screen on launch page and circle indicator
adapter = new MyPageAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
swipeHomeScreen = (LinearLayout) findViewById(R.id.swipeHomeScreen);
}
}
Here I have listed the code of one of my fragments where I am trying to share/pass the set the background image of ActivityMain.class...
public class ActivityNavSwipeTwo extends Fragment {
ActivityMain main;
public void onAttach(Activity activity) {
main = (ActivityMain) activity;
};
public static ActivityNavSwipeTwo newInstance() {
ActivityNavSwipeTwo fragment = new ActivityNavSwipeTwo();
return fragment;
}
public ActivityNavSwipeTwo() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_two, null);
main.swipeHomeScreen.setBackgroundResource(R.drawable.Image2);
return root;
}
}
My activity inflates the fragments fine, but the background image does not change.
How could I implement this for target api 23??
Thanks for your support.
Implement viewpager.onpagechangedlistener in your activity. Then add view.setonpageselecterlistener(this) in oncreated method. You can control selected fragment in implemented on pageselected method
Definitely you have any parent layout(Relative/Linear) for your activity. So you have to do following:
Create id of parent layout if not created.
access from fragment and change the background color of that parent layout.
Write this in Fragment's onCreateView() method.
relativeLayout = (RelativeLayout)getActivity().findViewById(R.id.relativelayout);
relativeLayout.setBackground(background);
I have an application with two fragments when the application starts the Actionbar title is the one of the first fragment. But when I'm going to the second Fragment the title doesn't change.
So how do I change the ActionBar title?
This is my code:
public class ClientList extends Fragment {
#Override
public void onResume() {
super.onResume();
String title;
title = getResources().getString(R.string.client_title);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title);
}
#Override
public void onDestroyView() {
super.onDestroyView();
String title2;
title2 = getResources().getString(R.string.action_settings);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title2);
}
}
so if you say you cannot reach the method from onCreateView, so this is your solution I think:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment,
container, false);
getActivity().setTitle("Title");
return view;
}
Because if you return before you call setTitle() you cannot reach it, that's true.
Hope it helps.
You can set your action bar title inside onCreate(if activity)/onCreateView(if fragment) method as getSupportActionBar().setTitle("Your Title");
Whenever you switch from one fragment to another fragment you can set the title in ActionBar. This can be done on onCreateView(...) of a fragment. Advisable try to change the name in Activity in which you are trying to load the fragment.
To do so ....
If you are using appcompat library
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(" your title ");
If you are not using appcompat library
ActionBar actionBar = getActionBar();
actionBar.setTitle(" your title ");
This should work:
getActivity().setTitle(YOUR_TITLE);
Call it from your Fragment when you wanted to set.
I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();