Showing posts with label display. Show all posts
Showing posts with label display. Show all posts

Tuesday, 27 May 2014

Android Spinner (drop down list)- How to add Items in Spinner

Android  uses  Spinner  as drop-down list item in activity or action bar frequently.It is a quick way to select a value from the list.
Here in my example I have implemented a spinner view which contains the list of cities. If we click on show button it will take the current selected value  from the spinner and show as toast message .
Reset button will reset the default value in spinner.


Step-1:  Create a new project and add the spinner layout in activity_main.xml file

<Spinner
        android:id="@+id/spinner1"
        android:layout_width="150dp"
        android:layout_height="40dp">
                
</Spinner>

You can define your layout size according to requirement  Or  leave as match_parent or wrap_content

Also add two buttons. After this our Activity file will we like this:

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"
    android:background="#2EFEF7" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="54dp"
        android:text="Show" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="47dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Reset" />

</RelativeLayout>

Step-2:   Define the list of items which you want to show in Spinner. You can do it in many ways. Like you can define in resources.xml file as string-array. See here. You can also take values from database and store in string-array. Here I have taken static values in string-array . Add this in to your MainActivity.java file

   String[] items=new String[] {"<Select City>","Bangalore","New            Delhi","Koltaka","Chennai"};


Step-3:  Define the Spinner layout.

    final Spinner spinner=(Spinner) findViewById(R.id.spinner1);

Step-4:   Create an ArrayAdapter  to show the values in list. Set the adapter with your string-array as follows.

// Create an ArrayAdapter using the string array and a default spinner layout
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
               android.R.layout.simple_spinner_item,items);                      // Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

Now our Spinner is ready for use.

Step-5:   For buttonClickListeners get the values from spinner by following:

// get the particular item from spinner
String item= spinner.getSelectedItem().toString();

//set the particular item in spinner
spinner.setSelection(0);

Final Code:

MainActivity.java

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

public class MainActivity extends Activity implements OnItemSelectedListener {

       //items to show in spinners
      
       String[] items=new String[] {"<Select City>","Bangalore","New Delhi","Koltaka","Chennai"};
      
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
             
              Button bShow=(Button) findViewById(R.id.button1);
              Button bReset=(Button) findViewById(R.id.button2);
              final Spinner spinner=(Spinner) findViewById(R.id.spinner1);
             
              // Create an ArrayAdapter using the string array and a default spinner layout
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                      android.R.layout.simple_spinner_item,items);
              // Specify the layout to use when the list of choices appears
              adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              // Apply the adapter to the spinner
              spinner.setAdapter(adapter);
             
              bShow.setOnClickListener(new OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                           // get the particular item from spinner
                          
                           String item= spinner.getSelectedItem().toString();
                           Toast.makeText(getApplicationContext(),"Selected item:"+item,0).show();
                          
                     }
              });
             
      bReset.setOnClickListener(new OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                          
                           //set the particular item in spinner
                           spinner.setSelection(0);
                           Toast.makeText(getApplicationContext(),"Spinner Resetted",0).show();
                          
                     }
              });
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public void onItemSelected(AdapterView<?> parent, View view, int pos,
                     long id) {
       // An item was selected. You can retrieve the selected item using
        //parent.getItemAtPosition(pos);
        //Here you can do more implementations according to your requirements    based on selecting particular item
       }

       @Override
       public void onNothingSelected(AdapterView<?> arg0) {
              // TODO Auto-generated method stub
             
       }

}

Step-6:   Run the project . You can your additional logic as per requirements in above three functions.
         Please comment and feedback for any issue .Thank You!!







Thursday, 8 May 2014

How to hide Action bar / Notification bar/ Status bar in Android | How to display activity in full screen

We saw in many games or another application that splash activity starts on full screen without status bar or anything. It’s very easy to implement this.
There are two sets of codes for android version 4.1 above and below. So I have put in If statement both codes.
Step-1: Copy and paste the below codes after setContentView() in onCreate(), whichever activity you want in full screen.

Code:

if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
       
        else{
              View decorView = getWindow().getDecorView();
              // Hide the status bar.
              int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
              decorView.setSystemUiVisibility(uiOptions);
              // Remember that you should never show the action bar if the
              // status bar is hidden, so hide that too if necessary.
              ActionBar actionBar = getActionBar();
              actionBar.hide();
        }

Step-2: After pasting this code will give some errors.Keep the mouse cursor on red and import relataed classes.. Your code will run.