简体   繁体   中英

Set TextView text from a different Activity(with a different layout)

Been struggling with this for a while now. Whenever I try to set the text of a label from my MainActivity using the setText() method, nothing happens, I've tried several solutions none of them seem to work for me, it is important to know though, that the layout Im trying to change has no Activity, because I want to use it inside another layout which would have a ListView inside, anyway I just wanna know if it is possible for me to do this from my MainActivity, here is an example of what I'm trying to do

Code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void letsee(View view) {

//LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View content = (View)getLayoutInflater().inflate(R.layout.test, null);

    //View vi = inflater.inflate(R.layout.test, null, false);

    TextView tv = (TextView)content.findViewById(R.id.testview);

    tv.setText("DOES THIS EVEN WORK?");

    setContentView(R.layout.test);
}
}

this is what I've tested, even what's commented, I want the text to be displayed in the TextView when i click the button to change to testlayout, layout's name's test btw

XML:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rtest">


    <TextView
    android:id="@+id/testview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
  </RelativeLayout>

I am aware you cannot call a resource by its id unless your set layout contains said element, I just hope there is a workaround for that.

You are using TextView without setting test layout as content view. Try to set content view first setContentView(R.layout.test) then use view as per your needs.

Try this:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.YOUR_BUTTON);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                letsee();
            }
        });
    }

    public void letsee() {
        setContentView(R.layout.test);

        TextView tv = (TextView) findViewById(R.id.testview);
        tv.setText("DOES THIS EVEN WORK?");
    }
}

UPDATED:

You can use Fragment to change your view. Here is an complete example:

  1. In our MainActivity, there are two buttons named Fragment No.1 & Fragment No.2 and a initial Fragment named Fragmentone
  2. If Button Fragment No.2 pressed, FragmentOne will be replaced by FragmentTwo (which contains TextView value This is fragment No.2 )
  3. If Button Fragment No.1 pressed, FragmentTwo will be replaced by FragmentOne (which contains TextView value This is fragment No.1 )

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" /> 

   <fragment
        android:name="com.android.fragmentstest.FragmentOne"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#00ffff">

       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="This is fragment No.1"
           android:textStyle="bold" />

</LinearLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#ffff00">

       <TextView
           android:id="@+id/textView2"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="This is fragment No.2"
           android:textStyle="bold" />

</LinearLayout>

FragmentOne.java

package com.android.fragmentstest;

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

       //Inflate the layout for this fragment

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

FragmentTwo.java

package com.android.fragmentstest;

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentTwo extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

      // Inflate the layout for this fragment

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

MainActivity.java

package com.android.fragmentstest;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
         Fragment fr;

         if(view == findViewById(R.id.button2)) {
             fr = new FragmentTwo();

         }else {
             fr = new FragmentOne();
         }

         FragmentManager fm = getFragmentManager();
         FragmentTransaction fragmentTransaction = fm.beginTransaction();
         fragmentTransaction.replace(R.id.fragment_place, fr);
         fragmentTransaction.commit();

    }
}

You can also see this tutorial

Hope this will help~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM