AdSense

AdSense3

Thursday 30 July 2015

Android RatingBar Example

android rating bar
Android RatingBar can be used to get the rating from the user. The Rating returns a floating-point number. It may be 2.0, 3.5, 4.0 etc.
Android RatingBar displays the rating in stars. Android RatingBar is the subclass of AbsSeekBar class.
The getRating() method of android RatingBar class returns the rating number.

Android RatingBar Example

Let's see the simple example of rating bar in android.

activity_main.xml

Drag the RatingBar and Button from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <RatingBar  
  8.         android:id="@+id/ratingBar1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="44dp" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/button1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/ratingBar1"  
  20.         android:layout_below="@+id/ratingBar1"  
  21.         android:layout_marginLeft="92dp"  
  22.         android:layout_marginTop="66dp"  
  23.         android:text="submit" />  
  24.   
  25. </RelativeLayout>  

Activity class

Let's write the code to display the rating of the user.
File: MainActivity.java
  1. package com.example.rating;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.RatingBar;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     RatingBar ratingbar1;  
  14.     Button button;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         addListenerOnButtonClick();  
  20.     }  
  21.   
  22.     public void addListenerOnButtonClick(){  
  23.         ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);  
  24.         button=(Button)findViewById(R.id.button1);  
  25.         //Performing action on Button Click  
  26.         button.setOnClickListener(new OnClickListener(){  
  27.   
  28.             @Override  
  29.             public void onClick(View arg0) {  
  30.                 //Getting the rating and displaying it on the toast  
  31.                 String rating=String.valueOf(ratingbar1.getRating());  
  32.                 Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
  33.             }  
  34.               
  35.         });  
  36.     }  
  37.     @Override  
  38.     public boolean onCreateOptionsMenu(Menu menu) {  
  39.         // Inflate the menu; this adds items to the action bar if it is present.  
  40.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  41.         return true;  
  42.     }  
  43.   
  44. }  

Output:

android rating bar example output 1 android rating bar example output 2

Wednesday 29 July 2015

Android AutoCompleteTextView Example

android autocompletetextview
Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.
Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.
Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class.

Android AutoCompleteTextView Example

In this example, we are displaying the programming languages in the autocompletetextview. All the programming languages are stored in string array. We are using the ArrayAdapter class to display the array content.
Let's see the simple example of autocompletetextview in android.

activity_main.xml

Drag the AutoCompleteTextView and TextView from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginTop="15dp"  
  14.         android:text="@string/what_is_your_favourite_programming_language_" />  
  15.   
  16.     <AutoCompleteTextView  
  17.         android:id="@+id/autoCompleteTextView1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentLeft="true"  
  21.         android:layout_below="@+id/textView1"  
  22.         android:layout_marginLeft="36dp"  
  23.         android:layout_marginTop="17dp"  
  24.         android:ems="10"  
  25.         android:text="">  
  26.    
  27.     </AutoCompleteTextView>  
  28.       
  29. </RelativeLayout>  

Activity class

Let's write the code of AutoCompleteTextView.
File: MainActivity.java
  1. package com.example.autocompletetextview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.graphics.Color;  
  6. import android.view.Menu;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.AutoCompleteTextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.           
  17.         //Creating the instance of ArrayAdapter containing list of language names  
  18.            ArrayAdapter<String> adapter = new ArrayAdapter<String>  
  19.             (this,android.R.layout.select_dialog_item,language);  
  20.         //Getting the instance of AutoCompleteTextView  
  21.            AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
  22.            actv.setThreshold(1);//will start working from first character  
  23.            actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
  24.            actv.setTextColor(Color.RED);  
  25.                           
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu) {  
  30.         // Inflate the menu; this adds items to the action bar if it is present.  
  31.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  32.         return true;  
  33.     }  
  34.   
  35. }  

Output:

android autocompletetextview example output 1 android autocompletetextview example output 2

Tuesday 28 July 2015

Android Spinner Example

android spinner
Android Spinner is like the combox box of AWT or Swing. It can be used to display the multiple options to the user in which only one item can be selected by the user.
Android spinner is like the drop down menu with multiple values from which the end user can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the adapter classes with spinner.
Android Spinner class is the subclass of AsbSpinner class.

Android Spinner Example

In this example, we are going to display the country list. You need to use ArrayAdapter class to store the country list.
Let's see the simple example of spinner in android.

activity_main.xml

Drag the Spinner from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Spinner  
  8.         android:id="@+id/spinner1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="83dp" />  
  14.   
  15. </RelativeLayout>  

Activity class

Let's write the code to display item on the spinner and perform event handling.
File: MainActivity.java
  1. package com.example.spinner;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.Spinner;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity implements  
  13. AdapterView.OnItemSelectedListener {  
  14.   
  15.     String[] country = { "India""USA""China""Japan""Other",  };  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         //Getting the instance of Spinner and applying OnItemSelectedListener on it  
  22.         Spinner spin = (Spinner) findViewById(R.id.spinner1);  
  23.         spin.setOnItemSelectedListener(this);  
  24.           
  25.         //Creating the ArrayAdapter instance having the country list  
  26.         ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
  27.         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  28.         //Setting the ArrayAdapter data on the Spinner  
  29.         spin.setAdapter(aa);  
  30.     }  
  31.   
  32.       
  33.     //Performing action onItemSelected and onNothing selected  
  34.     @Override  
  35.     public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {  
  36.         Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onNothingSelected(AdapterView<?> arg0) {  
  41.         // TODO Auto-generated method stub  
  42.           
  43.     }  
  44.   
  45.     @Override  
  46.     public boolean onCreateOptionsMenu(Menu menu) {  
  47.         // Inflate the menu; this adds items to the action bar if it is present.  
  48.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  49.         return true;  
  50.     }  
  51. }  

Output:

android spinner example output 1 android spinner example output 2