0

It drives me crazy. I have made research for one day, although I tried everything, nothing work.

this is my xml code:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/testo_tab_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="#000000"
    android:textSize="80dp"/>
</RelativeLayout>

My java's code is:

public class SocietaTabOne extends Fragment {


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

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

    TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
    titolo.setText("wow");

    return inflater.inflate(R.layout.societa_tab_one,null);
  }
}

I should see the text in my fragment, but I don't see anything.

Why?

2 Answers2

1

because here you are recreating the layout (erasing the text you set)

 return inflater.inflate(R.layout.societa_tab_one,null);

just return view you inflated at the beginning

return view;
Oleg Khalidov
  • 5,108
  • 1
  • 28
  • 29
0

Your Fragment should look Somthing like this

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

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

   TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
   titolo.setText("wow");

   return view;
}

Just return view instead of this line inflater.inflate(R.layout.societa_tab_one,null)

Masum
  • 4,879
  • 2
  • 23
  • 28