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>

Android Google Map Example

Android Google Map ExampleDOWNLOAD SOURCE CODE
       1.To integrate with Google maps application you need to apply for a free Google Map API Key.
       2.To get the Google Map API Key you need to sign up for the Android Maps API.
       3.To sign up, you will need to provide the certificate's fingerprint (MD5).
       4.If you have a certificate fingerprint (MD5)you can sign up here
     and get the API Key.


To get certificate fingerprint (MD5) follow the simple steps below:
  • You need to get the keystore file for getting the certificate fingerprint (MD5).
  • Your keystore file can be found at the following path
"C:\Documents and Settings\<username>\Local Settings\Application Data\Android"
(Or)
"C:\Documents and Settings\<username>\.android"




 SOURCE CODE
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="056mRQ2iioMKqXlXQ9O4PdhQgQhpb4QZ89_jQsw"/>
</RelativeLayout>


you have to add apikey in main.xml




and in android manifest file you have to add this line inside application tag

  <uses-library android:name="com.google.android.maps" />  




Android Interview Questions and Answers (Basic)


What is Android?
Android is a software stack for mobile devices that includes an operating system, middleware and key applications.
Explain the Architecture of android ?
Top -> Applications (Contacts, Browser, Phone, etc)
Below Applications -> Application Framework(Activity Manager, Window Manager, Content Providers, View System, Package manager,
Telephony manager, Resource, Notification, Location managers)
Below Application Framework -> System Libraries(Like Sqlite, webkit, SSL, OpenGL, Media Framework etc) & Android Runtime( Core Libraries and DVM)
Atlast Last -> Linux Kernel (which composed of drivers like display, camera etc.)
Describe the APK format.
The APK file is compressed the AndroidManifest.xml file, application code (.dex files), resource files, and other files.
A project is compiled into a single .apk file.
What is an action?
A description of something that an Intent sender desires.
What is an activity?
A single screen in an application, with supporting Java code.
An activity presents a visual user interface for one focused endeavor the user can undertake.
For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions.
Each one is implemented as a subclass of the Activity base class.
What is a service?
A service doesn’t have a visual user interface, but rather runs in the background for an indefinite period of time.
For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate
something and provide the result to activities that need it.
Each service extends the Service base class.
What is a Broadcast receivers?
A broadcast receiver is a component that does nothing but receive and react to broadcast announcements.
For example, announcements that the timezone has changed, that the battery is low or that the user changed a language preference.
All receivers extend the BroadcastReceiver base class.
Broadcast receivers do not display a user interface. However, they may start an activity in response to the information they receive,
or they may use the NotificationManager to alert the user like(flashing the backlight, vibrating the device, playing a sound)
What is a content provider?
A content provider makes a specific set of the application’s data available to other applications.The content provider extends the ContentProvider
base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls.
However, applications do not call these methods directly. Rather they use a ContentResolver object and call its methods instead.
What is intent?
A class (Intent) describes what a caller desires to do. The caller sends this intent to Android’s intent resolver, which finds the most suitable activity for the intent.
How is nine-patch image different from a regular bitmap?
It is a resizable bitmap resource that can be used for backgrounds or other images on the device. The NinePatch class permits drawing a bitmap in nine sections. The four corners are unscaled; the four edges are scaled in one axis, and the middle is scaled in both axes.
What languages does Android support for application development?
Android applications are written using the Java programming language.
What is a resource?
A user-supplied XML, bitmap, or other file, injected into the application build process, which can later be loaded from code.
What Virtual Machine Android runs on?
Dalvik virtual machine
Android Latest Version?
Android 3.0
How do you define the user interface?
XML Format is the best.

Friday, 30 December 2011

Android-Wheel: http://code.google.com/p/android-wheel/
It comes with a handy ScrollListener for listen to touch events on the wheel component. 

source code 

package com.vijay;


