I m using navigation drawer and want to put a launcher icon to the right of navigation drawer icon and put a title on right of launcher icon in ActionBar. I m trying this but didn't get the expected result.
Image here :
I m using
actionbar.setLogo(imageId); //to set the launchericon
actionbar.setTitle("home"); //to set the title
but the problem is, didn't able to set the launcher icon position in ActionBar.
please help me out.
Thanks in advance.
Use below code to set logo and title on your ActionBar :
RESULT / OUTPUT :
CODE:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setTitle("Home");
FULL CODE :
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Here is the code
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setTitle("Home");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
I hope you mean to say that your toolbar logo is not getting displayed for a custom toolbar. A possible workaround for that would be to an <ImageView> to your custom toolbar itself and set the image.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/my_custom_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_custom"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Title"
android:id="#+id/custom_title"
android:layout_toRightOf="#id/menu_icon"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Note that you could also add the android:centerHorizontal="true" to the textview if you want the title to be in center. Next declare the toolbar and call the textview in the required file by the id
TextView customTitle = (TextView) findViewById(R.id.custom_title);
customTitle.setText("My Custom Title");
Instead of using the getSupportActionBar().setLogo(R.mipmap.ic_launcher); we have defined it in the xml. Now use this code in the java.
ImageView cumstomIcon = (ImageView) findViewById(R.id.my_custom_icon);
customIcon.setImageResource(R.drawable.some_image);
You can also directly set the image in the xml but if you need to keep diff. images for various activities, then set the image programmatically..
Any doubts drop a comment!
Related
I am using the new BottomAppBar in an app.
But I can't make the badge to work.
I tried using the same logic used on BottomNavigationView, but it is not working.(In another app I have a BottomNavigationView inside a BottomAppBar where the badge works, but couldn't use the same code in the BottomAppBar app(Navigation Icon).
MenuView.ItemView itemView = (MenuView.ItemView) bottomAppBar.getChildAt(posicao);
notificationBadge = LayoutInflater.from(this).inflate(R.layout.view_notification_badge, (ViewGroup) itemView, false);
TextView notific = notificationBadge.findViewById(R.id.badge);
notific.setText(notificacao);
itemView.addView(notificationBadge);
What I want to do:
I tried the BadgeNavigationDrawable class. But couldn't find the right reference to the navigation icon in the BottomAppBar.
Thanks.
you can have a try like this:
in the xml ,set "app:navigationIcon"(the left icon in the bottomAppBar):
<android.support.design.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_gravity="bottom"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:fabAttached="true"
app:backgroundTint="#color/colorPrimary"
app:navigationIcon="#android:drawable/ic_dialog_email"
app:fabCradleVerticalOffset="12dp"/>
then in the activity:
BottomAppBar bottomAppBar = (BottomAppBar) findViewById(R.id.bottom_appbar);
bottomAppBar.setNavigationOnClickListener(this);
#Override
public void onClick(View v) {
//do something
}
I have an toolbar with overflow menu icon with menu items. I need to change the overflow icon to the circular imageview.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/white"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="38dp"
android:layout_height="30dp"
android:layout_gravity="right|end"
app:srcCompat="#android:drawable/sym_def_app_icon"/>
</android.support.v7.widget.Toolbar>
And the code looks like.
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
CircleImageView mciv = (CircleImageView) findViewById(R.id.profile_image);
setSupportActionBar(toolbarTop);
I have tried to change the overflow icon but most of SO mentioned about of using drawable. But i need to use the imageview. I have tried with the below
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setActionView(mciv);
return super.onOptionsItemSelected(item);
}
But its not working. How to change overflow menu icon with imageview?
You can set click event of imageview and on click open menu.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
or you can manually create Popup menu, here is the example for popup menu
I want to align the action bar title to centre without the help of custom view . I would appreciate any help.
Without using the custom view, modifying only default action bar title
You can align the title to the center when you use ActionBar, but you can use Toolbar to do this.
Toolbar is more useful and easier than ActionBar, you can use this layout to define the center title TextView for you activity:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/app_name" />
</android.support.v7.widget.Toolbar>
And use this code for a back button:
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.single_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
You also need to override onCreateOptionsMenu method for the menu, and you can refer to this project : chrisbanes/cheesesquare.
Ok, You can try this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:text="#string/app_name" />
</android.support.v7.widget.Toolbar>
And remember to add this line in your activity's java code:
getSupportActionBar.setTitle("");
It seems there is no way to do this without custom view. You can get the title view:
View decor = getWindow().getDecorView();
TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
But changing of gravity or layout_gravity doesn't have an effect. The problem in the ActionBarView, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:
ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
View v = actionBar.getChildAt(0);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity= Gravity.CENTER;
v.setLayoutParams(p);
v.setBackgroundColor(Color.BLACK);
The contextual action mode, when overlayed on top of an android.support.v7.widget.Toolbar, does not appear to filter touches over its entire width, but lets touches "fall through" to (invisible) widgets on the Toolbar.
My Toolbar contains a custom widget (one of the declared benefits of Toolbar). I have the contextual action mode styled to overlay the toolbar, and it hides it entirely. However, when I touch where the (now obscured) custom widget was located, the touch passes through to it. I wonder if this is a bug/oversight, but I may not have grasped this right, or my expectations may be lofty. I assume that the touch would not pass through if there was something on the contextual action overlay in the location of the obscured custom component.
I am testing this on Kitkat.
Here is some sample code:
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar_toolbar);
setSupportActionBar(toolbar);
TextView barTextView = (TextView) toolbar.findViewById(R.id.textview_bar);
barTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Bar TextView clicked", Toast.LENGTH_SHORT).show();
}
});
mActionModeCallback = new ActionModeCallback();
TextView mainTextView = (TextView) findViewById(R.id.textview_main);
mainTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Main TextView clicked", Toast.LENGTH_SHORT).show();
if (mActionMode != null) {
return;
}
getSupportActionBar().startActionMode(mActionModeCallback);
}
});
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/actionbar_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:id="#+id/textview_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Bar"
android:textColor="#FF0000"/>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/textview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SomeTextToClick"/>
</LinearLayout>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionModeOverlay">true</item>
</style>
</resources>
I also tried setting up the action mode with the following alternative, but the behavior was the same:
Toolbar toolbar = (Toolbar) MainActivity.this.findViewById(R.id.actionbar_toolbar);
mActionMode = toolbar.startActionMode(mActionModeCallback);
At this point I can only think of either explicitly disabling the custom component whenever the contextual action mode comes on, or somehow filling the contextual action bar with invisible junk to more seriously obscure the toolbar.
Some screenshots below.
Before clicking anything:
After clicking "SomeTextToClick" - contextual action mode is now on, and the text in the toolbar is obscured, but at this point I can click in the center of the contextual action bar, and the click gets handled by the listener on the "Bar" TextView in the toolbar:
Can anyone help ?
I am trying to implement an Action Bar on an Android Activity which implements Jeremy Feinstein's sliding menu and actionbarSherlock. The issue I am facing is to centre align the title text in the Action Bar. My action bar has a menu icon named "icon_sliding_menu" on the left of the text to activate the left menu sliding action.
I have used this example to write attached below. However, when I add the line "getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);" to try to customize my view, my title just goes away, and I don't see any text at all.
ActionBar when the line mentioned above is not in the code:
ActionBar when the line mentioned above is written in the code:
How can I align the text to centre? I am attaching my code below.
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp" />
</LinearLayout>
HomeActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final View actionBarLayout = LayoutInflater.from(this).inflate(
R.layout.actionbar, null);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(actionBarLayout);
getSupportActionBar().setTitle("Home");
getSupportActionBar().setHomeButtonEnabled(true);
initSlidingMenu();
}
private void initSlidingMenu() {
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setAboveOffset(50);
slidingMenu.setBehindOffset(150);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setOnOpenListener(new OnOpenListener() {
#Override
public void onOpen() {
getSupportActionBar().setTitle("Menu");
}
});
slidingMenu.setOnCloseListener(new OnCloseListener() {
#Override
public void onClose() {
getSupportActionBar().setTitle("Home");
}
});
slidingMenu.setSecondaryOnOpenListner(new OnOpenListener() {
#Override
public void onOpen() {
getSupportActionBar().setTitle("Current Trip");
}
});
// Add left menu
mTransaction = this.getSupportFragmentManager().beginTransaction();
slidingMenu.setMenu(R.layout.left_menu);
mTransaction.commit();
// Add right menu
mTransaction = this.getSupportFragmentManager().beginTransaction();
slidingMenu.setSecondaryMenu(R.layout.right_menu);
mTransaction.commit();
}
#Override
protected void onResume() {
super.onResume();
getSupportActionBar().setIcon(R.drawable.icon_sliding_menu);
}
You can do it using titleTextStyle attribute of ActionBar.
In your styles.xml, in the App theme, set actionBarTabSyle to a custom style as,
<item name="titleTextStyle">#style/TitleTextStyle</item>
And heres the CustomActionBarTabs,
<!-- action bar tab styles -->
<style name="TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<item name="android:gravity">center</item> //center the title
</style>
Using this you can set various styles to the Title Text like color etc. Remember this will only center the text for the space provided to the Title within the ActionBar. In case this doen't help much you can always create your own custom view and add it to ActionBar as,
getSupportActionBar().setCustomView(R.layout.action_bar);
In case you want to create your own layout, follow these 2 simple steps:
Java Code
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
Where R.layout.actionbar is the following XML.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
Source.