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 dynamicaly to a TextView.

Here is my code:

  • Generate apk name as package name in Android Studio
  • How to allow users to check for the latest app version from inside the app?
  • Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio
  • Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565
  • Android TextView link without underline?
  • How can I completely hide the groupIndicator of an ExpandableListView?
  • 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 apretiated…
    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"
        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: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:layout_alignRight="@+id/listaVista"
            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>
    

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

    Related posts:

    Android: long click on the child views of a ExpandableListView?
    How is Facebook Chat Heads implemented?
    IntelliJ Idea auto create new method
    Android device configuration for AOSP
    How to design Gridview like guardian app
    Android Select row by Id in Sqlite?
  • android requestFocusFromTouch brings up menu with first item highlighted
  • A SQLiteConnection object for database was leaked! Please fix your application
  • How to create a DataGrid display in android?
  • Android Date Time picker in one dialog
  • How to create a tree view in Android?
  • android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
  • 17 Solutions collect form web for “Android – Set text to TextView”

    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

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

    PUT THIS CODE IN YOUR JAVA FILE

    // Declaring componants like TextView Globally is a good Habbit

    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>
    

    Try This:

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

    Ensure you import TextView.

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

    Please correct the following line:

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

    to:

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

    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.

    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,

    This should do the trick:

    textView.setText("test");
    

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

    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

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

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

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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.