0

I've created badge in MainActivity. It's working perfectly from activity. But in MainActivity, I've taken ViewPager and I need to increase badge count from fragment. Any idea how to achieve this?

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.cart_menu, menu);

    menuItem = menu.findItem(R.id.action_cart);
    menuItem.setIcon(buildCounterDrawable(count, R.drawable.ic_cart));

    return true;
}

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_cart) {
        Intent intent = new Intent(this, CartActivity.class);
        intent.putExtra("id", orderid);
        startActivityForResult(intent, PICK_REQUEST);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

This is the method for Badge count increment

   private Drawable buildCounterDrawable(int count, int backgroundImageId) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
    view.setBackgroundResource(backgroundImageId);

    if (count == 0) {
        View counterTextPanel = view.findViewById(R.id.counterValuePanel);
        counterTextPanel.setVisibility(View.GONE);
    } else {
        TextView textView = (TextView) view.findViewById(R.id.count);
        textView.setText(String.valueOf(count));
    }

    view.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return new BitmapDrawable(getResources(), bitmap);
}

public void doIncrease(int val) {
    count = val;
    invalidateOptionsMenu();
}

Menu On Click of plus button, I need to increase the count. Totally unaware how to achieve this

AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
  • 1
    What do you mean by "badge" and "badge count"? What does the user actually see on the screen? What will the user do and what happens in the app when the user does it? We can provide better help if you answer these questions and show us what your app is supposed to do. One way is to give mock screens in your question. – Code-Apprentice Jan 05 '17 at 05:03
  • So the badge is in your activity. What you want to do is change something in that badge from a User interaction from a fragment. Is that right...? – Akhil Soman Jan 05 '17 at 05:08
  • @Code-Apprentice **Badge** - it shows that something is there which user need to show and count shows that number of items – AMAN SINGH Jan 05 '17 at 05:09
  • @AMANSINGH Can you show some mock screen shots to illustrate how badges work in your app? – Code-Apprentice Jan 05 '17 at 05:10
  • @Code-Apprentice please go through the ques again, I updated the image – AMAN SINGH Jan 05 '17 at 05:16
  • @AkhilSoman please refer the ques again, I updated the image. On click on plus button i need to increase the count – AMAN SINGH Jan 05 '17 at 05:17
  • Which part of the screenshot is the "badge"? How will the user interact with it? Is it clickable? – Code-Apprentice Jan 05 '17 at 05:17
  • on click of plus button it should be increase. This cart icon is in activity where above code is written. but this I need to use the method **doIncrease()** in this fragment image – AMAN SINGH Jan 05 '17 at 05:20
  • `OptionMenu` is created in MainActivity but i need to make changes from fragment – AMAN SINGH Jan 05 '17 at 05:21
  • Please check my answer – Akhil Soman Jan 05 '17 at 05:31
  • If I understand correctly, clicking on the plus button is supposed to increase the badge count. Is that correct? What screen uses the badge count? Is it displayed somewhere? Is it used in a calculation? – Code-Apprentice Jan 05 '17 at 06:20
  • yes, you got correctly. i'm using sqlite for calculating the items. – AMAN SINGH Jan 05 '17 at 06:24

2 Answers2

0

First Create a new Interface Class

public interface OnStepCompletedListener {
     void onStepCompleted(int position);
}

Now implement this interface in your Activity class.

public class MainActivity extends AppCompatActivity implements OnStepCompletedListener{

and implement the method(in your activity)

@Override
public void onStepCompleted(int position){
//
// invoke method to change the badge number here
//
}

Now inside your fragment. Add this code where you want to change the badge.(inside the '+' button click)

try{
     ((OnStepCompletedListener) getActivity()).onStepCompleted(0);
//This will invoke the implemented method in your activity class. You 
//can pass any type of value through to your activity. Just add the 
//parameter in your interface declaration.
}catch (ClassCastException e){
     e.printStackTrace();
}
Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
-1

Define count variable as static and access it from the menu layout class and thereby put the value of static count onto the textview. I have done adding menu from the fragment and this was the code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}
Sumit Shetty
  • 112
  • 1
  • 9