Showing Fragment title in custom Actionbar - android

i want to use custom actionbar with fragment title.but this method only display application name for all fragments.i need to use appropriate fragment name in actionbar.
getActivity().getActionBar().setDisplayShowCustomEnabled(true);
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(getActivity());
View v = inflator.inflate(R.layout.title, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(getActivity().getTitle());
//assign the view to the actionbar
getActivity().getActionBar().setCustomView(v);

You just need to define a method which will update the title of action bar and call that method from ever activity / Fragment's onResume()
In activity :
#Override
public void onResume() {
// For activity
setActionbarTitle("title");
// For Fragment
((ParentActivtyNAme)getActivty).setActionbarTitle("title");
}
setActionbarTitle Method :
public void setActionbarTitle(String title) {
textTitle.setText(title);
}
And initialize textTitle when you set Custom View in Actionbar :
textTitle= ((TextView)v.findViewById(R.id.title));
Hope it helps you ツ

Related

How to get the Title of ActionBar in non-activity class in android

public void onClick(View v) {
//referenceOfActivity
// here is the context, don't know how to use it to get ActionBar title
Toast.makeText(referenceOfActivity,"Already Liked",Toast.LENGTH_SHORT).show();
}
I want to get the title of action bar within an adapter class. As you can see I have the Context of Activity.
Cast your activity name to your context.
Example;
((MainActivity) referenceOfActivity).getSupportActionBar().getTitle()

How to change header according to navigation drawer

I am using navigation drawer in my application, with fragments. There are icons and text in each row(one of them is products, on clicking these a fragment is open with a list view.After clicking on the list view row i have an activity in this i want header name same as i have selected(products).but bydefault it is showing name off the app which is sales.please help.
pass product name in Intent in your list's onItemClickListner
Intent intent = new Intent(getActivity(),YOURACTIVITY.class);
intent.putExtra("product_name",YOUR PRODUCT_NAME);
getActivity().startActivity(intent);
and in your target activity (your product detail activity) put below code in onCreate() method.
getSupportActionBar().setTitle("YOUR DESIRED TITLE");
If using Frament , you can have a method in parent hosting Activity like
this
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
and invoke this method from your individual fragment like this and set the title
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//usual statements
((HomeActivity)getActivity()).setActionBarTitle("Child fragment name");
}
You can set the actionbar header with the following. You can try this.
ActionBar actionBar = getActionBar();
actionBar.setTitle("My Title");

How access ActionBar from fragment on Android

On my application I have only one activity (that extends ActionBarActivity ) and various Fragments (that extends Fragment).
When the user click on a menu option, the application change the Fragment.
At this moment I want to change de title and the background color of the ActionBar.
When I try ActionBar actionBar = getActivity().getActionBar(); I got a null exception.
I'm using support library, and on the Activity I'm using android.support.v7.app.ActionBar actionBar = getSupportActionBar(); successfully.
On the Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.material_cadastro, container, false);
viewHolder = new MaterialViewHolder(view);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle(R.string.inserir_material);
return view;
}
Debuging I got that the Activity is returning OK, but the ActionBar is null:
getActivity() = {br.com.americocarelli.vendasfacil.ui.MenuPrincipal#3b56766f}
getActivity().getActionBar() = null
Override your onAttach(Activity), then cast your Activity to ActionBarActivity, the you can get an ActionBar.
Like:
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
ActionBar actionBar=((ActionBarActivity)activity).getSupportActionBar();
}
Since you use the support library (ActionBarActivity), then use ((ActionBarActivity)getActivity()).getSupportActionBar(); to get access to the support action bar.
You are probably building it with build tools >=21, that versions causes activity.getActionBar() returns null, you should use getSupportActionBar() instead.
EDIT: if you get NPE at exactly this line: getActivity(). ... then you are probably referencing it before or after fragment is attached to activity. Could you please paste the context (place, in what function) you are using it?

How change Actionbar title by switching between Fragments

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.

how to reuse custom actionbar to all the activities in an application?

I have created a custom action bar which works fine on my starting activity but gives an error when I call the method from other activities in the same application.
This is the code I am using to set the ActionBar in my first activity
firstAct.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBarSetup(this);
}
void actionBarSetup(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
LayoutInflater inflator = LayoutInflater.from(context);
View v = inflator.inflate(R.layout.actionbar_layout, null);
//assign the view to the actionbar
ab.setCustomView(v);
}
}
**secondAct.java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pg);
(new firstAct()).actionBarSetup(secondAct.this);
}
I get NullPointerException when i call actionBarSetup() from secondAct.java
on line ActionBar ab = getActionBar().
Is it that getActionBar() cannot be called directly from other activities besides the main activity ie. firstAct.
How to call it from other Activities then?
You don't create new activities by calling their constructor. You have the system create and open them for you. I'm talking about the line
(new firstAct()).actionBarSetup(secondAct.this)
What are you trying to do here?
You probably want to make the actionBarSetup method accessible for all classes and not just instances of firstAct. Then declare it like this (maybe move it to a utility class?):
public static void actionBarSetup(Activity activity) {
ActionBar ab = activity.getActionBar(); // you need activity, not just context
// ...
}
Then call it from other classes like this:
firstAct.actionBarSetup(this);
Making a method static detaches it from an instances resources. You were taking in second activity (context parameter) but asking for an action bar from instance of the first activity (essentially this.getActionBar()) which was not setup by system (because you misused constructor).
Note: Please use PascalCase notation for class names (capital first letter).
EDIT
Warning: Your action bar may have different styling from your activity (e.g. black toolbar and white activity). In that case using the activity's inflater to inflate contents of the action bar will produce undesired results (inverted text color mainly). The following line is safer. But it's available no sooner than API 14.
LayoutInflater inflator = LayoutInflater.from(ab.getThemedContext());
You need create BaseActivity like
public class BaseActivity extends Activity {
public void actionBarSetup(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
LayoutInflater inflator = LayoutInflater.from(context);
View v = inflator.inflate(R.layout.actionbar_layout, null);
//assign the view to the actionbar
ab.setCustomView(v);
}
}
then you need firstAct and secondAct extend BaseActivity then in onCreate method call actionBarSetup()
This may help
private void showCustoNavBar(){
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.new_gradient));
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
View customNav = LayoutInflater.from(this).inflate(R.layout.actioncustomview, null);
getSupportActionBar().setCustomView(customNav);
}

Categories

Resources