Navigation drawer icon gets disappeared on setting CustomView in actionbar - android

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!

Related

Add and Remove views from Toolbar depending on Fragment Displayed

I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();

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)

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