import com.vijay.wheel.ArrayWheelAdapter;
import com.vijay.wheel.OnWheelChangedListener;
import com.vijay.wheel.OnWheelScrollListener;
import com.vijay.wheel.WheelView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity
    {
        // TODO: Externalize string-array
        String wheelMenu1[] = new String[]{"name 1", "name 2", "name 3", "name 4", "name 5", "name 6","name 7","name 8","name 9"};
        String wheelMenu2[] = new String[]{"age 1", "age 2", "age 3"};
        String wheelMenu3[] = new String[]{"10", "20","30","40","50","60"};

        // Wheel scrolled flag
        private boolean wheelScrolled = false;

        private TextView text;
        private EditText text1;
        private EditText text2;
        private EditText text3;

        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                initWheel1(R.id.p1);
                initWheel2(R.id.p2);
                initWheel3(R.id.p3);

                text1 = (EditText) this.findViewById(R.id.r1);
                text2 = (EditText) this.findViewById(R.id.r2);
                text3 = (EditText) this.findViewById(R.id.r3);
                text = (TextView) this.findViewById(R.id.result);
            }

        // Wheel scrolled listener
        OnWheelScrollListener scrolledListener = new OnWheelScrollListener()
            {
                public void onScrollStarts(WheelView wheel)
                    {
                        wheelScrolled = true;
                    }

                public void onScrollEnds(WheelView wheel)
                    {
                        wheelScrolled = false;
                        updateStatus();
                    }
            };

        // Wheel changed listener
        private final OnWheelChangedListener changedListener = new OnWheelChangedListener()
            {
                public void onChanged(WheelView wheel, int oldValue, int newValue)
                    {
                        if (!wheelScrolled)
                            {
                                updateStatus();
                            }
                    }
            };

        /**
         * Updates entered PIN status
         */
        private void updateStatus()
            {
                text1.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()]);
                text2.setText(wheelMenu2[getWheel(R.id.p2).getCurrentItem()]);
                text3.setText(wheelMenu3[getWheel(R.id.p3).getCurrentItem()]);

                text.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()] + " - " + wheelMenu2[getWheel(R.id.p2).getCurrentItem()] + " - " + wheelMenu3[getWheel(R.id.p3).getCurrentItem()]);
            }

        /**
         * Initializes wheel
         * 
         * @param id
         *          the wheel widget Id
         */

        private void initWheel1(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);
                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        private void initWheel2(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);
                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu2));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        private void initWheel3(int id)
            {
                WheelView wheel = (WheelView) findViewById(id);

                wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu3));
                wheel.setVisibleItems(2);
                wheel.setCurrentItem(0);
                wheel.addChangingListener(changedListener);
                wheel.addScrollingListener(scrolledListener);
            }

        /**
         * Returns wheel by Id
         * 
         * @param id
         *          the wheel Id
         * @return the wheel with passed Id
         */
        private WheelView getWheel(int id)
            {
                return (WheelView) findViewById(id);
            }

        /**
         * Tests wheel value
         * 
         * @param id
         *          the wheel Id
         * @param value
         *          the value to test
         * @return true if wheel value is equal to passed value
         */
        private int getWheelValue(int id)
            {
                return getWheel(id).getCurrentItem();
            }
    }

main.xml  

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/layout_bg">


    <LinearLayout
        android:layout_marginTop="24dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.vijay.wheel.WheelView
            android:id="@+id/p1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <com.vijay.wheel.WheelView
            android:id="@+id/p2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <com.vijay.wheel.WheelView
            android:id="@+id/p3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp">
        <EditText
            android:id="@+id/r1"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
        <EditText
            android:id="@+id/r2"
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
        <EditText
            android:id="@+id/r3"
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_gravity="center_horizontal"
            android:textSize="18sp"
            android:textColor="#000">
        </EditText>
    </LinearLayout>

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_gravity="center_horizontal"
        android:textSize="18sp"
        android:textColor="#FFF"
        android:text="Your choice">
    </TextView>
</LinearLayout>