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

  • Home
  • SEARCH
  • 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 8526449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:16:33+00:00 2026-06-11T08:16:33+00:00

I have a LinearLayout that contains a TextView, and always will. There will also

  • 0

I have a LinearLayout that contains a TextView, and always will. There will also always be at least one button located below the TextView, but there might be more than one under certain circumstances.

I can successfully create and add as many buttons as I need programmatically. I can also successfully set whatever appearance related parameters/options that I require for these buttons programmatically.

The problem is that I don’t know how to tell a programmatically created button that it should use a XML resource file, which contains the appearance and layout parameters, instead of setting these parameters programmatically.

I’ve looked at similarly named questions and spent time messing with the API itself, to no avail.

Edit:
Here’s an approximation of what I’m trying to do that will hopefully make explanations a bit clearer for me:

private TextView textView;
private SomeObject someObject;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false);
    textView = (TextView) scrollView.findViewById(R.id.game_data_text);
    textView.setText(someObject.getTextForTextView());

    LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container);
    for (String optionText : someObject.getTextForButtons()) {
        layout.addView(createOptionButton(optionText, layout));
    }
    return scrollView;
}

private View createOptionButton(String optionText, LinearLayout layout) {
    Button optionButton = new Button(this.getActivity());
    // set button layout/options here, somehow??
    optionButton.setText(optionText);
    return optionButton;
}

My XML layout file for the fragment looks like this (It’s this LinearLayout that I’m trying to add buttons to):

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/game_data_container"
        etc... >

        <TextView 
           android:id="@+id/game_data_text"
           etc... />

    </LinearLayout>

</ScrollView>

Also, if I’m to create an XML layout file for the button (lets call it custom_button.xml) should it look something like this?:

<?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/play_game_option_button"
        etc... />

Update:
Just to expand a bit on what MrFox@ is talking about, what I did to get it working was replace this line:

Button optionButton = new Button(this.getActivity());

with this one:

Button optionButton = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false);

…which inflates an xml file containing only a Button layout (the button template). In this case, it returns the root view of that file, which is just the button because there’s no parent above the button in the file.

However, if I had have set the last boolean value (attachToParent) to true, it would have returned the root container that the button will be in (which is just the ‘layout’ variable that was passed into the call).

I can now produce as many buttons as I want using this template.

  • 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-11T08:16:35+00:00Added an answer on June 11, 2026 at 8:16 am

    Have you thought of making a layout that is just the button with the applied XML styles and then inflating it into your linear layout?

    something like:

    inflater.inflate(R.layout.StyledButton, MyLinearLayout, true);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is one image on LinearLayout. I have to move that image in any
lets assume i have a LinearLayout , horizontal that contains a TextView and afterward
I have android app in which one layout contains Linearlayout , ScrollView , TextView
I have a Linear Layout that has a Button and a TextView on it.
I have a custom notification that uses a LinearLayout that contains an ImageView and
I have a Tab that has a custom layout which contains a TextView .
I have a table row defined in XML file. It contains 3 `LinearLayout' one
I have a LinearLayout that contains sublayouts which essentially mimick a 3x2 grid. Each
I have a ScrollView that contains a complex LinearLayout with various elements, among which
I have a LinearLayhout that contains a listView and below it an ImageButton .

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.