Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with Android spinner in 2.2 related to layout arrangement

Below is a link to the bug I am experiencing with my Android application. Rather than trying to explain it via a huge wall of text, I figured a simple video would be much more direct and easier to understand.

http://www.youtube.com/watch?v=9V3v854894g

I've been beating my head against a wall on this problem for a day and a half now. I only found that it could be solved by changing the XML layout just recently which makes absolutely no sense to me. I have no idea how to properly fix it, or a way to band-aid the problem since I need the nested layouts in my application.

Thank you everyone for all your help!

Here is the code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class Builder extends Activity {
    private Spinner mCompSelect;
    private Spinner mNameSelect;
    private int[] mCompColorAsBuilt;
    private int mComponent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.builder);

        mCompColorAsBuilt = new int[3];

        //Attach our objects
        mCompSelect = (Spinner) findViewById(R.id.component);
        mNameSelect = (Spinner) findViewById(R.id.component_name);

        //Attach an adapter to the top spinner
        ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(this, R.array.cc_components, android.R.layout.simple_spinner_item);
        a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCompSelect.setAdapter(a);
        //Create a listener when the top spinner is clicked
        mCompSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Save the position
                mComponent = position;
                //Create a new adapter to attach to the bottom spinner based on the position of the top spinner
                int resourceId = Builder.this.getResources().getIdentifier("component"+Integer.toString(mComponent)+"_color", "array", Builder.this.getPackageName());      
                ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(Builder.this, resourceId, android.R.layout.simple_spinner_item);
                a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mNameSelect.setAdapter(a);
                //Set the position of the bottom spinner to the saved position
                mNameSelect.setSelection(mCompColorAsBuilt[mComponent]);
            }
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //Attach an adapter to the bottom spinner
        int resourceId = this.getResources().getIdentifier("component"+Integer.toString(mComponent)+"_color", "array", this.getPackageName());      
        ArrayAdapter<CharSequence> b = ArrayAdapter.createFromResource(this, resourceId, android.R.layout.simple_spinner_item);
        b.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mNameSelect.setAdapter(b);
        mNameSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {       
                //Save the position of the bottom spinner
                mCompColorAsBuilt[mComponent] = position;
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner
        android:id="@+id/component"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/finish"
        android:drawSelectorOnTop="true"
        android:prompt="@string/component_spinner" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Spinner
            android:id="@+id/component_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawSelectorOnTop="true"
            android:prompt="@string/component_name_spinner" />
    </LinearLayout>
</RelativeLayout>
like image 217
user432209 Avatar asked Nov 15 '22 04:11

user432209


1 Answers

As a hack, try calling invalidate() on the affected Spinner. First, try after you call setSelection(). If that fails, try using postDelayed() on the Spinner to call invalidate() a bit later (e.g., 50ms).

In addition, I encourage you to create a demonstration project with two activities (or maybe just the one activity with two layouts) that illustrates this behavior, and post it and an explanation to http://b.android.com.

like image 105
CommonsWare Avatar answered Dec 09 '22 18:12

CommonsWare