Home » Android » How to create simple Android TextView and display text on it using java code?

How to create simple Android TextView and display text on it using java code?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have created a sample project and run ‘Hello Android Application’ in Eclipse.

I have learned that a Textview can be created in two ways, either using an XML tag or by using Java code.

By default I have one Textview saying “Hello world” in my sample project. I want to create a Textview using Java code and display some message on it.

I have searched a lot, but I am unable to understand the steps and layout settings mentioned in the code.

This is what I have done:

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

        TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tx.setText("ANDROID APP");
        lay
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Further I don’t know how to add this textview in addView().

This is my 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=".MainActivity" >
</RelativeLayout>

A step by step solution would be helpful for me and any good tutorial link would be appreciable. Thank you in advance!

How to&Answers:

Use this code, Create text view and set layout params

TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText(" Hello World ");

add this textview to the main layout

mainlayout.addView(dynamicTextView);

Answer:

.xml file

<LinearLayout 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"
    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="com.example.demo.MainActivity" >

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

</LinearLayout>

.java file

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name="Prakash Gajera";
        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(name);
    }

Answer:

Assuming you have one root Layout in the .xml file with id “my_root”

LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);

Create a new Layout:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); 

Create a TextView:

TextView textView = new TextView(this);

Set some text:

textView.setText("some text");

Add your TextView to the Layout:

layout.addView(textView);

Finally add yout Layout to the root Layout:

my_root.addView(layout);

Answer:

add the textView to the linearlayout like this.
linearLayout.addView(textView).

Before create an instance for the linearlayout.

Answer:

It is recommended to use XML to define layouts. Only create Views when you have to create them dynamically.

If you really want to create TextViews by code, then you need to have a reference to the parent layout. So instead of setting the content view to an XML layout directly, you would have to inflate the XML layout and then set the content view to that View. Example:

View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
setContentView(view);

LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("ANDROID APP");

view.addView(tx); //here the textview is attached to the parent

Answer:

COde:

TextView textView = new TextView(this);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setText("Test");

mainlayout.addView(textView );

Answer:

try this

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout)findViewById(yourlayoutid from xml file);
        LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

        TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tx.setText("ANDROID APP");
        layout.add(tx);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Answer:

copy and paste this code hope it will help you.

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   LinearLayout ll = new LinearLayout(this);

    TextView tx= new TextView(this);
    tx.setText("ANDROID APP");
    ll.addView(tx);

    setContentView(ll);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

 }

Answer:

Design

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

Java

TextView tv = findViewById(R.id.textview);
tv.setText("Kirubha");

Answer:

If your activity_main xml has a top LinearLayout with id mylayout.

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
layout.addView(tx);