Wednesday, August 6, 2014

Android : spinner sliding dropdown example

Android : spinner sliding dropdown example
This post about how to show spinner item like sliding view.
i have created two views. Button and ListView .
listview.xml


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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="80dip"
        android:background="@android:color/white"
        android:scrollbars="none"
        android:scrollingCache="false"
        android:visibility="gone" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/dropdownbg"
        android:text="Select Value" />

</RelativeLayout>









In activity adding transition for spinner onclick down and up.



package com.iamvijayakumar.dropdown;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener {

       ListView listView;
       Button btn;
       ArrayList<String> list = new ArrayList<String>();
       boolean isClicked = true;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.listview);
              listView = (ListView) findViewById(R.id.listView1);
              btn = (Button) findViewById(R.id.button1);
              list.add("1");
              list.add("2");
              list.add("3");
              list.add("4");
              list.add("5");
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, android.R.id.text1, list);
              listView.setAdapter(adapter);

              btn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           if (isClicked) {

                                  listView.setVisibility(View.VISIBLE);
                                  TranslateAnimation slide = new TranslateAnimation(0, 0,
                                                -1500, 0);
                                  slide.setDuration(2300);
                                  slide.setFillAfter(false);
                                  listView.startAnimation(slide);
                                  isClicked = false;
                           } else {

                                  TranslateAnimation slide = new TranslateAnimation(0, 0, 0,
                                                -4500);
                                  isClicked = true;
                                  slide.setDuration(4500);

                                  slide.setFillAfter(false);

                                  listView.startAnimation(slide);
                                  listView.setVisibility(View.GONE);
                           }
                     }
              });

              listView.setOnItemClickListener(new OnItemClickListener() {

                     @Override
                     public void onItemClick(AdapterView<?> parent, View view,
                                  int position, long id) {
                           // TODO Auto-generated method stub
                           btn.setText(list.get(position));
                           TranslateAnimation slide = new TranslateAnimation(0, 0, 0,
                                         -4500);

                           slide.setDuration(4500);

                           slide.setFillAfter(false);

                           listView.startAnimation(slide);
                           listView.setVisibility(View.GONE);
                     }
              });

       }

       @Override
       public void onClick(View v) {

       }
}






No comments:

Post a Comment

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...