Pages

Saturday 21 April 2012

Custom Spinners







Refer to the exercise "HelloAndroid with Spinner", 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".






-------------------------------------------------------------Code------------------------------------------------------------------------



package com.kiran;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class CustomSpinnerDemoActivity extends Activity {
String[] strings = {"CoderzHeaven","Google",
"Microsoft", "Apple", "Yahoo","Samsung"};

String[] subs = {"Heaven of all working codes ","Google sub",
"Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};

int arr_images[] = { R.drawable.coderzheaven,
R.drawable.google, R.drawable.microsoft,
R.drawable.apple, R.drawable.yahoo, R.drawable.samsung};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
        mySpinner.setAdapter(new MyAdapter(CustomSpinnerDemoActivity.this, R.layout.row, strings));
    }

    public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId, String[] objects) {
    super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater=getLayoutInflater();
    View row=inflater.inflate(R.layout.row, parent, false);
    TextView label=(TextView)row.findViewById(R.id.company);
    label.setText(strings[position]);

    TextView sub=(TextView)row.findViewById(R.id.sub);
    sub.setText(subs[position]);

    ImageView icon=(ImageView)row.findViewById(R.id.image);
    icon.setImageResource(arr_images[position]);

    return row;
    }
    }
   }

---------------------------------------------------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="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>


-------------------------------------------------------------Row 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="wrap_content"
    android:orientation="vertical"
    android:padding="3dip" >


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="2dip"
        android:layout_toRightOf="@+id/image"
        android:padding="3dip"
        android:text="CoderzHeaven"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/company"
        android:layout_marginLeft="5dip"
        android:layout_toRightOf="@+id/image"
        android:padding="2dip"
        android:text="Heaven of all working codes" />


</RelativeLayout>








Friday 20 April 2012

Shared Preferences

how data can be stored in SQLDB. However, many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs. 

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity (which is not discussed here). 

The context object lets you retrieve SharedPreferences through the methodContext.getSharedPreferences(). 


------------------------------------------CODE------------------------------------------------------

package com.kiran;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedpreActivity extends Activity {



Button save, show;
EditText enter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button save = (Button) findViewById(R.id.button1);
Button show = (Button) findViewById(R.id.button2);


        final EditText enter = (EditText) findViewById(R.id.editText1);
       
  //-----------------------------SAVE---------------------------------------------------------//    
save.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(SharedpreActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
String text = enter.getText().toString();
editor.putString("key", text);
editor.commit();

}

});
//----------------------------------SHOW-------------------------------------------------//
show.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
TextView textView = (TextView) findViewById(R.id.textView1);
SharedPreferences app_Preferences = PreferenceManager
.getDefaultSharedPreferences(SharedpreActivity.this);
String text = app_Preferences.getString("key", "null");
            textView.setText(text);

}
});
}

}
---------------------------------------------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"
    android:background="@drawable/back"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="TextView" android:textColor="@color/black"/>
   
   

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:text="SHOW" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="43dp" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/editText1"
        android:text="SAVE" />



    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/fish"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>