0

I am trying to make a shopping cart android app and i want to show the cart with number of items. attaching the screenshot of app below .

When clicking on the cart icon its does not going any where.. :(.

here is my code.

MainActivity.java

public class PandaActivity extends ActionBarActivity {

TextView notifCount;
RelativeLayout count;
int mNotifCount = 0;

protected ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.topbar));
    actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backbutton));

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_panda, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.badge) {
        Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
        startActivity(cartIntent);
        return true;
    }
    return super.onOptionsItemSelected(item);


 }
}

menu_panda.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sha.netstager.pandafoods.activity.PandaActivity">
<item
    android:title="demo"
    android:id="@+id/badge"
    app:actionLayout="@layout/feed_update_count"
    app:showAsAction="always">
</item>
</menu>

feed_update_count.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">

<!-- Menu Item Image -->
<ImageView
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:src="@drawable/cart" />

<!-- Badge Count -->
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="18dp"
    android:layout_marginRight="4dp"
    android:layout_height="22dp"
    android:layout_alignParentRight="true"
    android:text="99"
    android:textSize="16dp"
    android:background="#f7134e"
    android:textColor="#fff"/>

 </RelativeLayout>

I tried many ways to solve this problem with onclick methord and some others. but dont know what is the error is happening here.

There is no messages or warnings in logcat when I click on the menu item in action bar.

Many Thanks..

2 Answers 2

3

Try to add onclick in your onCreateOptionsMenu, instead of current solution as given below.

@Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu, menu);
                RelativeLayout rl_viewBag = (RelativeLayout) menu.findItem(R.id.badge).getActionView();

                txt_bagCount = (TextView)rl_viewBag.findViewById(R.id.actionbar_notifcation_textview);
                txt_bagCount.setText("10");

                rl_viewBag.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
                        startActivity(cartIntent);
                    }
                });
                return true;
            }   
0

for better look of actionbar use extends AppCompatActivity this is the same look and feel will be provided which is been used by gmail.

Not the answer you're looking for? Browse other questions tagged or ask your own question.