Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:46:18+00:00 2026-06-11T00:46:18+00:00

I just started android and I tried below questions to get this answer before

  • 0

I just started android and I tried below questions to get this answer before posting it here:

Android – Adding layout at runtime to main layout

Add button to a layout programmatically

Dynamically adding a child to LinearLayout with getting each child's position

And I am still not able to add a button to linear layout 🙁

Below is the code for activity, please let me know where I am wrong:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.activity_main, null);

        Button btn = new Button(this);
        btn.setId(123);
        btn.setText("Welcome to WI FI World");
        layout.addView(btn);
    }

And xml looks like below:

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

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:46:19+00:00Added an answer on June 11, 2026 at 12:46 am

    Take the Id for LinearLayout in XML and in jave code use that id of LinearLayout from XML

    <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:id="@+id/linear" >
    
    </LinearLayout>
    

    In onCreate():

    LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
    
     //Select widgets
     linear.addView()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started using android, and have about 5 layout files finished. However,
Im very new to this android development.I just started to create a hello world
Hey. I just started with Android. I'm using the ViewFlipper layout with 2 LinearLayouts.
I just got started today developing for Android. I tried to make an application
I have just started using Android. I have tried an apps with orientation change.
So I just started Android development, and I've already hit a wall. I have
I am a research student who just started the android programming for 3 weeks
I just started looking at android app development. For my first test app i
I just started working with Android and I have a problem with my map.
Hey I just started dabbling with android programming, and from my short period of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.