0

I'm using a Spinner below a title (TextView). It is initially set to View.GONE and when the title is clicked, Spinner is set to View.VISIBLE and the popup window is shown using performClick() below the title, which is what I want.

But I asynchronously update the BaseAdapter to add more items to the Spinner when it is still VISIBLE. After the update the Spinner is moved upwards and is overlaying on the title. How can I fix this?

I have used android:dropDownVerticalOffset, but shows the same behaviour after update.

My layout :

 <LinearLayout 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/some_other_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="@null"
        android:overlapAnchor="true"
        android:spinnerMode="dropdown"
        android:visibility="gone"></android.support.v7.widget.AppCompatSpinner>
</FrameLayout>
</LinearLayout>
10
  • can you put your whole layout? Commented Jul 27, 2017 at 12:56
  • Added the complete layout Commented Jul 27, 2017 at 13:09
  • Try the View.Invisible instead of View.Gone because its taking its space after. This will help you to fix the overlaying problem ;) Commented Jul 27, 2017 at 13:22
  • It shows the same behavíour with View.INVISIBLE. And this happens only after more items are added as in more items to fit the visible screen. Commented Jul 27, 2017 at 13:33
  • unfortunately i cannot test now i dont have the correct IDE atm but what you can try is to make the spinner visible see where its first created to make sure that its located at the correct position because i can see an error inside the framelayout there is an extra linear layout which does nothing basically. Commented Jul 27, 2017 at 13:35

2 Answers 2

0

Ok. I Tried running you problem with some slight adjustments on my mobile, and I find no issues. To simulate your asynchronous addition of items, I added a button.It's OnClick will add items to the adapter. Next, Instead of extending BaseAdapter, I just used an ArrayAdapter. And, I just added weights to the LinearLayout to help me with the layout look.

Here Is The Layout :

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


        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Title"
            android:textSize="20dp"
            android:gravity="center"
            android:textColor="@android:color/black"
        />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >

            <LinearLayout
                android:id="@+id/some_other_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>

            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:background="@null"
                android:overlapAnchor="true"
                android:spinnerMode="dropdown"></android.support.v7.widget.AppCompatSpinner>
        </FrameLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Load Adapter"
        android:id="@+id/button"
    />

</LinearLayout>

And Here Is the Code for the Activity :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    }


    Spinner spinner;
    Button button;

    @Override
    protected void onResume() {
        super.onResume();
        spinner = (Spinner) findViewById(R.id.spinner);
        button = (Button) findViewById(R.id.button);

        List<String> list = new ArrayList<>();
        list.add(getRandomStringInRange('A','Z',5));
        ArrayAdapter<String> adapter= new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter();
                adapter.add(getRandomStringInRange('A','Z',5));
            }
        });

    }
    public int getRandomNumberInRange(int lower,int higher){
        int range = higher-lower;
        range=(int)(range*Math.random());
        return lower + range;
    }

    public String getRandomStringInRange(char lower,char higher,int length){

        String str ="";
        for(int i=0;i<length;i++)
            str+=(char)(getRandomNumberInRange(lower,higher));
        return str;
    }

}

I did not find the spinner overlapping the title or it moving at all.

It's working fine.

If you want,I'll send you screenshots. Please tell me if you are facing any other issues

Sign up to request clarification or add additional context in comments.

Comments

0

I couldn't really find a solution for this. But solved by setting fixed height for the spinner as mentioned in this solution.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
    Field popup = Spinner.class.getDeclaredField("mPopup");
    popup.setAccessible(true);

    // Get private mPopup member variable and try cast to ListPopupWindow
    android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

    // Set popupWindow height to 500px
    popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    // silently fail...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.