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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:39:12+00:00 2026-06-12T21:39:12+00:00

In Android, I have an ArrayList of Sections (there is a Section class so

  • 0

In Android, I have an ArrayList of Sections (there is a Section class so it’s not just an ArrayList of Strings). I would like to represent each Section as a Button. Currently, I am accomplishing this by iterating through each Section, inflating section.xml, and then dynamically adding the properties that vary with each particular Section:

SectionsActivity.java:

public class SectionsActivity extends Activity {

    private int numSections;
    LayoutInflater inflater;

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

        numSections = App.Sections.getSectionList().size();
        inflater = getLayoutInflater();

        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        for (int i = 0; i < numSections; i++) {
            ll.addView(getSectionButton(App.Sections.getSectionList().get(i)));
        }
    }

    public Button getSectionButton(Section s) {
        Button b = (Button) inflater.inflate(R.layout.section, null);
        b.setHint("section" + s.getSectionId());
        b.setText(s.getName());
        b.setTextColor(Color.parseColor("#"+s.getColor()));
        return b;
    }

}

Sections.java:

public class Sections {

    private ArrayList<Section> SectionList;

    public ArrayList<Section> getSectionList() {
        return SectionList;
    }

    public void setSectionList(ArrayList<Section> sectionList) {
        SectionList = sectionList;
    }

}

Section.java:

public class Section {

    private String Color;
    private String Name;
    private int SectionId;

    //constructor, standard getters and setters

}

section.xml:

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

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold" />

This works fine, but I feel like there’s probably a better solution. Here’s an example in .NET for Windows Phone 7: you tell the XAML what you want bound (SectionList, which is an ObservableCollection) and then you give it a template for how each item in the collection should be represented.

<StackPanel Name="StackPanelSection">
    <ListBox Name="ListBoxSection" ItemsSource="{Binding SectionList}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name, Converter={StaticResource StringToLowercaseConverter}}" FontFamily="Segoe WP SemiLight" FontSize="48" Foreground="{Binding HTMLColor}" Tap="TextBlockSection_Tap" />
                </StackPanel>
            </DataTemplate>
         </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

This is better, both in simplicity and in that if you change the contents of SectionList, the UI automatically updates. I’ve read enough about data-binding in Android to know that there’s probably not a true equivalent, but what would be the best way to go about accomplishing the same task? Is there one? Even if data-binding is not a good solution here, is there a different way I should be structuring my Android code?

  • 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-12T21:39:14+00:00Added an answer on June 12, 2026 at 9:39 pm

    You get that binding in XAML because it’s baked into the framework. Your app executes in a runtime environment that supports the binding lookup, so you get a whole binding framework from Microsoft as part of the toolset. Android just doesn’t have this kind of thing.

    I don’t know of a way to do your binding in a declarative way like you would in XAML, but being in a similar boat (coming from WPF/.Net/XAML background into Android), I’ve found creative ways of just making it more convenient. Looks like you’re well along in that path. I make use of custom adapters for any lists or grids I use which give a similar convenience…not AS convenient as xaml binding, but still pretty cool.

    I haven’t seen your UI so I can only make assumptions from your code, but I can only assume that what you’re doing (Buttons inside a LinearLayout) can be accomplished with a ListView and a custom adapter. Probably the most classic Android developer video is World of ListView from a past Google I/O. It’s a few years old, but still a great watch and still relevant.

    https://www.youtube.com/watch?v=wDBM6wVEO70

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

Sidebar

Related Questions

I have an android app, where there are couple of activities. Each of the
I have an ArrayList of custom, simple Serializable objects I would like to cache
I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how
I'm just getting my feet wet with Android and have built a UI that
I've a class Persona with: String name; Int age; And I have an ArrayList<Persona>
I have an ArrayList which holds Planes (enemies) on my android game. These planes
I have a class called PlayerList, in in that class i have an ArrayList
I have created an ArrayList in my Application class : package a.b.layout; import java.util.ArrayList;
I have an android app posting to my sinatra service. Earlier I was not
I have created a ListView with Arraylist as below: nAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_checked,nArrayList); mListView.setAdapter(nAdapter); then

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.