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...
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");
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");
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>