Android: Setting Custom Title/Action Bar - not working - android

I am trying to set a custom Title Bar as follows (please note this activity extends from FragmentActivity if it matters):
...
import android.support.v4.app.FragmentActivity;
...
public class MyActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.app.ActionBar actionBar = this.getActionBar();
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.window_title, null);
TextView tv = (TextView) v.findViewById(R.id.titlex);
tv.setText("My Title");
actionBar.setCustomView(v);
setContentView(R.layout.myactivity);
}
...
}
But i do not see the Custom Title Bar at all, what is missing?

Set actionbar.setDisplayShowCustomEnabled(true); before actionBar.setCustomView(v);
EDIT
Here i past my code for Custom ActionBar creation
// Action Bar Customization
ActionBar ab =act.getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg));
ab.setBackgroundDrawable(colorDrawable);
ab.setDisplayShowTitleEnabled(false); // disables default title on
// actionbar.
ab.setDisplayShowCustomEnabled(true); // enables custom view.
ab.setDisplayShowHomeEnabled(false); // hides app icon.
ab.setTitle("");
// Inflating Layout
LayoutInflater inflater = (LayoutInflater) act.getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
txtUserName=(TextView)customActionBar.findViewById(R.id.txtUserName);
ab.setCustomView(customActionBar);

Related

Image in Action bar without AppCompatActivity and with android:Theme.Holo.Light theme

I want to place an image(ImageView) in top right corner of an Action bar. I don't want to use AppCompatActivity library and so my MainActivity extends Activity instead of AppCompatActivity.
I use theme: <style name="AppTheme" parent="android:Theme.Holo.Light">
I tried with this code but it doesn't work because I don't extend AppCompatActivity:
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_imageview, null);
v1 = inflator.inflate(R.layout.custom_imageview1, null);
actionBar.setCustomView(v);
EDIT: I run this code on a device that runs Android 4.4.2
Since you are using a simple Activity you have to change this line
actionBar = getSupportActionBar();
with (check the doc)
actionBar = getActionBar();
You can use Toolbar for this. It is best option for you.Toolbar is a new standard replacement for ActionBar, introduced in Android Lollipop. It supports whole bunch of customization as you wish along with no or less modification to ActionBar logic.
Or you can use code :`
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
//if you want..You can set action
}
});`

how to override default action bar with custom layout actionbar

I try to get rid of default action bar by load a custom action bar. But then, it become like this :
FYI : this project is including navigation drawer sidebar. I am not really sure if the gray block thingy come from navigation drawer.
try like this it may helpful
ActionBar actionBar = getActionBar();
actionBar.setTitle("Project1");
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.custom_actionbar, null);
actionBar.setCustomView(customActionBarView);
here custom_actionbar is the xml file to add items in actionbar(like text,images and all)

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);
}

Navigation drawer icon gets disappeared on setting CustomView in actionbar

Why navigation drawer icon gets disappeared on setting customView by
actionbar.setCustomView(R.layout.blah);
How can this be resolved?
OK, you don't post any code, so I have to assume stuff here. So if you can update your question and show actual code!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Depending on what your custom Action Bar will do, you might want to disable these!
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
// Here is how you can get specific items from your custom view!
TextView titleTextView = (TextView) customView.findViewById(R.id.title_text);
titleTextView.setText("My Own Title");
...
// MAKE SURE THESE ARE SET!! BOTH OF THEM!!
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
The biggest issue is probably the code at the very end. Please make sure you have that code!

Implement Actionbar in Android Honeycomb

How to implement the ActionBar in android honeycomb? How to customize the action bar? Any help would be appreciated.
try this
ActionBar actionBar = getActionBar();
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout."id", null);
// lay.setLayoutParams(new ActionBar.LayoutParams(
// android.app.ActionBar.LayoutParams.MATCH_PARENT,
// android.app.ActionBar.LayoutParams.MATCH_PARENT));
//Not wrking
actionBar.setCustomView(view);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
You can also use the following resource to know more about implementing the action bar-
http://developer.android.com/guide/topics/ui/actionbar.html

Categories

Resources