Spinner Position der Dropdown Liste

  • 4 Antworten
  • Letztes Antwortdatum
H

haner

Ambitioniertes Mitglied
0
Hallo,
ich habe meiner App einen Spinner hinzugefügt. Die Position des Spinners selbst passt. Er befindet sich untehalb eines Buttons und oberhalb eines editText-Felds.
Bei Anklicken des Spinners öffnet sich die Dropdownliste, aber leider nicht an Position des Spinners, sondern am oberen Bildschirmrand. Wie kann man das einstellen, dass sich die Drodown-Liste direkt an Position des Spinners öffnet, bzw. dort mit dem ersten Eintrag beginnt?

Hier der aktuelle relevante Code:

AnghinzuActivity.java
Code:
public class AnghinzuActivity extends AppCompatActivity implements View.OnClickListener {
    
    ArrayList<String> listItems = new ArrayList<>();
    ArrayAdapter<String> adapter;
    HttpURLConnection urlConnection = null;

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

        Spinner sp = (Spinner) findViewById(R.id.sp_kategorie);
        adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, listItems);//adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, listItems);
        sp.setAdapter(adapter);

        protected void onStart(){
        super.onStart();
        BackTask bt = new BackTask();
        bt.execute();
        }

        private class BackTask extends AsyncTask<Void, Void, Void>{
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
            list = new ArrayList<>();
        }

        protected Void doInBackground(Void...params){
            InputStream is = null;
            String result="";
            try{

                URL url = new URL("*********************");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                is = urlConnection.getInputStream();           

            }catch (Exception e) {
                e.printStackTrace();
            }
            // Convert the response string
            try{
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line ="";
                while((line = bufferedReader.readLine())!=null){
                    result+=line;
                }
                is.close();
            }catch(IOException e){
                e.printStackTrace();
            }
            // Parse json string
            try{
                JSONArray jsonArray = new JSONArray(result);
                for(int i=0; i<jsonArray.length();i++){
                    JSONObject jsonObject=jsonArray
                            .getJSONObject(i);
                    list.add(jsonObject.getString("Kategorie"));//iname
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute (Void rsult){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }

    }
}


spinner_layout.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

</android.support.constraint.ConstraintLayout>


activity_anghinzu.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.ta.mar.appangebote.AnghinzuActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageButton
            android:id="@+id/btnChoose"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toTopOf="@+id/btnFotoanzeigen"
            app:layout_constraintEnd_toStartOf="@+id/btnFotoupload"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_gallery_80" />

        <ImageView
            android:id="@+id/ivFoto"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintBottom_toTopOf="@+id/btnFotoanzeigen"
            app:layout_constraintEnd_toStartOf="@+id/btnFotoupload"
            app:layout_constraintStart_toEndOf="@+id/btnChoose"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@android:drawable/screen_background_light_transparent" />

        <ImageButton
            android:id="@+id/btnFotoupload"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_upload_80" />


        <Button
            android:id="@+id/btnFotoanzeigen"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="Foto anzeigen"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnFotoupload" />

        <Spinner
            android:id="@+id/sp_kategorie"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnFotoanzeigen" />

        <EditText
            android:id="@+id/etKategorie"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:ems="10"
            android:hint="Kategorie"
            android:inputType="textPersonName"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sp_kategorie
    </android.support.constraint.ConstraintLayout>
</ScrollView>" />
 
Hast du einen Screenshot? Spontan würde ich sagen dass es an deinem ConstaintLayout vom Spinner liegt. Dort ist layout_height auf match_parent gesetzt.
Ich würde dort etwas einfacheres verwenden. Z.B LinearLayout und beide Dimensionen auf wrap_content setzen.
 
  • Danke
Reaktionen: swa00
Verwende nun ein LinearLayout in der spinner_layout.xml. Leider hat sich dadurch nichts verändert. Das Problem besteht weiterhin.

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />
 
</LinearLayout>

Allerdings habe ich nun genauere Informationen. Bei Öffnen der AnghinzuActivity.java ist sofort die Tastatur geöffnet und der Curser befindet sich im, auch auf der Seite vorhandenen, editText-Feld. Klickt man dann den Spinner an, erscheint die Dropdown-Liste am oberen Bildschirmrand. Drückt man zuvor die Zurücktaste verschwindet die Tastatur und bei anschließendem Klick auf den Spinner ist die Dropdown-Liste korrekt positioniert.

Screenshot_20180106-111903.png Screenshot_20180106-111915.png
 
Dann liegt es daran. Das System versucht die Anzeige zu optimieren wenn die Tastatur aktiv ist. Da wirst du dann nicht viel drehen können und es ist ja auch ok.
Du könntest noch mit den adjust* Werten für den softInputMode rumspielen. <activity> | Android Developers
 
  • Danke
Reaktionen: Kardroid und swa00
Danke für den Tipp. Jetzt funktioniert es.
Habe es hiermit gelöst:
Code:
<activity android:windowSoftInputMode="stateHidden" />
 
Zurück
Oben Unten