Search code examples
androidfragmentfindviewbyidsetcontentview

Can't change text of TextView in onCreateView method of Fragment


I am using a default navigation drawer activity with fragments that populate each menu.

I have a textview in one of the fragments(fragment_listing_routel.xml). I want to modify a textview that is located in the fragment. To do so, I use findViewByID to return the textView instance so that I can modify its text.

I know that before using findViewByID, I have to use setContentView, otherwise I get null error. However, I can't do so because as soon as I setContentView to the fragment that contains the TextView, it immediately sets the fragment to full screen, which gets rid of the navigation drawer and keeps me from being able to switch between menus. What I don't understand is that when I use this approach, the changes are successfully applied but the nav drawer dissapears.

So I searched for a way to use findViewByID WITHOUT setContentView so that the navdrawer stays and so that I can also modify the TextView. I came accross this

Doing so doesn't give any errors, however does not return the right instance of the TextView, since when I try to change the text in TextView, it doesn't work.

fragment_listings_route.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".RouteListingsFragment">

    <TextView
        android:id="@+id/texttest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST"/>

</LinearLayout>

RouteListingFragment.java

TextView txtView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //get id
    final LayoutInflater factory = getLayoutInflater();
    final View routelistingsview = factory.inflate(R.layout.fragment_listings_route, null);

    txtView = (TextView) routelistingsview.findViewById(R.id.texttest);
    txtView.setText("NEW TEXT");    
    return inflater.inflate(R.layout.fragment_listings_route, container, false);
}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/fMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"></FrameLayout>
</android.support.constraint.ConstraintLayout>

So to recap, How can I modify a TextView from the Fragment class? I don't see why my approach is not working.


Solution

  • Try this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //get id
        final View routeListingsView = inflater.inflate(R.layout.fragment_listings_route, null);
    
        txtView = (TextView) routeListingsView.findViewById(R.id.texttest);
        txtView.setText("NEW TEXT");    
        return routeListingsView; // You need to return the view in which the changed text exists
    }