-1

Well,I have a counter value got it from another activity.Now in current activity,with a help of a button,I want to add that counter value on the toolbar(just like any cart:where one item added scenario).Below my receiving intent:

public class ShoppingCart extends AppCompatActivity {
private Button btn4_s_add,btn5_s_rem,btn6_s_add_to_cart;
private int c_number;
private TextView tv5;
public int counter_s=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shopingcart);

    tv5=(TextView)findViewById(R.id.textView5);
    btn4_s_add=(Button) findViewById(R.id.button4);
    btn5_s_rem=(Button) findViewById(R.id.button5);
    btn6_s_add_to_cart=(Button)findViewById(R.id.button6);


    c_number=getIntent().getIntExtra("counter",0);
    tv5.setText(Integer.toString(c_number));


    btn4_s_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            counter_s=c_number;
            counter_s++;

        }
    });
    btn5_s_rem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter_s=c_number;
            counter_s--;

        }
    });

    btn6_s_add_to_cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter_s=c_number;
            if(counter_s==0){
                Toast.makeText(ShoppingCart.this, "Your cart is empty", Toast.LENGTH_SHORT).show();
            }
            else{

//Now here I want to add the value of counter on the toolbar.Obviously,how do I add badge or other.This is like a checkout button,once you hit,you counter gets visible in toolbar.

            }
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

}

hemen
  • 1,460
  • 2
  • 16
  • 35

2 Answers2

0

you should customise the Toolbar. As it is a View. so you can do it easily. you can design a layout in the toolbar and place TextView in the corner.

<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/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/toolbar_counter" /> 


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

Now you can access this textViews like

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
TextView counter = (TextView) toolbarTop.findViewById(R.id.toolbar_counter);
Devendra Singh
  • 2,343
  • 4
  • 26
  • 47
0

Just make your toolbar layout include the tv5 TextView of yours. In your activity instantiate it:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

And get the reference on it, e.g: tv5 = (TextView) toolbar.findViewById(R.id.tv5);

Then display the counter depending on the button clickplay. Place the following in the onClickListener targetting your button:

tv5.setText(Integer.toString(c_number));
tv5.setVisibility(View.VISIBLE);

In before, you gotta set your TextView invisible i.e:
tv5.setVisibility(View.GONE);

Amesys
  • 816
  • 2
  • 10
  • 19
  • on toolbar ,like a badge – hemen May 20 '16 at 11:24
  • I edited my code. By the way i just found the following topic about a button inside a Toolbar, it may give you some last hints: http://stackoverflow.com/questions/31231609/creating-a-button-in-android-toolbar – Amesys May 20 '16 at 11:36
  • button is not inside toolbar,it is in activity – hemen May 20 '16 at 11:38
  • Whatever, just imagine they're talking about a TextView, it remains a basic layout element. What bothers you still? – Amesys May 20 '16 at 11:39