Monday, 1 September 2014

DAY 4 : LEARN ABOUT SPINNER , OVERRIDING METHOD , TO CHANGE THE DRAWABLE ICON


# HelloAndroid with Spinner

 In this exercise, I add a Spinner for user to select country where he come from. I will show how to add a Spinner in main.xml, add a array.xml to embed items into Spinner, and how to handle it in HelloAndroid.java.

A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.

Environment:
Ubuntu 9.04
Eclipse (ANY)
Android 1.5 SDK r3 OR HIGHER

The exercise continuous from the previous article,

Modify main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Who you are?"
  />
<EditText
android:id = "@+id/message_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Where you come from?"
  />
<Spinner
  android:id = "@+id/spinner_countries"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
<LinearLayout
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="right|bottom"
  >
<Button
android:id = "@+id/ok"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="OK" />
<Button
android:id = "@+id/cancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Cancel" />
</LinearLayout>
</LinearLayout>

Create a file, named array.xml in /HelloAndroid2/res/values/array.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--  Used in Spinner/spinner_2.java -->
<string-array name="countries">
 <item>Canada</item>
 <item>China</item>
 <item>Germany</item>
 <item>Japan</item>
 <item>Korea</item>
 <item>Russia</item>
 <item>UK</item>
 <item>USA</item>
</string-array>
</resources>

Finally, modify JavaAndroid.java


package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class HelloAndroid extends Activity {

private Spinner spinner_countries;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       Button okButton = (Button) findViewById(R.id.ok);
       okButton.setOnClickListener(okOnClickListener);
       Button cancelButton = (Button) findViewById(R.id.cancel);
       cancelButton.setOnClickListener(cancelOnClickListener);
      
       spinner_countries = (Spinner) findViewById(R.id.spinner_countries);
       ArrayAdapter<CharSequence> adapter
             = ArrayAdapter.createFromResource(this, 
               R.array.countries, android.R.layout.simple_spinner_item);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinner_countries.setAdapter(adapter);       
   }
   private Button.OnClickListener okOnClickListener = new Button.OnClickListener(){
 @Override
 public void onClick(View v) {
  EditText edit_text = (EditText) findViewById(R.id.message_text);
  CharSequence edit_text_value = edit_text.getText();
  setTitle("Hello: "+edit_text_value+" from "
   +spinner_countries.getItemAtPosition((int) spinner_countries.getSelectedItemId()));
 }
   };
  
   private Button.OnClickListener cancelOnClickListener = new Button.OnClickListener(){
 @Override
 public void onClick(View v) {
  finish();
 }
   };
}

NOW START/CREATE  AVD AND RUN THIS PACKAGE...

 ____________________________________________________________________
 

# Custom Spinner with icon 

FROM ABOVE EXERCISE,it's a basic spinner with default format to display simple text in the spinner. Current exercise is modified to have custom display with icon in the spinner, actually it is Spinner version of another exercise "ListView, with icon".


Create a row.xml in /res/layout/. To to setup the layout on each row.
 
<?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="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextView
android:id="@+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>


main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<Spinner
  android:id="@+id/spinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>


Main Java code, AndroidCustomSpinner.java


package com.exercise.AndroidCustomSpinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class AndroidCustomSpinner extends Activity {

String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
  "Wednesday", "Thursday", "Friday", "Saturday"};

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.row, R.id.weekofday, DayOfWeek);
      mySpinner.setAdapter(adapter);
  }
}


ANY QUERY REGARDING THIS ??? THEN COMMENT PLEASE OR MAKE ME MAIL :
RAKSHITSHAH1994@GMAIL.COM
 _____________________________________________________________________


#Tips and Tricks in using Eclipse: Override a method from a base class


 It's a trick to increasing your productivity in using Eclipse to develope Java code, also apply to Android programming. For example, you want to override the method finish(), but you forget the spelling, or you are too laze to type it. Right click on any space on your source code, and invoke Source > Override/Implement Methods.


 Select the desired method(s) to be inserted, also you can select the insertion point, click OK.


See, your code have been inserted.



HOPE SO , It's a very useful and helpful practice for YOU PEOPLE. :P 

___________________________________________________________________________________

#Simple steps to change the drawable icon with Transparency background using GIMP

Add a icon on Dialog 



Save the icon, , in the folder res/drawable.

Add one line in the openOptionsDialog() class:

private void openOptionsDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setIcon(R.drawable.android)
.setMessage(R.string.app_about_message)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}

In the Exercise: Add a icon on Dialog, the background of android icon is in white. It can be removed to be transparency using GIMP.

Open the android.png in GIMP.
Select Fuzzy Select Tool.

Click on the background of the icon.




Click Select > Invert to select the foreground image.

Click Edit > Copy to copy the select image to clipboard.

Close the original file.

Click File > New to create a new image

Expend the Advanced Options in the Create a New Image dialog.
Select Transparency in the Fill with option, and click OK
.


Click Edit > Paste to paste the copied image as the foreground.

 Run the Android Application again, the background have been changed to transparency now. 


ANY QUERY REGARDING THIS ??? THEN COMMENT PLEASE OR MAKE ME MAIL :
RAKSHITSHAH1994@GMAIL.COM



0 comments:

Post a Comment

DON'T BE SELFISH !!! SHARE IT !!!