Android Switch Button is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states.
Android Switch button is available only in Android 4.0 or later android devices.
Example :
You can use these buttons to change On and Off state of WiFi or Bluetooth

Source code:

Step 1 :Creating Project

Create an android application project named "SwitchButtonExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:gravity="center_horizontal"
    tools:context="com.java2blog.switchbuttonexampleapp.MainActivity">

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch : " >
    </Switch>
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.switchbuttonexampleapp;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {

    Switch switchButton;
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;

        switchButton = (Switch) findViewById(R.id.switch1);
        switchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (switchButton.isChecked()) {

                    Toast.makeText(activity, "Switch is in ON State", Toast.LENGTH_LONG).show();
                } else {

                    Toast.makeText(activity, "Switch is in OFF State", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
We are getting Switch reference from layout file and then using Switch setOnClickListener to register event for state change.
Switch's isChecked() method is used to know current state of Switch whether it is on state or off state.

Step 4 : Running the app 

When you run the app, you will get below screen:

When you click on switch button, it will go to on state.

When you again click on switch button, it will go to off state.

Android ToggleButton is UI widget which has only two states i.e. ON and OFF. It allows user to change setting between these two states.
Example :
You can use these button to change On and Off state of WiFi or Bluetooth

Source code:

Step 1 :Creating Project

Create an android application project named "ToggleButtonExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:gravity="center_horizontal"
    tools:context="com.java2blog.togglebuttonexampleapp.MainActivity">

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" >
    </ToggleButton>
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.togglebuttonexampleapp;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.view.View.OnClickListener;

import static android.webkit.WebSettings.PluginState.ON;

public class MainActivity extends AppCompatActivity {

    ToggleButton toggleButton;
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;

        toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (toggleButton.isChecked()) {

                    Toast.makeText(activity, "Toggle button is in ON State", Toast.LENGTH_LONG).show();
                } else {

                    Toast.makeText(activity, "Toggle button is in OFF State", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

We are getting toggle  reference from layout file and then using ToggleButton's setOnClickListener to register event for state change.
ToggleButton's isChecked() method is used to know current state of ToggleButton whether it is on state or off state.

Step 4 : Running the app 

When you run the app, you will get below screen:
When you click on toggle button, it will go to on state.
When you again click on toggle button, it will go to off state.
We are done with Android ToggleButton example. Please comment if you are facing any issue with the code.

In this post, we are going to see about Android Spinner Dropdown example.

Android Spinner is a UI widget which have dropdown. Once you click on dropdown , you can select one option among various options. I am going to put static data in android spinner and select one of value from them.

Source code:

Step 1 :Creating Project

Create an android application project named "SpinnerDropdownExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.spinnerdropdownexampleapp.MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select country:"
        />
    <Spinner
        android:id="@+id/spinner"
        android:layout_below="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.spinnerdropdownexampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get Spinner reference
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<string> countries = new ArrayList<string>();
        countries.add("India");
        countries.add("Nepal");
        countries.add("China");
        countries.add("Bhutan");

        // Creating array adapter for spinner
        ArrayAdapter<string> dataAdapter = new ArrayAdapter<string>(this, android.R.layout.simple_spinner_item, countries);

        // Drop down style will be listview with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }

    @Override
    public void onItemSelected(AdapterView parent, View view, int position, long id) {
        // getting selected item
        String item = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item in toast
        Toast.makeText(parent.getContext(), "Selected Country: " + item, Toast.LENGTH_LONG).show();

    }

    public void onNothingSelected(AdapterView arg0) {

    }
}
We have created ArrayAdapter and attached it with Spinner in similar way we have done for Simple Android ListView.
If you notice, we have also implemented OnItemSelectedListener for listening to item selection in drop down.

Step 4 : Running the app 

When you run the app, you will get below screen:
Android Spinner dropdown example

When you click on dropdown and select china, you will get below screen.
Android spinner selection example

After selecting china, you will get below screen.

We are done with Android Spinner Dropdown Example. Please comment if you are facing any issue while implementing it.

Android Toast is used to display message for short span of time. Message appears on the screen for short span of time then disappears after some time.
We have already used toast in previous examples like Android SeekBar , Android RatingBar .
In this post, I am going to demonstrate how to make custom toast and use in your app.

Source code:

Lets create Android custom toast example:

Step 1 : Creating Project

Create an android application project named "CustomToastExampleApp".

Step 2: Put any image in drawable folder with name "custom_toast_image"

Step 3 : Creating custom toast layout file

  • Go to res -> layout
  • Right click on layout
  • Click on New -> File. 
  • Create a file named "custom_toast.xml" and paste below code in custom_toast.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="#ffffe0"
    >
    <ImageView
        android:id="@+id/custom_toast_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="This is Custom toast image"
        android:src="@drawable/custom_toast_image"/>

    <TextView
        android:id="@+id/custom_toast_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="This is Custom toast"
        android:text="This is Custom toast" />
</LinearLayout>

Step 4 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.customtoastexampleapp.MainActivity">

    <Button
        android:text="Click to show Custom toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:id="@+id/button" />
</RelativeLayout>

Step 5 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.splashscreenexampleapp;

import android.app.Activity;
import android.os.Bundle;
package com.java2blog.customtoastexampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                //Creating the LayoutInflater instance
                LayoutInflater liCustomToast = getLayoutInflater();
                //Getting the View object as defined in the custom_toast.xml file
                View layout = liCustomToast.inflate(R.layout.custom_toast,
                        (ViewGroup) findViewById(R.id.custom_toast_layout));

                //Creating the Toast object
                Toast toast = new Toast(getApplicationContext());
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);//setting the view of custom toast layout
                toast.show();

            } });
    }
}

