Android - Set text to TextView

AndroidTextview

Android Problem Overview


I'm currently learning some android for a school project and I can't figure out the way to set text dynamically to a TextView.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enviar_mensaje);
    err = (TextView)findViewById(R.id.texto);
    err.setText("Escriba su mensaje y luego seleccione el canal.");
}

This is currently not working and I can't find a way to make it work...

Any help will be much appreciated... Thank you for the time, José.

EDIT: Here is the activity_enviar_mensaje.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    tools:context=".EnviarMensaje" >
    ...
    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listaVista"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/listaVista"
        android:text="TextView" />
    ...
</RelativeLayout>

By not working I mean the text shown does not change at any moment...

Android Solutions


Solution 1 - Android

In your layout XML:

<TextView
        android:id="@+id/myAwesomeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Escriba el mensaje y luego clickee el canal a ser enviado"
        android:textSize="20sp" />

Then, in your activity class:

// globally 
TextView myAwesomeTextView = (TextView)findViewById(R.id.myAwesomeTextView);

//in your OnCreate() method
myAwesomeTextView.setText("My Awesome Text");

Solution 2 - Android

PUT THIS CODE IN YOUR XML FILE

<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

PUT THIS CODE IN YOUR JAVA FILE

// Declaring components like TextView globally is a good habit

TextView mTextView = (TextView) findViewById(R.id.textview1);

// Put this in OnCreate

mTextView.setText("Welcome to Dynamic TextView");

Solution 3 - Android

Well, @+id/listaVista ListView is drawn after @+id/texto and on top of it. So change in ListView from:

android:layout_below="@+id/editText1"

to:

android:layout_above="@+id/texto"

Also, since the list is drawn after textview, I find it dangerous to have android:layout_alignRight="@+id/listaVista" in TextView. So remove it and find another way of aligning.

EDIT Taking a second look at your layout I think this is what you really want to have:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EnviarMensaje" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Escriba el mensaje y luego clickee el canal a ser enviado"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listaVista"
        android:layout_alignParentBottom="true"
        android:text="TextView" />

    <ListView
        android:id="@+id/listaVista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/texto"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" >
    </ListView>

</RelativeLayout>

Solution 4 - Android

In layout file.

<TextView
   android:id="@+id/myTextView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="Some Text"
   android:textSize="18sp"
   android:textColor="#000000"/>

In Activity

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText("Hello World!");

Solution 5 - Android

In xml use this:

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

In Activity define the view:

Textview textView=(TextView)findViewById(R.id.textView);
textView.setText(getResources().getString(R.string.txt_hello));

In string file:

<string name="txt_hello">Hello</string>

Output: Hello

Solution 6 - Android

I had a similar problem. It turns out I had two TextView objects with the same ID. They were in different view files and so Eclipse did not give me an error. Try to rename your id in the TextView and see if that does not fix your problem.

Solution 7 - Android

Why don´t you try to assign the textview contents onStart() rather than onCreate()

Solution 8 - Android

in your activity_main.xml paste this code:

            <TextView
                 android:id="@+id/name"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true"
                 android:layout_marginLeft="19dp"
                 android:layout_marginTop="43dp"
                 android:text="@string/name" />

and go to res folder->values->strings.xml paste the below code with the code that already exists:

          <string name="name">Escriba su mensaje y luego seleccione el canal.</string>

the above code means that you have created a textview with id:name(android:id="@+id/name") and assigned that textview to a string with an identifier name(android:text="@string/name") in strings.xml your using that identifier name to assign the text,

Solution 9 - Android

Try This:

TextView err = (TextView)findViewById(R.id.text);

Ensure you import TextView.

Solution 10 - Android

This should do the trick:

TextView.setText("test");

Solution 11 - Android

Please correct the following line:

err = (TextView)findViewById(R.id.texto);

to:

err = (TextView)findViewById(R.id.err);

Solution 12 - Android

In XML file,

<TextView
       android:id="@+id/textview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="My Name"
       android:textColor="#cccccc"/>

In Java Activity file,

public class MainActivity1 extends Activity
 {
   TextView t1;
   public void onCreate(Bundle onSavedInstance)
      {
         setContentView(R.layout.xmlfilename);
          t1 = (TextView)findViewbyId(R.id.textview);
      }
 }

Solution 13 - Android

> first your should create an object for text view TextView show_alter

show_alert = (TextView)findViewById(R.id.show_alert);
show_alert.setText("My Awesome Text");

Solution 14 - Android

You can set string in textview programatically like below.

TextView textView = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

or

