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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:10:44+00:00 2026-06-10T09:10:44+00:00

ok so im trying to add a button/view to a new activity other than

  • 0

ok so im trying to add a button/view to a new activity other than the one the Viewgroup is already in.
here is my code so far:

Src:

public class Main extends Activity {
    Button btn, btn1;
    LayoutInflater linflater;
    LinearLayout l;
    static int pos = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button  ButtonBack = (Button) findViewById(R.id.dynamicoption);
        ButtonBack.setOnClickListener(new View.OnClickListener(){

              public void onClick(View v) {
                  // Perform action on click

                startActivity(new Intent("com.nour.ImamKhomeini.DYNAMICOPTION"));


              }

          });

        btn = (Button) findViewById(R.id.Button01);

        l = (LinearLayout) findViewById(R.id.LinearLayout01);
        linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              View myView = linflater.inflate(R.layout.dynamicoption, l, true);
              myView.setId(pos);
              pos++;
          }
        });

        btn = (Button) findViewById(R.id.Button01);
        btn1 = (Button) findViewById(R.id.Button02);
        l = (LinearLayout) findViewById(R.id.LinearLayout01);
        linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        btn.setOnClickListener(new myListener());
        btn1.setOnClickListener(new myListener1());
    }

    class myListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            View myView = linflater.inflate(R.layout.dynamicoption, null);
            myView.setId(pos);
            pos++;
            l.addView(myView);
        }
    }

    class myListener1 implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (pos != 0) {
                pos--;
                View myView = l.findViewById(pos);
                l.removeView(myView);
            }
        }
    }
}

And here are my xmls:

Main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal">

    <Button android:id="@+id/Button01" android:background="@drawable/ic_launcher"
        android:layout_height="45dip" android:layout_width="45dip"></Button>

    <Button android:id="@+id/Button02" android:background="@drawable/ic_launcher"
        android:layout_height="45dip" android:layout_width="45dip"></Button>

    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>
</LinearLayout>

dynamicoption.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    <Button android:text="@+id/Button01" android:id="@+id/Button01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

Any Help is appreciated.
Thank you.

  • 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-10T09:10:46+00:00Added an answer on June 10, 2026 at 9:10 am

    I would make a couple changes. Look how I create OnClickListener, you don’t need to create a new subclass. And notice how I use the layout inflater:

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            View myView = linflater.inflate(R.layout.dynamicoption, l, true);
            myView.setId(pos);
            pos++;
        }
    });
    

    Also when XML opening and closing tags don’t have anything in between them:

    <EditText 
        android:id="@+id/EditText01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@+id/EditText01" ></EditText>
    

    You can use the /> shorthand and save yourself some typing

    <EditText 
        android:id="@+id/EditText01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@+id/EditText01" />
    

    Here is what your onCreate() method should look like:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Button ButtonBack = (Button) findViewById(R.id.dynamicoption);
        ButtonBack.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // Perform action on click
                startActivity(new Intent("com.nour.ImamKhomeini.DYNAMICOPTION"));
            }
        });
    
        l = (LinearLayout) findViewById(R.id.LinearLayout01);
        linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                View myView = linflater.inflate(R.layout.dynamicoption, l, true);
                myView.setId(pos);
                pos++;
            }
        });
    
        btn1 = (Button) findViewById(R.id.Button02);
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos > 0) {
                    pos--;
                    l.removeView(l.findViewById(pos));
                }
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add Become-a-Fan button to my 2008 asp.net website (vb) Here
I'm trying to add an AirPlay button to my app and used the following
I am trying to add a pinterest button to a control. I am trying
I'm trying to add a custom button to the button panel of jQuery's datepicker.
I am trying to add a transparent button bars at the top and bottom
I am trying to add bootstrap drop down button in a column of jqGrid
I'm trying to add the Twitter Share button to my website. I've gone to
I have been trying to add an askquestion dialog box to a delete button
I am trying to set up an add-tab button in a Gtk::Notebook (gtkmm). I
I am trying to add a simple log in with Facebook button to my

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.