It is default MainActivity provided by HelloWorld app.

Step 6 : Running the app 

When you run the app, you will get below screen:
When you click on the button, you will get below screen.

In this post, we are going to see how to load html String in a WebView in Android
Android WebView is a android UI widget which is used to open any web url or load html data. It is used to show web page in android activity. In simple words, Android WebView is a View that displays web pages.
Example :
Lets say You are creating an android app and you have some user agreement or some web pages hosted online, you can just render it using Android WebView.
There are two methods which we generally use to open web url or load html data.
  • loadUrl
  • loadData
I have already demonstrated about loadUrl in previous post. I am going to demonstrate about loadData in this post.

Source code:

Step 1 :Creating Project

Create an android application project named "WebViewLoadDataExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.webviewexampleapp.MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.webviewloaddataexampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

import com.java2blog.webviewloaddataexampleapp.R;

public class MainActivity extends AppCompatActivity {

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

        webView = (WebView) findViewById(R.id.webView);
        String htmlData=
                "<html>\n" +
                "<head>\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
                "<title>HelloWorld</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h3>Hello World from Java2blog!!</h3>\n" +
                "<h4> Android WebView load html data example</h4>\n"+
                "</body>\n" +
                "</html>";
        webView.loadData(htmlData, "text/html", "UTF-8");

    }
    
}
We are getting WebView reference from layout file and then using WebView's loadData method to load the html String in Android WebView.

Step 4 : Running the app 

When you run the app, you will get below screen:

In this post, we are going to see Android webview example.
Android WebView is a android UI widget which is used to open any web url or load html data. It is used to show web page in android activity. In simple words, Andorid WebView is a View that displays web pages.
Example :
Lets say You are creating an android app and you have some user agreement or some web pages hosted online, you can just render it using Android WebView.
There are two methods which we generally use to open web url or load html data.
  • loadUrl
  • loadData
I am going to demonstrate about loadUrl in this post and loadData in next post.

Source code:

Step 1 :Creating Project

Create an android application project named "WebViewExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.webviewexampleapp.MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.webviewexampleapp;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

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

        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.java2blog.com");

    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }
    }

    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
}
We are getting WebView reference from layout file and then using WebView's loadUrl method to load the URL

Step 4: Add internet permission in AndroidManifest.xml

Copy following code:
<uses-permission android:name="android.permission.INTERNET" />

Put it in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2blog.webviewexampleapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Done, we have added internet permission to AndroidManifest.xml. Your application must be able to access internet now.

Step 5 : Running the app 

When you run the app, you will get below screen:
Android WebView Example


Cloud computing and associate technologies are in high demand due to the radical shift to the adoption of cloud based technologies. Amazon Web Services (AWS) is most popular and market leader in the cloud computing service providers. If you look at the career opening with employers, there is a huge demand for the AWS and it became necessary for AWS developers to have certification to showcase their talent to their employers.
AWS Certified Solutions Architect Associate certification exam is the most basic level / entry level exam offered by AWS certification program. Those who are willing to start their career in AWS, this certification is the ideal place to start the preparation. In this article, I am going to explain you how to prepare for this certification and what are the useful information available that can be used during your preparation. If you have any questions, please contact me for the clarification.

