How to Implement Custom Spinner in Android studio

Arah Lab
0



How to Implement Custom Spinner - XML CODE

Android Studio Java Code Box with Copy Button
     
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity3"
    android:orientation="vertical"
    android:background="#E4EEED"
    >



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:background="@drawable/new_profile2"
        >

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="25dp"
            android:popupBackground="@drawable/bottom_curved1"
            android:transitionName="Jobayer"
            android:entries="@array/dropdown_items"
            android:layout_marginRight="25dp"
            android:background="@drawable/spinnerbac"
            />


   </LinearLayout>
    
   

</LinearLayout>

        


  app<=>res<=>layout<=>New Layout Resource File<=> spinner_dropdown_layout
Android Studio Java Code Box with Copy Button

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Arah Lab"
    android:textSize="20sp"
    android:padding="10dp"
    android:textColor="#FFFFFF"
    >


</TextView>

        


  app<=>res<=>layout<=>New Layout Resource File<=> spinner_dropdown_layout
Android Studio Java Code Box with Copy Button

        
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Arah Lab"
    android:textSize="20sp"
    android:padding="10dp"
    android:textColor="#FFFFFF"
    >

</CheckedTextView>

        


  app<=>res<=>drawable<=>New Drawable Resource File<=> bottom_curved1
Android Studio Java Code Box with Copy Button

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#FF0000" />
            <corners android:radius="5dp" />

            <corners
                android:bottomLeftRadius="20dip"
                android:bottomRightRadius="20dip"
                android:topRightRadius="0dip"
                />

        </shape>
    </item>
    
    
    <item
        android:bottom="3dp"
        android:left="0dp"
        android:right="0dp"
        android:top="3dp">
        
        
        <shape android:shape="rectangle" >
            <solid
                android:color="#151B54"
                />
            <corners
                android:bottomLeftRadius="20dip"
                android:bottomRightRadius="20dip"
                android:topRightRadius="0dip"
                />


        </shape>

        <shape android:shape="rectangle"  >
             <corners android:radius="0dip" />
             <stroke android:width="0dip"
                 android:color="#10000000" />
         </shape>
                 
                     
    </item>

</layer-list>

        


  app<=>res<=>drawable<=>New Drawable Resource File<=> new_profile1
Android Studio Java Code Box with Copy Button

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#FF0000" />
            <corners android:radius="5dp" />

            <corners
                android:bottomLeftRadius="30dip"
                android:bottomRightRadius="0dip"
                android:topRightRadius="30dip"
                />


        </shape>
    </item>


    <item
        android:bottom="3dp"
        android:left="0dp"
        android:right="0dp"
        android:top="3dp">

        <shape android:shape="rectangle" >
            <solid
                android:color="#E4EEED"
                />
            <corners
                android:bottomLeftRadius="30dip"
                android:bottomRightRadius="0dip"
                android:topRightRadius="30dip"
                />


        </shape>

        <shape android:shape="rectangle"  >
            <corners android:radius="0dip" />
            <stroke android:width="2dip"
                android:color="#FF0000" />
        </shape>


    </item>

</layer-list>

        


  How to Implement Custom Spinner - JAVA COD
Android Studio Java Code Box with Copy Button

package com.arah.jobayerahmed;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity3 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    String[] Jubayer = {"Md Jobayer", "Arah Lab", "Tsb Barta"};

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



        Spinner coloredSpinner = findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, R.layout.color_spinner_layout, Jubayer);

        adapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
        coloredSpinner.setAdapter(adapter);
        coloredSpinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView adapterView, View view, int position, long l) {
        Toast.makeText(this, adapterView.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();

        if (position==0){

        }else if (position==1){

        }else {

        }


    }

    @Override
    public void onNothingSelected(AdapterView adapterView) {

    }
}
        



Tags

Post a Comment

0Comments

Post a Comment (0)