Implement Actionbar in Android Honeycomb - android

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

Related

How can I change the ActionBar color of individual fragments in Android?

Is there any way to change the ActionBar color of fragments and change the title displayed in the ActionBar to the title of the menu option that was pressed?
If you are using AppCompat you always have to call getSupportActionBar()
Change Title :
getSupportActionBar().setTitle("Your Title");
Change Color :
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
If in Fragment :
Change Title :
getActivity().getActionBar().setTitle("YOUR TITLE");
Change Color :
getActivity().getAcationBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
Hope this help you
This is the suggested answer written in java :
getActionBar().setTitle("Test");
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
If you're using support libraries:
android.support.v7.app.ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
supportActionBar.setBackgroundDrawable(view.getBackground());
}
Otherwise:
android.app.ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
actionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
actionBar.setBackgroundDrawable(view.getBackground());
}

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

Android: Setting Custom Title/Action Bar - not working

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

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)

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!

Categories

Resources