0

I am trying to create a fragment with scrollable text. I have the following class extending Fragment with the two methods below. I'm getting an error "The method findViewbyID(int) is undefined for the type exampleFragmentText" and the method "stSelected(boolean) is undefined for the type exampleFragmentText" How should I be doing this?

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

        return inflater.inflate(R.layout.textview_main, container, false); //just return the  view ;

    }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //here you can initialise your variables,listeners,e.t.c ;
        super.onActivityCreated(savedInstanceState);

        TextView textView = (TextView) findViewbyID(R.id.myText);
        textView.stSelected(true);

     //   addListenerOnButton();
    }
protected View findViewById(int id)
{
    return getView().findViewById(id);
}
user3293643
  • 91
  • 1
  • 3
  • 12

3 Answers3

0

Try:

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


View view =  inflater.inflate(R.layout.textview_main, container, false);

TextView textView = (TextView) view.findViewbyID(R.id.myText);
textView.stSelected(true);
return view

}

Andre Perkins
  • 7,640
  • 6
  • 25
  • 39
0

Maybe you can try:

TextView textView = (TextView) getView().findViewbyID(R.id.myText);

This is because, fragment is a part of a base activity. You need to take the view of the base to know ids of components.

I don't know If I explain it well. Sorry for my english.

Another option is, if your fragment starts like this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_new_record, container, false);

you can use v to get the views of the components, ex:

TextView textView = (TextView) v.findViewbyID(R.id.myText)
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
0

Replace

textView.stSelected(true);

with

textView.setSelected(true);

Also, you don't need to override findViewById().

Objectist
  • 51
  • 6