Thursday, 22 January 2015

MultiAutoCompleteTextView Example









Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10">

        <requestFocus />
    </MultiAutoCompleteTextView>

</RelativeLayout>

MainActivity.java
package com.swamys.multiautocompletetextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {

       MultiAutoCompleteTextView multiAutoCompleteTextView;
       String months[] = { "jan", "feb", "march", "aprl", "may", "jun", "july" };

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);

              ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                           getApplicationContext(),
                           android.R.layout.simple_dropdown_item_1line, months);

              multiAutoCompleteTextView.setAdapter(adapter);
              multiAutoCompleteTextView
                           .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

              multiAutoCompleteTextView
                           .setOnItemClickListener(new OnItemClickListener() {

                                  @Override
                                  public void onItemClick(AdapterView<?> adapterView,
                                                View view, int position, long arg3) {
                                         Toast.makeText(
                                                       getApplicationContext(),
                                                       adapterView.getItemAtPosition(position)
                                                                     .toString(), Toast.LENGTH_LONG).show();
                                  }
                           });

       }
}


AutoCompleteTextView Example in Android





strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AutoComplete Example</string>
    <string name="hint">Enter Any Month Name</string>
</resources>

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="110dp"
        android:ems="10"
        android:hint="@string/hint" >
<requestFocus />
    </AutoCompleteTextView>
</RelativeLayout>

list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/black" >
    </TextView>

MainActivity:
package com.example.autocompleteexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity implements  OnItemClickListener {
AutoCompleteTextView complete;
       String[] Month={"January","February","March","April","May","June","July","August","September","October","November","December"};
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              complete=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
              ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,Month);
              complete.setAdapter(adapter);
      
              }

       public void onItemClick(AdapterView<?> arg0, View arg1, int positionlong arg3) {
              // TODO Auto-generated method stub
              Toast.makeText(getApplicationContext(), Month[position], Toast.LENGTH_LONG).show();
       }

}


Spinner Example

Spinner Example in Android.








String.xml:

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

    <string name="app_name">Spinner Example</string>
    <string name="action_settings">Settings</string>
    <string-array name="fruits">
        <item >apple</item>
        <item >mango</item>
        <item >sugarcane</item>   
        <item >cherry</item>         
    </string-array>

</resources>

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="46dp" />

</RelativeLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/black"
    android:textSize="25sp"  >
   
</TextView>

MainActivity.java:

package com.swamys.spinnerexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener{
       Spinner spinner;
       String[] fruits={"apple","mango","sugarcane","cherry"};

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Spinner spinner=(Spinner)findViewById(R.id.spinner1);
              ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, fruits);
              spinner.setAdapter(adapter);
              spinner.setOnItemSelectedListener(this);
             
                     }
      
      
       public void onItemSelected(AdapterView<?> arg0, View view, int poistion,
                     long arg3) { 
              Toast.makeText(getApplicationContext(), ""+fruits[poistion],Toast.LENGTH_LONG).show();
              
       }
       @Override
       public void onNothingSelected(AdapterView<?> arg0) {
                           
       }
}

ListView example

ListView Example in android






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

    <string name="app_name">ListView</string>
   
</resources>

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#000000">
   
</TextView>

MainActivity.java:
package com.swamys.listview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

       String[] tech = { "Java", "Android", "Dot Not", "Oracle" };

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              ListView listView = (ListView) findViewById(R.id.listView1);

              ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                           getApplicationContext(), R.layout.listview_xml2, tech);

              listView.setAdapter(adapter);
              listView.setOnItemClickListener(this);

       }

       @Override
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
              Toast.makeText(getApplicationContext(), "  " + tech[arg2],
                           Toast.LENGTH_LONG).show();
       }
}