How to change action bar and action bar icons in fragments [duplicate]
This question already has an answer here:
Access the ActionBar from within a Fragment
2 answers
I am creating an with app fragment,each fragment have different styles of action bar and icon how can we achieve on this fragments.
I have created action bar at MainActivity class but this action bar will display on all fragments.
I except the action bar change style and icons on each fragments
my app main activity will look like this. it have app logo at the centre and navigation drawer in some fragments the app logo can be changed to app page name
[]
The second image is my fragments so the action bar navigation drawer changed to back button and the action bar icons also

marked as duplicate by ADM, Community♦ Jan 2 at 4:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Access the ActionBar from within a Fragment
2 answers
I am creating an with app fragment,each fragment have different styles of action bar and icon how can we achieve on this fragments.
I have created action bar at MainActivity class but this action bar will display on all fragments.
I except the action bar change style and icons on each fragments
my app main activity will look like this. it have app logo at the centre and navigation drawer in some fragments the app logo can be changed to app page name
[]
The second image is my fragments so the action bar navigation drawer changed to back button and the action bar icons also

marked as duplicate by ADM, Community♦ Jan 2 at 4:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are you implementing a customactionbar
?
– Mosius
Jan 1 at 5:48
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38
add a comment |
This question already has an answer here:
Access the ActionBar from within a Fragment
2 answers
I am creating an with app fragment,each fragment have different styles of action bar and icon how can we achieve on this fragments.
I have created action bar at MainActivity class but this action bar will display on all fragments.
I except the action bar change style and icons on each fragments
my app main activity will look like this. it have app logo at the centre and navigation drawer in some fragments the app logo can be changed to app page name
[]
The second image is my fragments so the action bar navigation drawer changed to back button and the action bar icons also

This question already has an answer here:
Access the ActionBar from within a Fragment
2 answers
I am creating an with app fragment,each fragment have different styles of action bar and icon how can we achieve on this fragments.
I have created action bar at MainActivity class but this action bar will display on all fragments.
I except the action bar change style and icons on each fragments
my app main activity will look like this. it have app logo at the centre and navigation drawer in some fragments the app logo can be changed to app page name
[]
The second image is my fragments so the action bar navigation drawer changed to back button and the action bar icons also
This question already has an answer here:
Access the ActionBar from within a Fragment
2 answers


edited Jan 1 at 5:38


Ratilal Chopda
3,45241126
3,45241126
asked Jan 1 at 5:23


