Search code examples
androidandroid-layouttextviewlayout-inflatersettext

text isn't showing after using setText() to TextView() in inflated layout


Yes, I know that similar questions were asked before but I tried many of those. So I have a problem with setting text in TextView in the inflated layout. I tried setContentView() then it works but then activity_main.xml with the menu isn't working. So I tried to inflate layout with TextView that I need. When I try to setText() it just doesn't displaying it. Here is my code:

public void bodAlg() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.layout1,  null);  

    EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
    String[] serversArray = editText.getText().toString().split(", ");

    TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
    textView.setText(Arrays.toString(serversArray));
}

And here is xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/rl_01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/ll_01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/bod_servers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Cray CS-Storm, Vulcan – Blue Gene/Q, Blue Gene/Q, Stampede – PowerEdge C8220, Piz Daint – Cray XC30"
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/bod_serv_array"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="15sp"
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:paddingBottom="5dp"/>
    </LinearLayout>
</RelativeLayout>
</ScrollView>

Thank you for answer!


Solution

  • Thanks for comments! Problem is solved!

    public void bodAlg() {
        LayoutInflater inflater = (LayoutInflater) 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View vi = inflater.inflate(R.layout.layout1,  null);  
    
        EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
        String[] serversArray = editText.getText().toString().split(", ");
    
        TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
        textView.setText(Arrays.toString(serversArray));
    
        // solution 
        CoordinatorLayout mainL = (CoordinatorLayout) findViewById(R.id.main_view);
        mainL.removeAllViews(); // remove previous view, add 2nd layout
        mainL.addView(vi);
    }