Open top menu


 In this tutorial Implement multiple Choice Custom Spinner in android. explain Briefly..

Step1 create gradient xml inside res/drawable folder..

text_cover.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" 
android:padding="7dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>

<stroke android:width="2dip" 
android:color="#dce0e2" />  
</shape>

...................................................................................................................................................................

Step 2 Create activity_main xml inside res/layout folder..
activity_main.xml.
...................................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
 android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="@drawable/register_cover"    
android:orientation="vertical"   
android:padding="10dp">


<ImageView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:id="@+id/imageView"        
android:layout_gravity="center"        
android:src="@drawable/my_heder_img"/>
<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:layout_margin="4dp"        
android:orientation="horizontal"        
android:weightSum="2">
    
</LinearLayout>

<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:layout_margin="4dp"        
android:gravity="center_horizontal"        
android:orientation="horizontal"        
android:weightSum="2">
        
<TextView            

android:layout_width="150dp"           
android:layout_height="48dp"
android:layout_weight="1"            
android:gravity="center_vertical"            
android:text="Select Item" />

        
<LinearLayout            
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:layout_margin="4dp"            
android:layout_weight="1"            
android:gravity="center_horizontal"            
android:orientation="vertical"            
android:weightSum="2">

            
<TextView                
android:id="@+id/textView44"                
android:layout_width="fill_parent"                
android:layout_height="50dp"                
android:text=""                
android:background="@drawable/edit_text_gradient"                
android:drawableRight="@drawable/short_dwon"                
android:gravity="center_vertical"                
android:paddingLeft="18dp"                
android:textSize="18dp" />
            
<LinearLayout                
android:orientation="vertical"                
android:layout_width="match_parent"                
android:layout_height="match_parent"                
android:id="@+id/layout"                
android:background="@drawable/edit_text_gradient"                
android:visibility="invisible"                 >
            
<ListView                
android:id="@+id/spinAddGuest"                
android:layout_width="match_parent"               
android:layout_height="250dp"                
android:visibility="invisible" />

            
<Button                
android:layout_width="match_parent"                
android:layout_height="wrap_content"                
android:text="Ok"               
 android:id="@+id/button"               
 android:visibility="invisible"                
android:layout_margin="7dp"                
android:background="@drawable/register_cover"                
android:layout_gravity="center_horizontal" />
            
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

...................................................................................................................................................................

Step 3 Create MainActivity inside srcfolder.
MainActivity.Java.
...................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.app.ActionBar;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends Activity {
    ArrayList<String> item_array;
    String specialAssistence;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar();

        setContentView(R.layout.activity_main);
        item_array = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            item_array.add("Item" + i);
        }

        final ListView spinCount = (ListView) findViewById(R.id.spinAddGuest);
        final TextView text = (TextView) findViewById(R.id.textView44);
        final Button okbtn = (Button) findViewById(R.id.button);
        final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

        text.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                spinCount.setVisibility(View.VISIBLE);
                okbtn.setVisibility(View.VISIBLE);
                layout.setVisibility(View.VISIBLE);
            }
        });
        ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, item_array);
        spinCount.setAdapter(spinnerArrayAdapter);
        spinCount.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        spinCount.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        okbtn.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                SparseBooleanArray positions = spinCount.getCheckedItemPositions();
                int j = 0;
                for (int k = 0; k < item_array.size(); k++) {
                    if (positions.get(k)) {
                        String str = item_array.get(k);


                            specialAssistence = specialAssistence + ",\t" + str;

                    }
                }
                if(!specialAssistence.isEmpty()) {
                    text.setText(specialAssistence.substring(5).trim());
                }
                spinCount.setVisibility(View.INVISIBLE);
                okbtn.setVisibility(View.INVISIBLE);
                layout.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void ActionBar() {
        final ActionBar abar = getActionBar();

        abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_image));

        View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_title, null);
       /*        * Center the textview in the ActionBar !        */
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);

        textviewTitle.setText("Custom Spinner");
        abar.setCustomView(viewActionBar, params);
        abar.setDisplayShowCustomEnabled(true);
        abar.setDisplayShowTitleEnabled(false);
        abar.setDisplayHomeAsUpEnabled(true);
        abar.setHomeButtonEnabled(true);
    }
}


...................................................................................................................................................................


try this...










0 comments