Rojan RoyRojan Roy
1
1
marked as duplicate by ADM, Community♦ Jan 2 at 4:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ADM, Community♦ Jan 2 at 4:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are you implementing a customactionbar
?
– Mosius
Jan 1 at 5:48
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38
add a comment |
Are you implementing a customactionbar
?
– Mosius
Jan 1 at 5:48
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38
Are you implementing a custom
actionbar
?– Mosius
Jan 1 at 5:48
Are you implementing a custom
actionbar
?– Mosius
Jan 1 at 5:48
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38
add a comment |
2 Answers
2
active
oldest
votes
The best way to handle an action bar that you have to set only one action bar in Main activity.
From fragment, you can access the main activity and you can change the action bar from the fragment as per your fragment. Like below :
((MainActivity)getActivity()).ChangeActionbar();
You can create various methods in Main activity to change action bar as per your use. SO you have to just call those methods from fragment like ChangeActionbar()
.
But for that, you have to take care about the back press event because if you going back at that time you have to set your previous fragment action bar so the best way to handle that you have to add the code to change action bar in onResume()
method. Because onResume()
method call when the fragment is opened.
Hope! this will help you.
Happy coding...
add a comment |
to change title and logo write method in main activity like below:
public void fontToTitleBar(String title,int logo) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/av_Medium.ttf");
title = "<font color='#ffffff'>" + title + "</font>";
SpannableString s = new SpannableString(title);
s.setSpan(font, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
toolbar.setTitle(Html.fromHtml(String.valueOf(s), Html.FROM_HTML_MODE_LEGACY));
} else {
toolbar.setTitle((Html.fromHtml(String.valueOf(s))));
}
toolbar.setLogo(logo);
}
then you can call this method from your fragment by getActivity()
to change title
like:((MainActivity) getActivity()).fontToTitleBar("title",r.drawable.icon);
for change menu in fragment Override onCreateOptionsMenu()
and inflat new menu and set setHasOptionsMenu(true);
in onCreate()
method of fragment.
also you can handle click menu in fragment with onOptionItemSelected
method .
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The best way to handle an action bar that you have to set only one action bar in Main activity.
From fragment, you can access the main activity and you can change the action bar from the fragment as per your fragment. Like below :
((MainActivity)getActivity()).ChangeActionbar();
You can create various methods in Main activity to change action bar as per your use. SO you have to just call those methods from fragment like ChangeActionbar()
.
But for that, you have to take care about the back press event because if you going back at that time you have to set your previous fragment action bar so the best way to handle that you have to add the code to change action bar in onResume()
method. Because onResume()
method call when the fragment is opened.
Hope! this will help you.
Happy coding...
add a comment |
The best way to handle an action bar that you have to set only one action bar in Main activity.
From fragment, you can access the main activity and you can change the action bar from the fragment as per your fragment. Like below :
((MainActivity)getActivity()).ChangeActionbar();
You can create various methods in Main activity to change action bar as per your use. SO you have to just call those methods from fragment like ChangeActionbar()
.
But for that, you have to take care about the back press event because if you going back at that time you have to set your previous fragment action bar so the best way to handle that you have to add the code to change action bar in onResume()
method. Because onResume()
method call when the fragment is opened.
Hope! this will help you.
Happy coding...
add a comment |
The best way to handle an action bar that you have to set only one action bar in Main activity.
From fragment, you can access the main activity and you can change the action bar from the fragment as per your fragment. Like below :
((MainActivity)getActivity()).ChangeActionbar();
You can create various methods in Main activity to change action bar as per your use. SO you have to just call those methods from fragment like ChangeActionbar()
.
But for that, you have to take care about the back press event because if you going back at that time you have to set your previous fragment action bar so the best way to handle that you have to add the code to change action bar in onResume()
method. Because onResume()
method call when the fragment is opened.
Hope! this will help you.
Happy coding...
The best way to handle an action bar that you have to set only one action bar in Main activity.
From fragment, you can access the main activity and you can change the action bar from the fragment as per your fragment. Like below :
((MainActivity)getActivity()).ChangeActionbar();
You can create various methods in Main activity to change action bar as per your use. SO you have to just call those methods from fragment like ChangeActionbar()
.
But for that, you have to take care about the back press event because if you going back at that time you have to set your previous fragment action bar so the best way to handle that you have to add the code to change action bar in onResume()
method. Because onResume()
method call when the fragment is opened.
Hope! this will help you.
Happy coding...
answered Jan 1 at 7:48
Pratik SataniPratik Satani
301111
301111
add a comment |
add a comment |
to change title and logo write method in main activity like below:
public void fontToTitleBar(String title,int logo) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/av_Medium.ttf");
title = "<font color='#ffffff'>" + title + "</font>";
SpannableString s = new SpannableString(title);
s.setSpan(font, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
toolbar.setTitle(Html.fromHtml(String.valueOf(s), Html.FROM_HTML_MODE_LEGACY));
} else {
toolbar.setTitle((Html.fromHtml(String.valueOf(s))));
}
toolbar.setLogo(logo);
}
then you can call this method from your fragment by getActivity()
to change title
like:((MainActivity) getActivity()).fontToTitleBar("title",r.drawable.icon);
for change menu in fragment Override onCreateOptionsMenu()
and inflat new menu and set setHasOptionsMenu(true);
in onCreate()
method of fragment.
also you can handle click menu in fragment with onOptionItemSelected
method .
add a comment |
to change title and logo write method in main activity like below:
public void fontToTitleBar(String title,int logo) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/av_Medium.ttf");
title = "<font color='#ffffff'>" + title + "</font>";
SpannableString s = new SpannableString(title);
s.setSpan(font, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
toolbar.setTitle(Html.fromHtml(String.valueOf(s), Html.FROM_HTML_MODE_LEGACY));
} else {
toolbar.setTitle((Html.fromHtml(String.valueOf(s))));
}
toolbar.setLogo(logo);
}
then you can call this method from your fragment by getActivity()
to change title
like:((MainActivity) getActivity()).fontToTitleBar("title",r.drawable.icon);
for change menu in fragment Override onCreateOptionsMenu()
and inflat new menu and set setHasOptionsMenu(true);
in onCreate()
method of fragment.
also you can handle click menu in fragment with onOptionItemSelected
method .
add a comment |
to change title and logo write method in main activity like below:
public void fontToTitleBar(String title,int logo) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/av_Medium.ttf");
title = "<font color='#ffffff'>" + title + "</font>";
SpannableString s = new SpannableString(title);
s.setSpan(font, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
toolbar.setTitle(Html.fromHtml(String.valueOf(s), Html.FROM_HTML_MODE_LEGACY));
} else {
toolbar.setTitle((Html.fromHtml(String.valueOf(s))));
}
toolbar.setLogo(logo);
}
then you can call this method from your fragment by getActivity()
to change title
like:((MainActivity) getActivity()).fontToTitleBar("title",r.drawable.icon);
for change menu in fragment Override onCreateOptionsMenu()
and inflat new menu and set setHasOptionsMenu(true);
in onCreate()
method of fragment.
also you can handle click menu in fragment with onOptionItemSelected
method .
to change title and logo write method in main activity like below:
public void fontToTitleBar(String title,int logo) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/av_Medium.ttf");
title = "<font color='#ffffff'>" + title + "</font>";
SpannableString s = new SpannableString(title);
s.setSpan(font, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
toolbar.setTitle(Html.fromHtml(String.valueOf(s), Html.FROM_HTML_MODE_LEGACY));
} else {
toolbar.setTitle((Html.fromHtml(String.valueOf(s))));
}
toolbar.setLogo(logo);
}
then you can call this method from your fragment by getActivity()
to change title
like:((MainActivity) getActivity()).fontToTitleBar("title",r.drawable.icon);
for change menu in fragment Override onCreateOptionsMenu()
and inflat new menu and set setHasOptionsMenu(true);
in onCreate()
method of fragment.
also you can handle click menu in fragment with onOptionItemSelected
method .
answered Jan 1 at 6:01


amin mahmoudiamin mahmoudi
323213
323213
add a comment |
add a comment |
Are you implementing a custom
actionbar
?– Mosius
Jan 1 at 5:48
What did you do until now to achieve this? Could you please post the code?
– Lino
Jan 1 at 7:38