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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:08:56+00:00 2026-05-20T17:08:56+00:00

I have a bug with my activity. I have three view stubs in my

  • 0

I have a bug with my activity.

I have three view stubs in my linear layout like so –

<ViewStub 
 android:id="@+id/index_1"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
<ViewStub 
 android:id="@+id/index_2"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
<ViewStub 
 android:id="@+id/index_3"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />

my onCreate conditionally checks what to inflate:

for (int i = 0; i < 3; i++) {
    int id = convertIndexToId(i); //will turn i into R.id.index_1
    ViewStub stub = findViewById(id);
    if (bShouldBeSpinner) {
        stub.setLayoutResource(R.layout.index_spinner);
        View root = stub.inflate();
        Spinner spin = (Spinner)root.findViewById(R.id.my_spinner);
        spinner.setAdapter(adapter);
        spinner.setSelection(0);
    }
    else {
        stub.setLayoutResource(R.layout.index_edittext);
        View root = stub.inflate();
        EditText et = (EditText)root.findViewById(R.id.my_edittext);
        //et.phoneHome(); just kidding
        et.setText(String.valueOf(System.currentTimeMillis()));
    }
}

I force bShouldBeSpinner to false. The output of the edittext’s is as follows:
1300373517172
1300373517192
1300373517221

However, when I rotate the screen and onCreate is called a second time the output is this:
1300373517221
1300373517221
1300373517221

Initially that made me think you should only inflate the view once, and the heirarchy is kept inbetween onCreate’s… however when i only run it the first time the second time no views are shown for the stubs.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Spinner style="@style/SearchInput" android:id="@+id/my_spinner" />
</LinearLayout>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText style="@style/SearchInput" android:id="@+id/my_edittext" />
</LinearLayout>

I feel the documentation is assuming something that I did not notice or am missing. Does anyone see what I am doing wrong?

EDIT

I added to the view stubs android:inflatedId=”index_1_root”… etc

it is the strangest thing, when I add these lines after the for loop:

EditText v = indexRoot1.findViewById(R.id.index_edit_text);
Log.d(TAG, "EditTExt: " + v);

EditText v2 = indexRoot2.findViewById(R.id.index_edit_text);
Log.d(TAG, "EditTExt: " + v2);

the output says (I believe) they are references to different EditTexts.

EditTExt: android.widget.EditText@47210fe8
EditTExt: android.widget.EditText@47212ba8

So they are getting inflated again, but the text is set to what the last edittext was set to on the first pass.

  • 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-20T17:08:56+00:00Added an answer on May 20, 2026 at 5:08 pm

    Instead of using ViewStubs, I added an id to the root of those stubs (android:id=”index_roots”) and used

    view.addView( (isSpinner) ? 
        new Spinner(this) : new EditText(this) ); 
    

    to fix this problem, I will however not accept this answer right away, I’ll allow others to answer using the method I was going for.

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

Sidebar

Related Questions

I have an activity using an xml layout where a WebView is embedded. I
I am a beginner with Android, and I have a strange bug with the
I want to make Activity like this using GridView. And this Activity have only
i have bug that i cannot find, i have Class Point with method who
I have a bug I'm struggling to track down. I believe what's happening is
We have a bug in our application that does not occur every time and
We have this bug that only appears 30% of the time for the Release
Hello I have a bug in my app and I cannot figure it out.
A bit of a newbie question on rails associations. I have a Bug model,
I have a strange bug. I launch a PHP Unit test Suite. At the

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.