Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Set text to TextView

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...

like image 809
jpaguerre Avatar asked Oct 18 '13 14:10

jpaguerre


People also ask

How do I assign text to TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

What is SetText in Android Studio?

SetText(Int32, TextView+BufferType)Sets the text to be displayed using a string resource identifier.

What is a TextView in android?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

What is lineSpacingExtra?

The difference is that android:lineSpacingExtra add extra spacing between lines of text of TextView and android:lineSpacingMultiplier work as scale factor for height of line space. in other words, each line height will be height*multiplier + extra. Follow this answer to receive notifications.


1 Answers

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"); 
like image 197
Hussain Akhtar Wahid 'Ghouri' Avatar answered Oct 22 '22 06:10

Hussain Akhtar Wahid 'Ghouri'