Amazon Free Tier Account

You cannot pass this exam without the practical experience on using the Amazon Web Services (AWS) platform. It is not easy to afford this platform for everyone on their own, by luckily Amazon offers 1 year free account for the first time users. This free account offers you most of the services with limited usage.
You can sign-up to this free tier account and start exploring the each services offered by Amazon. You have to start using the services like S3 (which is the online storage provided by Amazon to store the static files. You can even host the static website using S3 service, trust me, it is awesome to use S3), EC2 (processing speed for running your applications), etc.
Here is the link to sign-up for this free service:
https://aws.amazon.com/
AWS Registration



AWS Official Certification Preparation Resources 

Amazon itself hosting lot of official resources that are very useful to prepare for the Solutions Architect Associate exam. Here is the list of documentation that are very helpful:
Certification Site: This is the starting point. This is the official certification site for Solutions Architect Associate exam. There is lot of information that has to be read before you start preparing for the certification. This link provides the basic information about this certification and advantages of this certification.
Frequently Asked Questions (FAQs): This is one of the most useful section of the AWS certifications preparation. This section would help you to understand about each services. There is a FAQ section for each services. You have to go through each of them before attending the exam (Amazon FAQs).
Exam Blueprint: Exam blue print is the syllabus and exam pattern document. This tells you what are the sections covered in the exam and pattern of the exam. This is the exam syllabus that will have to be prepared for the exam. You can download it from here (AWS Solutions Architect Associate BluePrint).
AWS Practice Questions: If you want to get the idea of how the real exam questions will look, you can download the free sample questions from the AWS certification page itself. Here is the link for sample questions. 

Solutions Architect Associate Exam Details 

Here is the summary of exam information for Solutions Architect Associate certification exam.
Number of Questions: 60 Multiple Choice Questions (There is no guarantee that it is fixed. Amazon keeps changing the number of questions)
Duration : 80 Minutes
Passing Score : 65% (There is no guarantee that it is fixed. Amazon keeps changing the passing score) There is no negative mark in the exam.

Recommended Books

There is only one book available in the market for this exam and also there is no other books for other certifications as well as of writing this article. You can buy the official book released for the solutions architect associate certification exam.
https://www.amazon.com/Certified-Solutions-Architect-Official-Study/dp/1119138558/
AWS Certification books


Who can take the AWS certification? 

There is a misconception that only technical background with programming skills can write the AWS certification exams. But, AWS certification exam will be suitable for non-technical background too. Having the programming skills are a added advantage because it is easy for you to trouble shoot the problems and you can understand the infrastructure. Anyone can write the AWS certification and have a successful career in AWS technology. Only requirement is to be more dedicated on learning various concepts about the cloud computing. There is quite number of services that has to be configured and used for implementing the AWS.

Career Prospect for AWS certified Developers 

Here is the few snapshots that shows how AWS jobs are on the rise. There is huge demand for AWS jobs in the coming years. Look at the Google Trends and Indeed search engine for the AWS related keywords. This clearly shows that chart is going up over the years. 


                                                                           (Source)

Practice Questions 

Once you have gone through the white papers, official documentation offered by Amazon and books, it is time to try the practice questions. This is one of the most important step for gaining confidence for attending the real exam. There are various leading one exam simulators there for taking up the practice questions. 
I would recommend the following online resources for preparing for the Solutions architect associate exam: 

Summary 

I hope this article would help you to prepare for the AWS certification exam. If you are interested in start your career in cloud computing or Amazon Web Services (AWS), it is right time to start preparing for the AWS Certified Solutions Architect Associate certification exam. With 2 months of dedicated effort, you will be able to pass this certification. I will keep writing few more articles in Amazon Web Services in my next blog posts. Keep watching my blog and subscribe to receive the future updates.

In previous post, we have seen simple android ListView example. In this post, we are going to see Android Custom ListView example.
So we will create custom ListView in which each list item will have Country flag, Country name and its capital in different font size as below.

It is widely used in android apps to list number of items and select one or more items from the list.

Source code:

Step 1 :Creating Project

Create an android application project named "CustomListViewExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.customlistviewexampleapp.MainActivity">

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
</RelativeLayout>
You will see below screen in design view.

Step 3: Creating layout for Row

