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); }
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);
}
}