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 8581241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:59:59+00:00 2026-06-11T20:59:59+00:00

I already have an XML layout with some buttons in it, and now i

  • 0

I already have an XML layout with some buttons in it, and now i want to add a textview to the same layout, but in my Java class. I don’t get any errors until the “addView” line. I would also appreciate it if someone could tell me a better way to add onto a pre-existing XML layout in Java.

public class MyActivity extends Activity{
TextView textview;
RelativeLayout layout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout=(RelativeLayout)findViewById(R.id.mylayout); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textview=new TextView(this);
textview.setId(16);
textview.setText("Help");
layout.addView(textview, params);
setContentView(layout);
}
  • 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-11T21:00:01+00:00Added an answer on June 11, 2026 at 9:00 pm

    Your code should be like this, since you already have xml layout, you should first setContent to xml layout and then add new view to the Relativelayout.

    public class MyActivity extends Activity{
    
    TextView textview;
    RelativeLayout layout;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_name);
        layout=(RelativeLayout)findViewById(R.id.mylayout); 
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        textview=new TextView(this);
        textview.setId(16);
        textview.setText("Help");
        layout.addView(textview, params);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XDocument class with the XML contents already made. I basically want
I already have an enterprise Java EE application. I want expose some of the
I already have a relative_layout.xml and it contains sub layouts. Now I've to add
We already have the database with real data inside. Now we want to build
I have added some buttons with the following lines: for (int i=0; i<XML.size(); i++)
I have a LinearLayout view that already contains several elements. I want to add
I'm new to Android and Java but do have some experience in Objective C
I am having difficulties referencing all 3. I have already written my XML,XSD and
i already have a project with TreeNode class which creates a hierachy of nodes
I already have code which lazy loads scripts on request. My issue now is

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.