As We have declared ListView widget in activity_main.xml. Now we need to provide layout for individual row.
  • Go to res -> layout
  • right click on layout
  • Click on New -> File. 
  • Create a file named "row_item.xml" and paste below code in row_item.xml.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView

        android:layout_rowSpan="2"
        android:layout_width="80dp"
        android:layout_height="70dp"

        android:scaleType="fitXY"
        android:id="@+id/imageViewFlag"
        android:layout_row="0"
        android:layout_column="0" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_marginLeft="10dp"
        android:textSize="30dp"
        android:textColor="#1E90FF"
        android:id="@+id/textViewCountry"
        android:layout_row="0"
        android:layout_column="1" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_marginLeft="10dp"
        android:textSize="20dp"
        android:textColor="#4B0082"
        android:id="@+id/textViewCapital"
        android:layout_row="1"
        android:layout_column="1" />
</GridLayout>

Step 4 :  Creating ArrayAdapter for ListView

Before creating MainActivity, we need to create CustomCountryList class for custom ListView row.
package com.java2blog.customlistviewexampleapp;

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

import com.java2blog.customlistviewexampleapp.R;

public class CustomCountryList extends ArrayAdapter<String> {
    private String[] countryNames;
    private String[] capitalNames;
    private Integer[] imageid;
    private Activity context;

    public CustomCountryList(Activity context, String[] countryNames, String[] capitalNames, Integer[] imageid) {
        super(context, R.layout.row_item, countryNames);
        this.context = context;
        this.countryNames = countryNames;
        this.capitalNames = capitalNames;
        this.imageid = imageid;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;
        LayoutInflater inflater = context.getLayoutInflater();
        if(convertView==null)
            row = inflater.inflate(R.layout.row_item, null, true);
        TextView textViewCountry = (TextView) row.findViewById(R.id.textViewCountry);
        TextView textViewCapital = (TextView) row.findViewById(R.id.textViewCapital);
        ImageView imageFlag = (ImageView) row.findViewById(R.id.imageViewFlag);

        textViewCountry.setText(countryNames[position]);
        textViewCapital.setText(capitalNames[position]);
        imageFlag.setImageResource(imageid[position]);
        return  row;
    }
}
This class is used to populating data for ListVIew. getView method is get called for drawing each row.

Step 5 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.customlistviewexampleapp;

import android.app.ListActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.java2blog.customlistviewexampleapp.CustomCountryList;
import com.java2blog.customlistviewexampleapp.R;


public class MainActivity extends ListActivity {

    private ListView listView;
    private String countryNames[] = {
            "India",
            "China",
            "Nepal",
            "Bhutan"
    };

    private String capitalNames[] = {
            "Delhi",
            "Beijing",
            "Kathmandu",
            "Thimphu"
    };


    private Integer imageid[] = {
            R.drawable.india,
            R.drawable.china,
            R.drawable.nepal,
            R.drawable.bhutan

    };


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

        // Setting header
        TextView textView = new TextView(this);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText("List of Countries");

        ListView listView=(ListView)findViewById(android.R.id.list);
        listView.addHeaderView(textView);

        // For populating list data
        CustomCountryList customCountryList = new CustomCountryList(this, countryNames, capitalNames, imageid);
        listView.setAdapter(customCountryList);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(getApplicationContext(),"You Selected "+countryNames[position-1]+ " as Country",Toast.LENGTH_SHORT).show();        }
        });
    }
}
If you notice, we have extended to ListActivity for this class. ListActivity class provides some methods specific to ListView.
We have declared three arrays to take care of Country textView, Capital textView and flag ImageView and customCountryList is being used to populate data in ListView.
As we have added textView to ListView as header in above code thats why we have used countryNames[position-1] while setting toast text, if you don't use header then it should be countryNames[position].

Step 6: Put images in drawable folder

Download source code, locate res -> drawable and put images in your application 's res -> drawable folder.

Step 7 : Running the app 

When you run the app, you will get below screen:


When you click on Bhutan list item, you will get below screen:


In previous post, we have already seen Android SeekBar example. In this post, we will see how to create Custom SeekBar.
If you follow Android SeekBar example , you will see below screen.

We will customize above SeekBar and it will look as below after customization.

If you want to customize above SeekBar , you can follow below tutorial to create Custom SeekBar it.

Source code:

Step 1 :Creating Project

Create an android application project named "AndroidCustomSeekBarExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<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="com.java2blog.customseekbarexampleapp.MainActivity" >

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:max="10"
        android:thumb="@drawable/thumb"
        android:secondaryProgress="5"
        android:progress="5"
        android:progressDrawable="@layout/progress_seekbar"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seekBar"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="14dp" />

