4

I have created an activity with toolbar and recyclerview,Implemented selection option for recyclerview, After selection of list items i need to update selected items count in toolbar.I'm trying to achieve this by using

    @Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
    // Inflate a menu resource providing context menu items
    MenuInflater inflater = actionMode.getMenuInflater();
    inflater.inflate(R.menu.menu_cab_recyclerviewdemoactivity, menu);

    return true;
}

Now after selecting recyclerview items extra actionbar adding above the toolbar,Here is screen http://screencast.com/t/UY2KSs9r

sri
  • 73
  • 1
  • 11

2 Answers2

2

If you just want to update the count in the toolbar, you can just update the title,

final ActionBar ab = getSupportActionBar();
if(ab!=null)
    ab.setTitle(stringVariable);

I'm not sure what else you want to do, so for now here is my answer.

hehe
  • 1,294
  • 13
  • 26
2

First of all remove the default ActionBar

edit your style.xml

 <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme colors here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>

    <!-- Disables the Default ActionBar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Then in your main_activity_layout.xml, remove any padding on the root RelativeLayout tag.

Next

 <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/main_settings_btn"
            android:background="@drawable/ic_settings"
            android:backgroundTint="@color/accent"
            android:contentDescription="Copy"/>

        <ImageButton
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:background="@drawable/ic_points"
            android:backgroundTint="@color/accent"
            android:contentDescription="Paste"/>

    </android.support.v7.widget.Toolbar>
    ...... <your_code_here_possibly_recyclerview>

This solves your extra toolbar issue.

Next

private Toolbar toolbar;                              // Declaring the Toolbar Object


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.mainToolbar); // Attaching the layout to the toolbar object
    setSupportActionBar(toolbar);                   // Setting toolbar as the ActionBar with setSupportActionBar() call

   // Now whenever you want to update the title just change text in below 
   getSupportActionBar().setTitle("My title"); 

}
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • I can able to create activity with toolbar and recyclerview ,after recyclerview items selection need to change toolbar. – sri Dec 22 '15 at 07:52
  • you do know when the items get selected right? whenever you select an item you just add a value to an integer variable and then update your toolbar title by calling getSupportActionBar().setTitle(String.valueOf(selectedTotalInt); – Nishant Srivastava Dec 22 '15 at 07:54