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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:46:55+00:00 2026-05-23T18:46:55+00:00

I have a problem with getting the child’s position of LinearLayout . First I’m

  • 0

I have a problem with getting the child’s position of LinearLayout. First I’m adding dynamically a number of buttons and then I’m trying to return each child’s index and display it into a TextView. Here I’m sharing code:

java source:

private String[] categories;

private LinearLayout ll;
private TextView tv;

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

    categories = getResources().getStringArray(R.array.categories);

    tv = (TextView) findViewById(R.id.text);
    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        Button btn = new Button(this);
        btn.setText(categories[i]);
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        tv.setText(ll.indexOfChild(v));
    }
};

xml structure:

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

<HorizontalScrollView
    android:id="@+id/Footer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:fadingEdge="none"
    >

    <LinearLayout
        android:id="@+id/hsvLinearLayout"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </LinearLayout>

</HorizontalScrollView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

resources:

<string name="today">Today</string>
<string name="life">Life</string>
<string name="corner">Corner</string>
<string name="banks">Banks</string>
<string name="it">IT</string>
<string name="fun">Fun</string>

<array name="categories">
    <item>@string/today</item>
    <item>@string/life</item>
    <item>@string/corner</item>
    <item>@string/banks</item>
    <item>@string/it</item>
    <item>@string/fun</item>
</array>

The dynamically adding is fine, but the way I’m setting the OnClickListener produce some error. Any help will be useful! The purpose of that is if I want to add one or more button(s) to the HorizontalScrollView not to be necessary to edit many files, and just to go to sting.xml and create a new item into the categories-array!

This is what LogCat produced:

07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle...
07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441)
07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms)
07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms

To clarify – the application starts without any errors, but when I click on a button it produces this line from the above log:

07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000

And this is from debugger:

TestDynam [Android Application] 
    DalvikVM[localhost:8611]    
        Thread [<3> main] (Suspended (exception Resources$NotFoundException))   
            ViewRoot.handleMessage(Message) line: 1704  
            ViewRoot(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4203    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 791  
            ZygoteInit.main(String[]) line: 549 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<13> Binder Thread #2] (Running)    
        Thread [<11> Binder Thread #1] (Running)
  • 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-05-23T18:46:57+00:00Added an answer on May 23, 2026 at 6:46 pm

    your problem is resources….
    you use the setText(int) method (that index is int…) which looks for resources and not string, this is not StringBuilder for which you can throw any type and get a string.
    you needs to replace

    tv.setText(ll.indexOfChild(v));

    with

    tv.setText(Integer.toString(ll.indexOfChild(v)));

    and if you want even little bit of efficiency:

    public class TestActivity extends Activity {
    
        private String[] categories;
    
        private LinearLayout ll;
        private TextView tv;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            categories = getResources().getStringArray(R.array.categories);
    
            tv = (TextView) findViewById(R.id.text);
            ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);
    
            for(int i = 0; i < categories.length; i++) {
                Button btn = new Button(this);
                btn.setText(categories[i]);
                btn.setOnClickListener(buttonClick);
                ll.addView(btn);
                int idx = ll.indexOfChild(btn);
                btn.setTag(Integer.toString(idx));
            }
        }
    
        OnClickListener buttonClick = new OnClickListener() {
            public void onClick(View v) {
                String idxStr = (String)v.getTag();
                tv.setText(idxStr);
            }
        };
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have using Silverlight and Caliburn Micro and am having a problem getting child
I have a problem getting the right Price for a product based on Effectivity
I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the
I have a problem regarding getting the path of a user control. The scenario
I have a problem with getting my pager-function to work. For some reason it
I have a problem with getting jquery to retrieve results from a WCF service.
I seem to have a problem with getting MVC to fill in my custom
I have a problem where a method is getting an undefined variable error, even
I have a problem while scrolling images on tableview. I am getting a Signal
I currently have a problem with my jgroups configuration, causing thousands of messages getting

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.