1
public TextView descriptionTextView(Context context, String text) {
        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new EditText(context);
        textView.setLayoutParams(lparams);
        textView.setTextSize(10);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setText(" " + text + "");
        textView.setMaxEms(8);
        textView.setKeyListener(null);
        textView.setFocusableInTouchMode(false);
        textView.setEnabled(false);
        return textView;
    }

Here is the code that I have written for the TextView. I would like to reference it from another classes, or within the same class, but I cannot find a way to pin it down; as in I would like to change the value based on an input.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
Alex Z
  • 11
  • 3
  • You must be using this method to add `TextView` to some Layout at runtime. So you can find it out again at runtime by reading the children views of that Layout and than cast the `View` as `TextView` and update the data. – Harpreet Feb 03 '20 at 10:18

3 Answers3

1

Give TextView a id and then access the textView via id like below -

give id like textView.setId(1);

public TextView descriptionTextView(Context context, String text) {
        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new EditText(context);
        textView.setLayoutParams(lparams);
        textView.setTextSize(10);
        textView.setId(1);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setText(" " + text + "");
        textView.setMaxEms(8);
        textView.setKeyListener(null);
        textView.setFocusableInTouchMode(false);
        textView.setEnabled(false);
        return textView;
    }

let say your layout for textView is LinearLayout.

you can get textView by id by using below code -

TextView tv = (TextView)view.findViewById(1); // view is LinearLayout object

Hope this helps!

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
0

This is the code to create TextView Dynamically

LinearLayout layout = findViewById(R.id.linearLayout);

TextView view = descriptionTextView(this, "Sample text");
layout.addView(view);

Below method for calling it from same/different class:

public TextView descriptionTextView(Context context, String text){
    ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView (context); //Change this to TextView
    textView.setLayoutParams(lparams);
    textView.setTextSize(10);
    textView.setTextColor(Color.rgb(0,0,0));
    textView.setText(" " + text + "");
    textView.setMaxEms(8);
    textView.setKeyListener(null);
    textView.setFocusableInTouchMode(false);
    textView.setEnabled(false);
    return textView;
}
Karthik
  • 59
  • 3
0

Creating Views other them XML file is not a preferred method in Android. The best approach is to make this TextView in XML file and link it using its id. like this.

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

Futher use this anywhere in you classes and activity by creating an object like this.

 TextView textView = findViewById(R.id.textView);
    //to change value use this 
    textView.setText("Value you want to set");
    //to change its visibility
    textView.setVisibility(textView.GONE);
    //to get its string
    String value = textView.getText().toString();

If this method is not helpful and you still want to make views in Java file then, make them public and out of function boundaries to make them accessible everywhere.

Zia
  • 705
  • 9
  • 11