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>
quad
  • 872
  • 3
  • 17
  • 37
  • can you put your whole layout? – Yamen Nassif Jul 27 '17 at 12:56
  • Added the complete layout – quad Jul 27 '17 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 ;) – Yamen Nassif Jul 27 '17 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. – quad Jul 27 '17 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. – Yamen Nassif Jul 27 '17 at 13:35
  • just try to move the spinner inside that layout and make it visible at the start to see its first location before doing any thing to it. if the location was correct and after updating it got moved somewhere else then its something wrong with the Java code not the layout it self – Yamen Nassif Jul 27 '17 at 13:36
  • The linear layout below the title is always fixed. There is more content inside which I did not add. When the title is clicked I overlay the Spinner on top of this fixed LinearLayout and then hide the Spinner when title clicked again or back press for example. – quad Jul 27 '17 at 13:40
  • The location is right initially when set to visible (below the title). Only when a new item is added and that new item cannot be completely shown in the list adapter, the pop up scrolls up, to show the new item fully. Maybe be something to do with the adapter. – quad Jul 27 '17 at 13:41
  • sadly i cannot test this right now will check it later for sure – Yamen Nassif Jul 27 '17 at 13:42
  • Thanks for your help so far. – quad Jul 27 '17 at 13:43

2 Answers2

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

akash rao
  • 59
  • 1
  • 4
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...
}
quad
  • 872
  • 3
  • 17
  • 37