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...
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.");
}
This should do the trick:
TextView.setText("test");
In your layout. Your Texto should not contain (android:text=...). I would remove this line. Either keep the Java string OR the (android:text=...)
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");
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!");
Maybe you have assigned the text in onResume() function
Please correct the following line:
err = (TextView)findViewById(R.id.texto);
to:
err = (TextView)findViewById(R.id.err);
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);
}
}
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
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.");
Try This:
TextView err = (TextView)findViewById(R.id.text);
Ensure you import TextView.
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" />
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
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.
Go to your activityMain and set the text by adding a widget from the widgets section and manually changing text by selecting and typing.
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");
Why don´t you try to assign the textview contents onStart() rather than onCreate()
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>
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
:
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)
}
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>
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,
Source: Stackoverflow.com