0

i am learning android development and i was testing something: i was testing and everything went successful but my text didn't appear on my app. What's the problem ?

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView monTexte=new TextView(this);

        monTexte.setText("A salamu aleykoum");
        monTexte.setTextSize(50);
        monTexte.setTextColor(0x0000FF);

        setContentView(monTexte);
    }
}
webprogrammer
  • 2,393
  • 3
  • 21
  • 27
fathi93
  • 327
  • 2
  • 12

1 Answers1

0

You can just do them in your xml by adding these to your TextView

        android:text="A salamu aleykoum"
        android:textSize="50sp"
        android:textColor="#0000FF"

To do it programatically, you need to give your TextView an ID in your xml. In my case (activity_main.xml)

    <TextView
        android:id="@+id/monTexte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Then bring your TextView and find it using findViewById

package com.example.stackoverflow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView monTexte;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        monTexte = findViewById(R.id.monTexte);
    }



}

Now you can


    monTexte.setText("A salamu aleykoum");
    monTexte.setTextSize(50);
    monTexte.setTextColor(0x0000FF);

Altogether:


package com.example.stackoverflow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView monte;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        monTexte = findViewById(R.id.monTexte);
        monTexte.setText("A salamu aleykoum");
        monTexte.setTextSize(50);
        monTexte.setTextColor(0x0000FF);
    }

}


I really hope it helps!

unobatbayar
  • 1,223
  • 1
  • 10
  • 24
  • thanks ! it works. another thing, i wanted to know if it is possible to create and display a TextView programmatically ? i mean without using and XML file – fathi93 Feb 17 '20 at 03:35
  • Hi, I'm glad it worked. Yes, it's possible with addView() method. It's answered here: https://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview – unobatbayar Feb 17 '20 at 03:42