0

I'm trying to set the number/count of notifications in a textview on a tab on my MainActivity. I have successfully get the data between my fragment and activity through the use of interface. Every time when I received a notification, it will pass the number to my MainActivity. I am able to display the number but I do not know how to implement and set on my textview.

public void onDataPass(String data) { Log.d("LOG","hello " + data); }

I am able to get value from the above code. Below is my logcat:

06-02 07:02:16.213 12310-12310/ D/LOG: hello Number of notifications: 1

06-02 07:03:26.697 12310-12310/ D/LOG: hello Number of notifications: 2

06-02 07:04:35.718 12310-12310 D/LOG: hello Number of notifications: 3

However, I'm trying to set this number on my textview. I'm getting null value even after declaring my "data" as public. I'm not sure how do I put the value of my data in the textview. Can anybody help me with this?

   if(tab != null && tab.getCustomView() != null) {
        TextView b = (TextView) tab.getCustomView().findViewById(R.id.badge);
        if (b != null) {
            b.setText(data);
            Log.d("hello1", "test1" + data);
        }

enter image description here

Below is my full MainActivity code: MainActivity.java

public class MainActivity extends AppCompatActivity implements NotificationFragment.OnDataPass {
    public String data;
    private String name;
    private String email;
    private TextView badgeText;

    public void onDataPass(String data) {
        Log.d("LOG","hello " + data);
    }

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


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

        ActionBar ab = getSupportActionBar();
        ab.setTitle("Test");

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);



        final int[] tabIcon = new int[]{
                R.drawable.tab_item_remit_state,
                R.drawable.tab_item_account_state,
                R.drawable.tab_item_notification,
        };

        tabLayout.getTabAt(0).setIcon(tabIcon[0]);
        tabLayout.getTabAt(1).setIcon(tabIcon[1]);
        tabLayout.getTabAt(2).setIcon(tabIcon[2]);

        TabLayout.Tab tab = tabLayout.getTabAt(2);
        tab.setCustomView(R.layout.badged_tab);


        if(tab != null && tab.getCustomView() != null) {
            TextView b = (TextView) tab.getCustomView().findViewById(R.id.badge);
            if (b != null) {
                b.setText(data);
                Log.d("hello1", "test1" + data);
            }
            final View v = tab.getCustomView().findViewById(R.id.badgeCotainer);
            View a = tab.getCustomView();

            a.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View a, MotionEvent event) {
                    v.setVisibility(View.INVISIBLE);
                    return false;
                }
        });

        if (v != null ) {
            v.setVisibility(View.VISIBLE);

        }
    }
arsenallavigne
  • 131
  • 1
  • 2
  • 16
  • Couple of things, in the onDataPass() function can't see the part which assigns the data to your class level variable of the activity, you are only logging? Also I think while onCreate method is called data won't be there in the class level data variable of activity. You might want to manually call the function which will set the tab text value inside onDataPass() function. – Sathyajith Jun 02 '17 at 08:38
  • @SAJ Yup, i'm only logging. How do I call the method onDataPass in my onCreate? Or is it possible if I set text under the method onDataPass? I only log under onDataPass because my tabs are onCreate . – arsenallavigne Jun 02 '17 at 08:51
  • 1
    yes, you need to add the setTabText logic inside your onDataPass method. You shouldn't call onCreate method. Extract the tab text logic to a separate function which sets the and call that function from onDataPass. – Sathyajith Jun 02 '17 at 09:11
  • Thanks! I will try to implement the setText on tab logic on my onDataPass method. – arsenallavigne Jun 02 '17 at 13:34

1 Answers1

1

Create an interface

public interface OnDataReceiverListener {
   void onDataReceived(String myData);
}

Declare Interface in class, where you got data that needs to be passed in other activity and create setter for that... Assuming MyReceiver.java for class name

private static OnDataReceiverListener onDataReceiveListener;
public static void setOnDataReceiveListener(OnDataReceiverListener _listener) {
    onDataReceiveListener = _listener;
}

call this setter in the Activity's onCreate(), Assuming MainActivity

MyReceiver.setOnDataReceiveListener(this);

// This will overided
@Override
public void onDataReceived(String myData) {
    // Process your data 
    TextView b = (TextView) mTabLayout .getTabAt(2).getCustomView().findViewById(R.id.badge);
    if (b != null) {
        b.setText(myData);
    }       
}

Don't forget to set setter to null as interface is static. Do this job in onDestroy()

public void onDestroy() {
   MyReceiver.setOnDataReceiveListener(null);
   super.onDestroy();
}

Summary, Declare mTabLayout as global instance, set badge text in onDataReceived callback.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • Thanks for providing me an example of Interface. Actually, I'm already using Interface to pass the data and I am able to get the data as well. I'm just trying to set the data on textview on the tabs but I dont' know how to call the tabs on my onDatapass method as the tabs are onCreate. I'm trying to implement the logic under the onDatapass method. Another way im doing is to set the data onCreate but have no idea how to call the method inside onCreate. Sorry, if my question confuses you. – arsenallavigne Jun 02 '17 at 13:44