How to refer to a textView that is in a Layout that is not that of the setContentView ()?

6

UPDATE

I have 2 layouts that are loaded at the same time, in the 1st layout activity_main.xml I have a listView and a textView , in the 2nd layout single_post.xml I have 4 textView that are those shown in listView of activity_main.xml

I want to manipulate the color of the text programmatically from single_post.xml , I try to access it using LayoutInflater but apparently it does not work, with the code I have below I can change the text color of textView of activity_main.xml but I can not do anything with the textView of single_post.xml

On the MainActivity.java

 public class MainActivity extends AppCompatActivity {

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

        String sDesignation= "manager";

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.single_post, null);

        TextView estatus_SINGLE_POST=(TextView)view.findViewById(R.id.txt_designation_SINGLE_POST);
        TextView estatus_ACTIVITY_MAIN=(TextView)findViewById(R.id.txt_designation_ACTIVITY_MAIN);

            if(sDesignation.equals("manager")){
                   Toast.makeText(MainActivity.this, "YES "+sDesignation, Toast.LENGTH_LONG).show();
                   estatus_ACTIVITY_MAIN.setTextColor(Color.RED);
                   estatus_SINGLE_POST.setTextColor(Color.RED);
            }else{
                Toast.makeText(MainActivity.this, "NO "+sDesignation, Toast.LENGTH_LONG).show();
                estatus_ACTIVITY_MAIN.setTextColor(Color.GREEN);
                estatus_SINGLE_POST.setTextColor(Color.GREEN);
            }
          }
        }

In the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    tools:context="com.example.user.testbd.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation_ACTIVITY_MAIN"
        android:textSize="18sp"
        android:textColor="@android:color/black" />    
</RelativeLayout>

In the single_post.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main3">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation_SINGLE_POST"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

The idea is to obtain the value of a textView that is loaded in another layout but I can only get the value of the textView that are loaded in the layout that is indicated in the method setContentView();

    
asked by El Cóndor 02.03.2017 в 06:07
source

5 answers

4

If it is not declared within the layout that is loaded via setContentView () which in this case is activity_main.xml , this is not possible.

setContentView(R.layout.activity_main);

The only option is that within the file of the layout there is a reference to another that contains it, this is done through include

For example, suppose the TextView is inside the otro_layout.xml file that is indicated as include , so if you can get the reference:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/otro_layout"/>


</LinearLayout>
    
answered by 02.03.2017 в 07:05
2

If the TextView that you want to use has not been loaded in the context of the activity by setContentView() because it is in another layout, you can load the other layout explicitly and quite simply, making use of < a href="https://developer.android.com/reference/android/view/LayoutInflater.html"> LayoutInflater .

// Instancia del servicio LayoutInflater
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// other.xml layout cargado (inflado) como View.
View view = inflater.inflate(R.layout.other, null); 

// Obtiene una referencia al TextView del otro layout, other.xml
TextView tvOther = (TextView)view.findViewById(R.id.tvOther); 

Now, you can make normal use of TextView , how to get your text and even add it to the current activity layout by using addView()

    
answered by 02.03.2017 в 09:33
1
  

The idea is to obtain the value of a textView that is loaded in another layout   but I can only get the value of the textView that are loaded in the   layout that is indicated in the setContentView () method;

If you want to use a text that comes from another Activity / Fragment / Etc with another layout, you should send that text to the Activity where you want to use it as an extra in the intent.

I give you an example that uses a static method to manage the extras of the Activity, doing it this way is a good practice.

Example:

First you need the text that you get when you use your layout.

String text = textView.getText().toString();

Now you need to send this text to the Activity where you want to use it and it does not contain that textView in your layout.

startActivity(MyActivity.buildIntent(this, text));

When you start the Activity you get the text of the extra you sent.

    public static String EXTRA_TEXT = "extra_text";

    public static Intent buildIntent(Context context, String text){
        Intent intent = new Intent(context, MyActivity.class);
        intent.putExtra(EXTRA_TEXT, text);
        return intent;
    }

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

        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
            String text = bundle.getString(EXTRA_TEXT);
        }
    }

Here you already have in the variable 'String text' the text that comes from a TextView of another layout that you do not use in this Activity

From what I see you are loading the second layout but you do not add it to any view, so it should not be accessible even if it is loaded.

If I have understood correctly, you should add all the views you want to see, modify, etc in the main layout.

Try this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    tools:context="com.example.user.testbd.MainActivity">

    <include
        android:id="@+id/other_layout" 
        layout="@layout/single_post"/>

    <ListView
        android:layout_below="@id/other_layout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation_ACTIVITY_MAIN"
        android:textSize="18sp"
        android:textColor="@android:color/black" />    
</RelativeLayout>

If you want the layout not to be visible add visibility="gone". And if you want it to be visible but appear elsewhere, restructure the layout activity_main.xml

    
answered by 02.03.2017 в 11:22
1

What if you only declare it with your methods in a class and then just call it from the main class.

example:

public String obtener valor(String s){
    TextView estatus=(TextView)findViewById(R.id.txt_designation_single_post);

 s=estatus.getText().toString();

  return s;
}

Even though of course in the class that contains the methods you declare the corresponding layout.

    
answered by 06.03.2017 в 11:21
0

That manipulation must be done in the Adapter when you add each element to the ListView. Surely at some point you are inflating and that is where you can modify it, within the Adapter.

If you want to put the code of your Adapter and guide you, but although there are many ways to do it, you can check if you have the getView(int position, View convertView, ViewGroup parent) method in your adapter. This method is responsible for creating the view that is mirtrated in each item of the list. Here you can modify and search for any element with the id.

    
answered by 07.03.2017 в 14:58