0

I have a SlidingTabLayout and I'm attempting to change a settext every time a different page (Fragment) is selected.

I am attempting to do this through the fragments themselves. I have 3 total, and i'm doing so by calling getActivity() in the onCreateView

The application does not crash, but this causes the tabs to display the incorrect tab name on each tab. They are out of order and can display the name twice depending on the order clicked.

I'm not sure what I am doing wrong. Here's my current code:

Tab 1-3:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    TextView textView = (TextView)getActivity().findViewById(R.id.pageName);
    textView.setText("Tab X");

    return inflater.inflate(R.layout.discover_fragment,container,false);

}

I can display more code if requested. All help is appreciated.

Arshid KV
  • 9,631
  • 3
  • 35
  • 36
HTG
  • 584
  • 1
  • 8
  • 28
  • You must inflate the layout before accessing any UI elements in that layout. `inflater.inflate()` function should go first. – gnuanu Mar 16 '15 at 04:41

3 Answers3

0

Tutorial and Source code is available here and here.

In order to change the Tab Name while swiping tabs you need to check what is the current tab say in a fragment you can check the if the Fragment's onResume method is called, Now in onResume you need to update the Text of your current Tab.

You have not given reference to your TextView while initializing

Do like this

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.discover_fragment,container,false);
        TextView textView = (TextView)view.findViewById(R.id.pageName);
        textView.setText("Tab X");

        return view ;

    }
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • It gave me a nullpointerexception on the line for "textview.setText("Tab X");" line. – HTG Mar 16 '15 at 04:50
  • @SolomonPowell make sure you have id 'pageName' in your layout discover_fragment.xml – Jitender Dev Mar 16 '15 at 04:53
  • The `pageName` id is inside a layout that contains the `discover_fragment.xml` fragment. Is there something I should change? – HTG Mar 16 '15 at 05:01
0

The application does not crash, but this causes the tabs to display the incorrect tab name on each tab.

Call findViewById using getActivity() means accessing View from Activity layout in which current Fragment is added.

If TextView is inside Fragment layout then use getView() or View object which is return from onCreateView to access views from layout:

 @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, 
                                                      Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.discover_fragment,container,false);
        TextView txtView = (TextView)view.findViewById(R.id.pageName);
        txtView.setText("Tab X");
        return view;
    }

OR

If want to access view from Activity then make sure calling setContentView by passing layout id before adding Fragment to Activity:

TextView textView = (TextView)getActivity().findViewById(R.id.pageName);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • It gave me a nullpointerexception on the line for "textview.setText("Tab X");" line. – HTG Mar 16 '15 at 04:50
  • @SolomonPowell: in which layout you have `TextView` which `pageName` id? – ρяσѕρєя K Mar 16 '15 at 04:50
  • You mean which layout is my textview in? The `pagename` textview is located inside my `mainactivity.xml` layout. The 3 Tabs are fragments that are displayed inside the mainactivity. – HTG Mar 16 '15 at 04:57
0

Open your SlidingTabLayout.java, and add this line at the beginning of the class:

private ArrayList<TextView> mTextView = new ArrayList<>();

Add this line below something like tabTitleView.setText(adapter.getPageTitle(i));:

mTextView.add(tabTitleView);

Finally, add this method:

public void changeTitle(int position) {
    final PagerAdapter adapter = mViewPager.getAdapter();
    TextView tv = mTextView.get(position);
    if (tv != null) {
        tv.setText(adapter.getPageTitle(position));
    }
}

Now you can call changeTitle(0) to change the title text for tab 0.

iForests
  • 6,757
  • 10
  • 42
  • 75
  • Where do I call `changeTitle(0)`? – HTG Mar 16 '15 at 05:21
  • Anywhere you can reference your SlidingTab. For example, `slidingTabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs); slidingTabs.changeTitle(0);` will change the text for tab 0. – iForests Mar 16 '15 at 06:56
  • I tried to call it inside of the Fragment's tab like this `SlidingTabLayout tabs = (SlidingTabLayout)view.findViewById(R.id.tabs); tabs.changetitle(0)` inside my first tab, but it gave me a nullpointerexception when I tried to call `tabs.changeTitle(0)`? – HTG Mar 16 '15 at 19:10
  • Nevermind. There was an error with the compiler. Resetting did the trick. Thanks! – HTG Mar 18 '15 at 02:56