TextView textView = (TextView)findViewById(R.id.texto);
err.setText(getActivity().getResource().getString(R.string.seleccione_canal));

You can set string in xml like below.

<TextView
         android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="19dp"
         android:layout_marginTop="43dp"
         android:text="Escriba su mensaje y luego seleccione el canal." />

or

<TextView
         android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="19dp"
         android:layout_marginTop="43dp"
         android:text="@string/seleccione_canal" />

Solution 15 - Android

As you have given static text

err.setText("Escriba su mensaje y luego seleccione el canal.");

It will not change , it will remain same.

Example for Dynamic Text for textview is :

MainActivity.java
package com.example.dynamictextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	int count = 0;
	Button clickMeBtn;
	TextView dynamicText;

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

		clickMeBtn = (Button) findViewById(R.id.button_click);
		dynamicText = (TextView) findViewById(R.id.textview);

		clickMeBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				count++;
				dynamicText.setText("dynamic text example : " + count);

			}
		});

	}

}

For activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dynamictextview.MainActivity" >

    <Button 
        android:id="@+id/button_click"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click me"
        android:layout_centerInParent="true" />
    
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@id/button_click"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        />

</RelativeLayout>

Solution 16 - Android

You should use ButterKnife Library http://jakewharton.github.io/butterknife/

And use it like

@InjectView(R.id.texto)
TextView err;

in onCreate method

ButterKnife.inject(this)
err.setText("Escriba su mensaje y luego seleccione el canal.");

Solution 17 - Android

Your code is ok, you are loading the .xml that contains the TextView using setContentView():

   setContentView(R.layout.activity_enviar_mensaje);

and then getting the reference of the TextView inside activity_enviar_mensaje.xml, and setting a text:

err = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

The problem is that your TextView is hidden by the ListView:

enter image description here

Solution 18 - Android

I know its 2 months but yeah

replace your code

Private TextView err;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enviar_mensaje);
    err = (TextView)findViewById(R.id.texto);
    err.setText("Escriba su mensaje y luego seleccione el canal.");
}

Solution 19 - Android

In your layout. Your Texto should not contain (android:text=...). I would remove this line. Either keep the Java string OR the (android:text=...)

Solution 20 - Android

Go to your activityMain and set the text by adding a widget from the widgets section and manually changing text by selecting and typing.

Solution 21 - Android

final TextView err = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

you can find every thing you need about textview here

Solution 22 - Android

Kotlin Solution

To set using a String, just use this

view.text = "My string"

To do the same with a resource value, add this extension property to much more easily set your text

view.textRes = R.string.my_string

var TextView.textRes
    get() = 0 // HACK: property requires getter
    set(@StringRes textRes) {
        text = resources.getText(textRes)
    }

Solution 23 - Android

Maybe you have assigned the text in onResume() function

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjpaguerreView Question on Stackoverflow
Solution 1 - AndroidHussain Akhtar Wahid 'Ghouri'View Answer on Stackoverflow
Solution 2 - AndroidAnkush JoshiView Answer on Stackoverflow
Solution 3 - AndroidgunarView Answer on Stackoverflow
Solution 4 - AndroidVishal ChhodwaniView Answer on Stackoverflow
Solution 5 - AndroidShivam KumarView Answer on Stackoverflow
Solution 6 - Androidholy molyView Answer on Stackoverflow
Solution 7 - AndroidloonighanView Answer on Stackoverflow
Solution 8 - AndroidsaikaView Answer on Stackoverflow
Solution 9 - AndroidsamuelView Answer on Stackoverflow
Solution 10 - AndroidDenVikView Answer on Stackoverflow
Solution 11 - AndroidRaiyus13View Answer on Stackoverflow
Solution 12 - AndroidGavine JoyceView Answer on Stackoverflow
Solution 13 - AndroidJerin StephenView Answer on Stackoverflow
Solution 14 - AndroiddugguView Answer on Stackoverflow
Solution 15 - AndroidHeena AroraView Answer on Stackoverflow
Solution 16 - AndroidNileshView Answer on Stackoverflow
Solution 17 - AndroidJorgesysView Answer on Stackoverflow
Solution 18 - Androidlife isnothingtomeView Answer on Stackoverflow
Solution 19 - AndroidSalman MohammedView Answer on Stackoverflow
Solution 20 - AndroidThe_CIAView Answer on Stackoverflow
Solution 21 - AndroidAmirhossein AarbView Answer on Stackoverflow
Solution 22 - AndroidGiboltView Answer on Stackoverflow
Solution 23 - AndroidPriyanthiView Answer on Stackoverflow