Pages

Wednesday, 4 January 2012

Spinner with XML in Android


Spinner Using XML in Android






Complete CODE:


Activity Code:





public class ApplicationSetting<View> extends Activity {

     String[] presidents = {
                "Async Task",
                "Flipper",
              
        };
   
        Spinner s1;
        TextView tv;
        @Override   
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.application_setting);
   
            s1 = (Spinner) findViewById(R.id.spinner1);
            tv=(TextView)findViewById(R.id.userName);
            tv.setText("http://192.168.2.175:8080/ezeeMobileServer/seam/resource/rest/hotelLists/byChain?chainId=1");
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, presidents);
   
            s1.setAdapter(adapter);
            s1.setOnItemSelectedListener(new OnItemSelectedListener()
            {
                public void onItemSelected(AdapterView<?> arg0,
                View arg1, int arg2, long arg3)
                {
                    int index = s1.getSelectedItemPosition();
                    Toast.makeText(getBaseContext(),
                        "You have selected item : " + presidents[index],
                        Toast.LENGTH_SHORT).show();               
                }
   
                public void onNothingSelected(AdapterView<?> arg0) {}

                @Override
                public void onItemSelected(AdapterView<?> arg0,
                        android.view.View arg1, int arg2, long arg3) {
                    // TODO Auto-generated method stub
                   
                }
            });
        }

   

}
 







XML Code:




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@layout/summarylist_logo_titlebar_custemshape" android:orientation="vertical"
    android:layout_height="fill_parent">
   
   
    <ImageView android:layout_width="wrap_content"
        android:gravity="center"
        android:paddingTop="6dip"
        android:paddingBottom="4dip"
        android:paddingLeft="100dip"
        android:src="@drawable/lo_g_o"
        android:layout_height="wrap_content" />
       
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:paddingTop="100dip"
        android:background="@layout/summarylist_backgroundstyle_customshape"
        android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                 android:paddingTop="6dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:background="@layout/summarylist_backgroundstyle_customshape"
                android:layout_height="wrap_content">
       
       
       
            <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                 android:paddingTop="6dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:background="@layout/summarylist_backgroundstyle_customshape"
                android:layout_height="wrap_content">   
       
       
       
         <TableLayout android:layout_width="fill_parent"
                         android:orientation="vertical"
                         android:gravity="center_horizontal"
                         android:layout_height="wrap_content" 
                         android:stretchColumns="*"
                         android:background="@layout/login_tablelayout_borderline">
                        
                <TableLayout android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:gravity="center_horizontal"
                    android:background="@layout/login_table_background"
                    android:layout_height="wrap_content" android:padding="5dip">
        <TableRow android:paddingTop="25dip">
       
                <TextView
           
             android:id="@+id/userNameText"  
             android:layout_height="fill_parent"
            android:textColor="#e7a71c"
            android:textSize="7pt" 
            android:textStyle="bold"
            android:paddingLeft="8dip"
            android:text="Sync Url:"
          
          android:layout_width="100px"                                                                                              
            android:gravity="left"
            />
       
        <EditText
            android:id="@+id/userName"
           
            android:layout_width="160px"
            android:layout_height="40px"
         
            android:singleLine="True"></EditText>
       
       
        </TableRow>
       
        <TableRow>
       
                <TextView
           
            android:id="@+id/PasswordText"  
             android:layout_height="fill_parent"
            android:textColor="#e7a71c"
            android:textSize="7pt" 
            android:textStyle="bold"
            android:paddingLeft="8dip"
            android:text="Sync:"
          
          android:layout_width="100px"                                                                                            
            android:gravity="left"
            />
       
   
         <Spinner
        android:id="@+id/spinner1"
       android:layout_width="160px"
            android:layout_height="40px"
        android:drawSelectorOnTop="true" />      
       
       
        </TableRow>
       
   
    </TableLayout>
        </TableLayout>
       

</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>

No comments:

Post a Comment