Wednesday, 28 December 2011

How To Create A Spinner With Data Bind In Android.



Create the Spinner code in "main.xml". This for the UI (spinner design).




  <RelativeLayout >
         <TextView
        android:id="@+id/textView1"
        android:layout_width="120dip"
         android:layout_marginTop="10dip"
        style="@style/FixAppointMentDettextView"
        android:layout_height="wrap_content"
        android:text="Blood Group" />
       
         </RelativeLayout>
       
     <LinearLayout android:layout_height="wrap_content" android:layout_width="180dip" >
    <Spinner
       
        android:id="@+id/add_pa_bloodgroup_spinner"      
        android:layout_width="180dip"
        android:textSize="90dip"
        android:layout_height="wrap_content" >
    </Spinner>
    </LinearLayout>


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


Create an array of data.... that display in the popup of the spinner.




res/values/String.xml




<string-array name="BloodGroup" > 
        <item>Select</item>
        <item>A+</item>
        <item>A-</item>
        <item>B+</item>
        <item>B-</item>
        <item>AB+</item>
        <item>AB-</item>
        <item>O+</item>
        <item>O-</item>
        </string-array>




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




Add the java code in the onCreate of .java






Spinner spinner = (Spinner) findViewById(R.id.add_pa_bloodgroup_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    this, R.array.BloodGroup, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);




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






Create A callback method in .java.





public class MyOnItemSelectedListener implements OnItemSelectedListener {


   public void onItemSelected(AdapterView<?> parent,
       View view, int pos, long id) {
    blood = parent.getItemAtPosition(pos).toString();    
    System.out.println("555555555VALUE Blood" + blood);
   }
   
   
  
   public void onNothingSelected(AdapterView parent) {



                        }
             }




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


Use this on the .java's onCreate part.




add_pa_bloodgroup_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());




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

How To Create An Alert Box in Android.


Create a "main.xml" data in your layout folder.


<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"><Button android:id="@+id/button1" android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content"/></LinearLayout>

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




Use the following code in your .java page (class file)


Here (AlertActivity.java)




public class AlertActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this);
builder.setMessage("are you sure want to exit");
builder.setCancelable(false);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
 
@Override
public void onClick(DialogInterface dialog, int which) {

AlertActivity.this .finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

}

});
        
        
    }
}
..........................................................................................................................................................

AndroidManifest.xml


<manifest package="com.alert" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="10"/><application android:icon="@drawable/bea" android:label="@string/app_name"><activity android:name=".AlertActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>


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