Add a textview and a button to linear layout programmatically

It’s very simple to add textviews and buttons to linear layout programmatically, just follow this steps. The fist step. Add a LinearLayout in .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_linear_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/thewordis"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

</LinearLayout>

Now in .class add:
1) add LinearLayout
2) add LayoutParams
3) add TextView
4) add Button
5) add the TextView and the Button to View

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ClassTheDeveloperWorldIsYours extends Activity{

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

        //added LInearLayout
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_linear_layout);

        //added LayoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        //add textView
        TextView textView = new TextView(this);
        textView.setText("The developer world is yours");
        textView.setId(1);
        textView.setLayoutParams(params);

        // added Button
        Button button = new Button(this);
        button.setText("thedeveloperworldisyours");
        button.setId(2);

        //added the textView and the Button to LinearLayout
        linearLayout.addView(textView);
        linearLayout.addView(button);

    }
}

You can download this code:

Download code

11 thoughts on “Add a textview and a button to linear layout programmatically

  1. Thanks for this tutorial, simple, readable and helpfull. It is 3 days i decided to start learn Android and build my first app and half of today spend with searching for help what to do when I have part of ui in XML and part need to be generated by code. This tutorial solves it within 3 minutes, good work bro đŸ˜€

  2. Thanks for your help. My simple problem is setContentView(), I set it in the end of the onCreate() procedure. So that, It took 2 days to solve.

  3. txtview[i]= new TextView(Shownews.this);
    txtview[i].setText(parseObjects.get(i).get(“NewsTitle”).toString());
    txtview[i].setLayoutParams(params);

    line[i]=new View(Shownews.this);
    line[i].setBackgroundColor(argb(255,255,207,50));
    line[i].setMinimumHeight(2);

    layout1.addView(txtview[i]);
    layout1.addView(line[i]);

    for multiple views and textview
    but showing error

  4. public class Shownews extends Activity {

    public LinearLayout layout1;
    public View line[];
    public TextView txtview[];

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

    layout1 = (LinearLayout) findViewById(R.id.info1);

    final LinearLayout.LayoutParams params;
    params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ParseQuery query = ParseQuery.getQuery(“UpdatedNews”);
    query.findInBackground(new FindCallback()
    {

    // public void done(List scoreList, ParseException e) {
    @Override
    public void done(List parseObjects, com.parse.ParseException e)
    {
    if (e == null)
    {
    Log.d(“news——>”, String.valueOf(parseObjects.size()));

    for (int i=0;i” + i, parseObjects.get(i).get(“NewsTitle”).toString());
    }
    }
    else
    {
    Log.d(“score”, “Error: ” + e.getMessage());
    }
    }
    });

    }

    }

    error like as follows
    02-16 17:40:09.097 29073-29073/com.dsstechnologies.janardanswami E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.dsstechnologies.janardanswami.Shownews$1.done(Shownews.java:58)
    at com.parse.FindCallback.internalDone(FindCallback.java:45)
    at com.parse.FindCallback.internalDone(FindCallback.java:31)
    at com.parse.Parse$4$1.run(Parse.java:792)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
    at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
    at dalvik.system.NativeStart.main(Native Method)

Leave a Reply

Your email address will not be published. Required fields are marked *