</RelativeLayout>
If you notice, it has two new attributes android:thumb="@drawable/thumb" and android:progressDrawable="@layout/progress_seekbar". You will understand more about it in further steps.

Step 3: Create thumb and put it in drawable folder :

Thumb is nothing but item which you drag to change the progress.  Create a thumb image called "thumb.png" and put it in drawable folder. It will look something like below :
You can download image from source.

Step 4 : Creating XML for drawing progress bar :

  • Go to res -> layout
  • right click on layout
  • Click on New -> File. 
  • Create a file named "progress_seekbar.xml" and paste below code in row_item.xml.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="10dp"/>

            <gradient
                android:angle="270"
                android:endColor="#252945"
                android:startColor="#8a834b" />
        </shape>
    </item>
    <item>
        <clip>
            <shape android:shape="rectangle" >
                <corners android:radius="10dp" />

                <gradient
                    android:angle="270"
                    android:endColor="#990800"
                    android:startColor="#d14900" />
            </shape>
        </clip>
    </item>

</layer-list>
Above XML is used to design progresses for custom SeekBar. Above XML uses layer-list tag. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

Step 5 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.customseekbarexampleapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.java2blog.customseekbarexampleapp.R;

import static com.java2blog.customseekbarexampleapp.R.id.seekBar;
import static com.java2blog.customseekbarexampleapp.R.id.textView;

public class MainActivity extends Activity {

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

        customSeekbar = (SeekBar) findViewById(R.id.seekBar);
        textView = (TextView) findViewById(R.id.textView);
        // Set default value to 0
        textView.setText("Progress : "+customSeekbar.getProgress() + "/" + customSeekbar.getMax());
        customSeekbar.setOnSeekBarChangeListener(
                new OnSeekBarChangeListener() {
                    int progress = 0;
                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                                                  int progresValue, boolean fromUser) {
                        progress = progresValue;
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // Display the value in textview
                        textView.setText("Progress : "+progress + "/" + seekBar.getMax());
                    }
                });
    }
}

We are getting widget reference from layout file and then using SeekBar's setOnSeekBarChangeListener method to set listener for our custom seekbar.

Step 6 : Running the app 

When you run the app, you will get below screen:


Drag ping circle and use it to set the value for SeekBar.

We are done with Android Custom SeekBar example.
Happy Android Learning !!

In this post, we will see how to create Android splash screen.
Android splash screen is nothing but screen that appears when some background task (such as fetching data from database,loading images) is going on. These kind of task generally tasks long and at that time, we can show splash screen. Splash screen can be app icon, company logo , punch line. Once background task completes, another activity will get called.
For example:
When Xender app loads, you will see screen as below.
Xender App

Source code:

Lets create Android splash screen example:

Step 1 :Creating Project

Create an android application project named "SplashScreenExampleApp".

Step 2: Put any image in drawable folder with name "splash_logo"

Step 3 : Creating splash screen layout file

  • Go to res -> layout
  • Right click on layout
  • Click on New -> File. 
  • Create a file named "splash_screen.xml" and paste below code in splash_screen.xml.
<LinearLayout 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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/splash_logo"
        android:layout_gravity="center"/>
</LinearLayout>

Step 4: Create SplashActivity

package com.java2blog.splashscreenexampleapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.java2blog.splashscreenexampleapp.R;

public class SplashActivity extends Activity {

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

        //creating new thread just for demonstration of background tasks
        Thread t=new Thread() {
            public void run() {

                try {
                    //sleep thread for 10 seconds
                    sleep(10000);

                    //Call Main activity
                    Intent i=new Intent(SplashActivity.this, com.java2blog.splashscreenexampleapp.MainActivity.class);
                    startActivity(i);

                    //destroying Splash activity
                    finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        //start thread
        t.start();
    }
}
Create SplashActivity as above. We are creating a new thread and calling sleep for 10 secs on it. In real time, this will be replaced by actually task that will be performed such as fetching data from database or loading images.

Step 5 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.splashscreenexampleapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World from Java2blog.com" />
</RelativeLayout>

Step 6 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.splashscreenexampleapp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.java2blog.splashscreenexampleapp.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
It is default MainActivity provided by HelloWorld app.

Step 7 : Changing AndroidManifest.xml

We need to put launcher activity as SplashActivity, so we need to do following changes in app -> src -> main -> AndroidManifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2blog.splashscreenexampleapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.java2blog.splashscreenexampleapp.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"
            android:label="@string/app_name">
        </activity>

    </application>

</manifest>

Step 7 : Running the app 

When you run the app, you will get below screen:

Android Splash Screen tutorial
After 10 secs, you will get below screen:


Android Splash Screen Example

In this post , we are going to create simple ListView.
ListView is the view in which you arrange your list items in vertical order.
For example:
It is very popular widget which is used almost in every application. In this post, I am going to create a very simple list view . If you want to create a some what complex Listview which have images and text in same row , you can go through Android Custom ListView example.

Source code:

Step 1 :Creating Project

Create an android application project named "AndroidListViewExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.androidlistviewexampleapp.MainActivity">

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.androidlistviewexampleapp;

import android.app.ListActivity;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    String[] listofCountries={"India","China","Nepal","Bhutan"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listofCountries));

        TextView textView = new TextView(this);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText("List of Countries");

        ListView listView=(ListView)findViewById(android.R.id.list);
        listView.addHeaderView(textView);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(this, "You have selected : " + listofCountries[position-1]+ " as country", Toast.LENGTH_LONG).show();
    }
}
If you notice closely, our MainActivity extends ListActivity as ListActivity has some method specifically for ListView.
    setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listofCountries));
Whenever we create ListView, we need to provide ArrayAdapter which sets view for each row and also we need to provide a file which defines layout of each Row. In this example, we are using android.R.layout.simple_list_item_1 which is android's predefined layout file .

Step 4 : Running the app 

When you run the app, you will get below screen:
Android simple ListView example

When you click on Bhutan , you will get below screen

Android Simple ListView Tutorial


We are done with Android Simple ListView example.
Happy Android Learning !!

In this post, we are going to see about Android Rating Bar.
RatingBar is android widget which is used to provide rating bar with star icons. You might have seen this RatingBar when any application asks for you to rate installed app.

It is very easy to create Rating in Android. Just follow below steps to create Rating Bar.

Source code:

Step 1 :Creating Project

Create an android application project named "RatingBarExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/rateApp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rate this application"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:numStars="5"
        android:stepSize="0.5"
        android:rating="3.0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:text="Submit" />

    <TextView
        android:id="@+id/ratingVal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.ratingbarexampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class MainActivity extends AppCompatActivity {

    private RatingBar ratingBar;
    private Button button;
    private TextView ratingVal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        ratingVal=(TextView) findViewById(R.id.ratingVal);
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                ratingVal.setText("Current Rating  : "+String.valueOf(rating));

            }
        });
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, String.valueOf(ratingBar.getRating()), Toast.LENGTH_LONG).show();
            }

        });

    }
}
We are getting widget reference from layout file and then using RatingBar setOnRatingBarChangeListener method to set listener for our RatingBar.

Step 4 : Running the app 

When you run the app, you will get below screen:
Android RatingBar Example

Click on stars to give appropriate rating

Android RatingBar Example tutorial
 When you submit , you will see below screen.

Android RatingBar toast example

We are done with Android Rating example.
Happy Android Learning !!

In this tutorial , we are going to see Android SeekBar example.
SeekBar is extension of ProgressBar. You can drag cursor left or right to select correct value. It is generally used in setting brightness or music progress or setting sound.
It is very easy to create SeekBar in Android. Just follow below steps to create SeekBar.

Source code:

Step 1 :Creating Project

Create an android application project named "SeekBarExampleApp".

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.java2blog.seekbarexampleapp.MainActivity">


    <SeekBar
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:max="10"
        android:id="@+id/seekBar" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seekBar"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="14dp" />
</RelativeLayout>

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.seekbarexampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textView = (TextView) findViewById(R.id.textView);
        // Set default value to 0
        textView.setText(seekBar.getProgress() + "/" + seekBar.getMax());
        seekBar.setOnSeekBarChangeListener(
                new OnSeekBarChangeListener() {
                    int progress = 0;
                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                                                  int progresValue, boolean fromUser) {
                        progress = progresValue;
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // Display the value in textview
                        textView.setText("Progress : "+progress + "/" + seekBar.getMax());
                    }
                });
    }


}

We are getting widget reference from layout file and then using SeekBar's setOnSeekBarChangeListener method to set listener for our seekbar.

Step 4 : Running the app 

When you run the app, you will get below screen:


Drag ping circle and use it to set the value for SeekBar.

We are done with Android SeekBar example.
Happy Android Learning !!
 

Java tutorial for beginners Copyright © 2012