Monday, 9 July 2012

Difference Between Interface and Abstract Class


    Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
    Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
    Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
    Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
    An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
    A Java class can implement multiple interfaces but it can extend only one abstract class.
    Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
    In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
When to prefer an interface

Back when I wrote about the importance of composition, I mentioned that it is extremely useful when you don’t want a massive hierarchical type framework. The same applies to interfaces. This isn’t my example, but its the best one Ive come across. Lets say you have an interface for a Director and another interface for a Actor.

public interface Actor{
   Performance say(Line l);
}

public interface Director{
   Movie direct(boolean goodmovie);
}

In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes, we can implement both Actor and Director. We could even define an ActorDirector interface that extends both like this:

public interface ActorDirector extends Actor, Director{
...
}

We could achieve the same thing using abstract classes. Unfortunately the alternative would require up to 2^n (where n is the number of attributes) possible combinations in order to support all possibilities.

Sunday, 8 July 2012

Create Server and clent Side application in java

import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ServerTest
{
    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static BufferedReader bufferedReader;
    private static String inputLine;
    public static void main(String[] args)
    {
        // Wait for client to connect on 63400
        try
        {
            serverSocket = new ServerSocket(63400);
            clientSocket = serverSocket.accept();
            // Create a reader
            bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // Get the client message
            while((inputLine = bufferedReader.readLine()) != null)
            System.out.println(inputLine);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}



And client side
import java.net.Socket;
import java.io.PrintWriter;
public class ClientTest
{
    private static Socket socket;
    private static PrintWriter printWriter;
    public static void main(String[] args)
    {
        try
        {
            socket = new Socket("localhost",63400);
            printWriter = new PrintWriter(socket.getOutputStream(),true);
            printWriter.println("Hello Socket");
            printWriter.println("EYYYYYAAAAAAAA!!!!");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Friday, 6 July 2012

Java's Access Specifiers

Java's Access Specifiers

In Java code, class and variable and method and constructor declarations can have “access specifiers”, that is one of: private, protected, public. (or none.)

(Note: it is important to remember that constructors in Java are treated differently than methods. Class members are made of 2 things:class's variables.class's methods. Constructors are NOT considerd a class member.)

The purpose of access specifiers is to declare which entity can not be accessed from where. Its effect is different when used on any of: {class, class variable, class method, class's constructor}.
Access Specifiers for Class Variables and Class Methods

Below is a table showing the effects of access specifiers for class members (i.e. class variable and class methods).
For example, if a variable is declared “protected”, then the class itself can access it, its subclass can access it, and any class in the same package can also access it, but otherwise a class cannot access it.

If a class memeber doesn't have any access specifier (the “(none)” row in above), its access level is sometimes known as “package”.

Here's a example.

class P {
    int x = 7;
}
public class AS {
    public static void main(String[] args) {
        P p = new P();
        System.out.println(p.x);
    }
}

The code compiles and runs. But, if you add “private” in front of int x, then you'll get a compiler error: “x has private access in P”. This is because when a member variable is private, it can only be accessed within that class.
Access Specifiers for Constructors

Constructors can have the same access specifiers used for variables and methods. Their meaning is the same. For example, when a constructor has “private” declared, then, only the class itself can create a instance of it (kind of like self-reference). Other class in the same package can not create a instance of that class. Nor any subclass of that class. Nor any other class outside of this package.

Here is a sample code.

class Q {
    public int x;
    private Q (int n) {
        x=n;
        System.out.println("i'm born!");
    }
}

public class ASS {
    public static void main(String[] args) {
        Q q = new Q(3);
        System.out.println(q.x);
    }
}

In the above code, it won't compile because Q's contructor is “private” but it is being created outside of itself. If you delete the “private” keyword in front of Q's constructor, then it compiles.
Constructors in Same Class Can Have Different Access Specifiers

Remember that a class can have more than one constructors, each with different parameters. Each constructor can have different access specifier.

In the following example, the class Q has two constructors, one takes a int argument, the other takes a double argument. One is declared private, while the other with no access specifier (default package level access).

class Q {
    Q (int n) {
        System.out.println("i'm born int!");
    }
    private Q (double d) {
        System.out.println("i'm born double!");
    }
}

public class ASS2 {
    public static void main(String[] args) {
        Q q1 = new Q(3);
        //Q q2 = new Q(3.3);
    }
}

Thursday, 5 July 2012

What Is a Web Application?

What Is a Web Application?

Web applications are by nature distributed applications, meaning that they are programs that run on more than one computer and communicate through a network or server. Specifically, web applications are accessed with a web browser and are popular because of the ease of using the browser as a user client. For the enterprise, the ability to update and maintain web applications without deploying and installing software on potentially thousands of client computers is a key reason for their popularity. Web applications are used for web mail, online retail sales, discussion boards, weblogs, online banking, and more. One web application can be accessed and used by millions of people.

Like desktop applications, web applications are made up of many parts and often contain miniprograms, some of which have user interfaces, and some of which do not require a graphical user interface (GUI) at all. In addition, web applications frequently require an additional markup or scripting language, such as HTML, CSS, or JavaScript programming language. Also, many applications use only the Java programming language, which is ideal because of its versatility.

A web application can be as simple as a page that shows the current date and time or as complex as a set of pages on which you can look up and book the most convenient flight, hotels, and car rentals for your next vacation.

The Java technologies you'll use to create web applications are a part of the Java EE platform, in addition to many of the Java Platform, Standard Edition (Java SE) classes and packages. In order for many of these technologies to work on a server, the server must have a container, or web server, installed that recognizes and runs the classes you create. For development and testing of these technologies, you can use the tools detailed in this article, but when you deploy, make sure that the server has Java server software installed to run Java technology-based web applications. If you don't have access to this information, ask the server administrator.
Java Technologies to Use in Web Applications

There are too many Java technologies to list in one article, so this article will describe only the ones most frequently used. The number of technologies listed here can appear overwhelming. Keep in mind that you will not need to use them all. In fact, a web application often consists of nothing more than one page created with the JavaServer Pages (JSP) technology. Sometimes you will combine three or more such technologies. No matter how many you end up using, it's good to know what is available to you and how you can use each one in a web application.

Java Servlet API

The Java Servlet API lets you define HTTP-specific classes. A servlet class extends the capabilities of servers that host applications that are accessed by way of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For instance, you might use a servlet to get the text input from an online form and print it back to the screen in an HTML page and format, or you might use a different servlet to write the data to a file or database instead. A servlet runs on the server side -- without an application GUI or HTML user interface (UI) of its own. Java Servlet extensions make many web applications possible.

Figure 1 shows clients talking to Java Servlet extensions. Clients may range in complexity from simple HTML forms to sophisticated Java technology-based applets.

Wednesday, 4 July 2012

How to convert Java object to / from JSON (Gson)


JSON is stand for JavaScript Object Notation, it is a lightweight data-interchange format. You can see many Java applications started to throw away XML format and start using json as a new s data-interchange format. Java is all about object, often times, you need to convert an object into json format for data-interchange or vice verse.

In this article, we show you how to use Gson, JSON library, to convert object to/from json.

Gson is easy to learn and implement, what we need to know are following two methods

    toJson() – Convert Java object to JSON format
    fromJson() – Convert JSON into Java object

1. Gson Dependency

For non-Maven user, get the Gson library from Gson official site, for Maven user, declares following dependency in your pom.xml.

  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>1.7.1</version>
  </dependency>
2. POJO

A pojo, with initialized values. Later use Gson to convert this object to/from JSON formatted string.

package com.mkyong.core;

import java.util.ArrayList;
import java.util.List;

public class DataObject {

    private int data1 = 100;
    private String data2 = "hello";
    private List<String> list = new ArrayList<String>() {
      {
        add("String 1");
        add("String 2");
        add("String 3");
      }
    };

    //getter and setter methods

    @Override
    public String toString() {
       return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
        + list + "]";
    }

}
3. toJson() example

Convert object to JSON string, and save it as “file.json“.

package com.mkyong.core;

import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.Gson;

public class GsonExample {
    public static void main(String[] args) {

    DataObject obj = new DataObject();
    Gson gson = new Gson();

    // convert java object to JSON format,
    // and returned as JSON formatted string
    String json = gson.toJson(obj);

    try {
        //write converted json data to a file named "file.json"
        FileWriter writer = new FileWriter("c:\\file.json");
        writer.write(json);
        writer.close();

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

    System.out.println(json);

    }
}

Output

{"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}

4. fromJson() example

Read data from “file.json“, convert back to object and display it.

package com.mkyong.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;

public class GsonExample {
    public static void main(String[] args) {

    Gson gson = new Gson();

    try {

        BufferedReader br = new BufferedReader(
            new FileReader("c:\\file.json"));

        //convert the json string back to object
        DataObject obj = gson.fromJson(br, DataObject.class);

        System.out.println(obj);

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

    }
}

Output

DataObject [data1=100, data2=hello, list=[String 1, String 2, String 3]]

Tuesday, 3 July 2012

Android Manifest.permission

Manifest.permission

String     ACCESS_CHECKIN_PROPERTIES     Allows read/write access to the "properties" table in the checkin database, to change values that get uploaded.
String     ACCESS_COARSE_LOCATION     Allows an application to access coarse (e.g., Cell-ID, WiFi) location
String     ACCESS_FINE_LOCATION     Allows an application to access fine (e.g., GPS) location
String     ACCESS_LOCATION_EXTRA_COMMANDS     Allows an application to access extra location provider commands
String     ACCESS_MOCK_LOCATION     Allows an application to create mock location providers for testing
String     ACCESS_NETWORK_STATE     Allows applications to access information about networks
String     ACCESS_SURFACE_FLINGER     Allows an application to use SurfaceFlinger's low level features
String     ACCESS_WIFI_STATE     Allows applications to access information about Wi-Fi networks
String     ACCOUNT_MANAGER     Allows applications to call into AccountAuthenticators.
String     ADD_VOICEMAIL     Allows an application to add voicemails into the system.
String     AUTHENTICATE_ACCOUNTS     Allows an application to act as an AccountAuthenticator for the AccountManager
String     BATTERY_STATS     Allows an application to collect battery statistics
String     BIND_ACCESSIBILITY_SERVICE     Must be required by an AccessibilityService, to ensure that only the system can bind to it.
String     BIND_APPWIDGET     Allows an application to tell the AppWidget service which application can access AppWidget's data.
String     BIND_DEVICE_ADMIN     Must be required by device administration receiver, to ensure that only the system can interact with it.
String     BIND_INPUT_METHOD     Must be required by an InputMethodService, to ensure that only the system can bind to it.
String     BIND_REMOTEVIEWS     Must be required by a RemoteViewsService, to ensure that only the system can bind to it.
String     BIND_TEXT_SERVICE     Must be required by a TextService (e.g.
String     BIND_VPN_SERVICE     Must be required by an VpnService, to ensure that only the system can bind to it.
String     BIND_WALLPAPER     Must be required by a WallpaperService, to ensure that only the system can bind to it.
String     BLUETOOTH     Allows applications to connect to paired bluetooth devices
String     BLUETOOTH_ADMIN     Allows applications to discover and pair bluetooth devices
String     BRICK     Required to be able to disable the device (very dangerous!).
String     BROADCAST_PACKAGE_REMOVED     Allows an application to broadcast a notification that an application package has been removed.
String     BROADCAST_SMS     Allows an application to broadcast an SMS receipt notification
String     BROADCAST_STICKY     Allows an application to broadcast sticky intents.
String     BROADCAST_WAP_PUSH     Allows an application to broadcast a WAP PUSH receipt notification
String     CALL_PHONE     Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed.
String     CALL_PRIVILEGED     Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed.
String     CAMERA     Required to be able to access the camera device.
String     CHANGE_COMPONENT_ENABLED_STATE     Allows an application to change whether an application component (other than its own) is enabled or not.
String     CHANGE_CONFIGURATION     Allows an application to modify the current configuration, such as locale.
String     CHANGE_NETWORK_STATE     Allows applications to change network connectivity state
String     CHANGE_WIFI_MULTICAST_STATE     Allows applications to enter Wi-Fi Multicast mode
String     CHANGE_WIFI_STATE     Allows applications to change Wi-Fi connectivity state
String     CLEAR_APP_CACHE     Allows an application to clear the caches of all installed applications on the device.
String     CLEAR_APP_USER_DATA     Allows an application to clear user data
String     CONTROL_LOCATION_UPDATES     Allows enabling/disabling location update notifications from the radio.
String     DELETE_CACHE_FILES     Allows an application to delete cache files.
String     DELETE_PACKAGES     Allows an application to delete packages.
String     DEVICE_POWER     Allows low-level access to power management
String     DIAGNOSTIC     Allows applications to RW to diagnostic resources.
String     DISABLE_KEYGUARD     Allows applications to disable the keyguard
String     DUMP     Allows an application to retrieve state dump information from system services.
String     EXPAND_STATUS_BAR     Allows an application to expand or collapse the status bar.
String     FACTORY_TEST     Run as a manufacturer test application, running as the root user.
String     FLASHLIGHT     Allows access to the flashlight
String     FORCE_BACK     Allows an application to force a BACK operation on whatever is the top activity.
String     GET_ACCOUNTS     Allows access to the list of accounts in the Accounts Service
String     GET_PACKAGE_SIZE     Allows an application to find out the space used by any package.
String     GET_TASKS     Allows an application to get information about the currently or recently running tasks.
String     GLOBAL_SEARCH     This permission can be used on content providers to allow the global search system to access their data.
String     HARDWARE_TEST     Allows access to hardware peripherals.
String     INJECT_EVENTS     Allows an application to inject user events (keys, touch, trackball) into the event stream and deliver them to ANY window.
String     INSTALL_LOCATION_PROVIDER     Allows an application to install a location provider into the Location Manager
String     INSTALL_PACKAGES     Allows an application to install packages.
String     INTERNAL_SYSTEM_WINDOW     Allows an application to open windows that are for use by parts of the system user interface.
String     INTERNET     Allows applications to open network sockets.
String     KILL_BACKGROUND_PROCESSES     Allows an application to call killBackgroundProcesses(String).
String     MANAGE_ACCOUNTS     Allows an application to manage the list of accounts in the AccountManager
String     MANAGE_APP_TOKENS     Allows an application to manage (create, destroy, Z-order) application tokens in the window manager.
String     MASTER_CLEAR   
String     MODIFY_AUDIO_SETTINGS     Allows an application to modify global audio settings
String     MODIFY_PHONE_STATE     Allows modification of the telephony state - power on, mmi, etc.
String     MOUNT_FORMAT_FILESYSTEMS     Allows formatting file systems for removable storage.
String     MOUNT_UNMOUNT_FILESYSTEMS     Allows mounting and unmounting file systems for removable storage.
String     NFC     Allows applications to perform I/O operations over NFC
String     PERSISTENT_ACTIVITY     This constant is deprecated. This functionality will be removed in the future; please do not use. Allow an application to make its activities persistent.
String     PROCESS_OUTGOING_CALLS     Allows an application to monitor, modify, or abort outgoing calls.
String     READ_CALENDAR     Allows an application to read the user's calendar data.
String     READ_CALL_LOG     Allows an application to read the user's call log.
String     READ_CONTACTS     Allows an application to read the user's contacts data.
String     READ_EXTERNAL_STORAGE     Allows an application to read from external storage
String     READ_FRAME_BUFFER     Allows an application to take screen shots and more generally get access to the frame buffer data
String     READ_HISTORY_BOOKMARKS     Allows an application to read (but not write) the user's browsing history and bookmarks.
String     READ_INPUT_STATE     This constant is deprecated. The API that used this permission has been removed.
String     READ_LOGS     Allows an application to read the low-level system log files.
String     READ_PHONE_STATE     Allows read only access to phone state.
String     READ_PROFILE     Allows an application to read the user's personal profile data.
String     READ_SMS     Allows an application to read SMS messages.
String     READ_SOCIAL_STREAM     Allows an application to read from the user's social stream.
String     READ_SYNC_SETTINGS     Allows applications to read the sync settings
String     READ_SYNC_STATS     Allows applications to read the sync stats
String     READ_USER_DICTIONARY     Allows an application to read the user dictionary.
String     REBOOT     Required to be able to reboot the device.
String     RECEIVE_BOOT_COMPLETED     Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.
String     RECEIVE_MMS     Allows an application to monitor incoming MMS messages, to record or perform processing on them.
String     RECEIVE_SMS     Allows an application to monitor incoming SMS messages, to record or perform processing on them.
String     RECEIVE_WAP_PUSH     Allows an application to monitor incoming WAP push messages.
String     RECORD_AUDIO     Allows an application to record audio
String     REORDER_TASKS     Allows an application to change the Z-order of tasks
String     RESTART_PACKAGES     This constant is deprecated. The restartPackage(String) API is no longer supported.
String     SEND_SMS     Allows an application to send SMS messages.
String     SET_ACTIVITY_WATCHER     Allows an application to watch and control how activities are started globally in the system.
String     SET_ALARM     Allows an application to broadcast an Intent to set an alarm for the user.
String     SET_ALWAYS_FINISH     Allows an application to control whether activities are immediately finished when put in the background.
String     SET_ANIMATION_SCALE     Modify the global animation scaling factor.
String     SET_DEBUG_APP     Configure an application for debugging.
String     SET_ORIENTATION     Allows low-level access to setting the orientation (actually rotation) of the screen.
String     SET_POINTER_SPEED     Allows low-level access to setting the pointer speed.
String     SET_PREFERRED_APPLICATIONS     This constant is deprecated. No longer useful, see addPackageToPreferred(String) for details.
String     SET_PROCESS_LIMIT     Allows an application to set the maximum number of (not needed) application processes that can be running.
String     SET_TIME     Allows applications to set the system time
String     SET_TIME_ZONE     Allows applications to set the system time zone
String     SET_WALLPAPER     Allows applications to set the wallpaper
String     SET_WALLPAPER_HINTS     Allows applications to set the wallpaper hints
String     SIGNAL_PERSISTENT_PROCESSES     Allow an application to request that a signal be sent to all persistent processes
String     STATUS_BAR     Allows an application to open, close, or disable the status bar and its icons.
String     SUBSCRIBED_FEEDS_READ     Allows an application to allow access the subscribed feeds ContentProvider.
String     SUBSCRIBED_FEEDS_WRITE   
String     SYSTEM_ALERT_WINDOW     Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications.
String     UPDATE_DEVICE_STATS     Allows an application to update device statistics.
String     USE_CREDENTIALS     Allows an application to request authtokens from the AccountManager
String     USE_SIP     Allows an application to use SIP service
String     VIBRATE     Allows access to the vibrator
String     WAKE_LOCK     Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming
String     WRITE_APN_SETTINGS     Allows applications to write the apn settings
String     WRITE_CALENDAR     Allows an application to write (but not read) the user's calendar data.
String     WRITE_CALL_LOG     Allows an application to write (but not read) the user's contacts data.
String     WRITE_CONTACTS     Allows an application to write (but not read) the user's contacts data.
String     WRITE_EXTERNAL_STORAGE     Allows an application to write to external storage
String     WRITE_GSERVICES     Allows an application to modify the Google service map.
String     WRITE_HISTORY_BOOKMARKS     Allows an application to write (but not read) the user's browsing history and bookmarks.
String     WRITE_PROFILE     Allows an application to write (but not read) the user's personal profile data.
String     WRITE_SECURE_SETTINGS     Allows an application to read or write the secure system settings.
String     WRITE_SETTINGS     Allows an application to read or write the system settings.
String     WRITE_SMS     Allows an application to write SMS messages.
String     WRITE_SOCIAL_STREAM     Allows an application to write (but not read) the user's social stream data.
String     WRITE_SYNC_SETTINGS     Allows applications to write the sync settings
String     WRITE_USER_DICTIONARY     Allows an application to write to the user dictionary.

Sunday, 1 July 2012

Android Slider Code example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:att="http://schemas.android.com/apk/res/com.att.control"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/textFieldslider"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a continuous Slider Control example."
          android:textStyle="bold"
          android:paddingBottom="10dp"/>
     <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider"
          att:max_value="100"
          att:min_value="0"
          att:divide_in="4"  
          att:current_value="5"/>
    <TextView android:id="@+id/slide_bar_1_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_1"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
    <TextView android:id="@+id/textFieldslider2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a segmented Slider Control example."
          android:textStyle="bold"
          android:layout_below="@id/slide_bar_1_text"
          android:paddingBottom="10dp"/>    
    <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider2"
          att:max_value="100"
          att:min_value="-100"
          att:divide_in="4"
          att:current_value="-50"/>
    <TextView android:id="@+id/slide_bar_2_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_2"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
    <TextView android:id="@+id/textFieldslider3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a disabled Slider Control example."
          android:textStyle="bold"
          android:paddingBottom="10dp"
          android:layout_below="@id/slide_bar_2_text"/>         
    <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider3"
          att:max_value="20"
          att:min_value="0"
          att:divide_in="2"
          att:current_value="5"/>
        
    <TextView android:id="@+id/slide_bar_3_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_3"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
  

</RelativeLayout>



Full layout code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:att="http://schemas.android.com/apk/res/com.att.control"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/textFieldslider"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a continuous Slider Control example."
          android:textStyle="bold"
          android:paddingBottom="10dp"/>
     <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider"
          att:max_value="100"
          att:min_value="0"
          att:divide_in="4"  
          att:current_value="5"/>
    <TextView android:id="@+id/slide_bar_1_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_1"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
    <TextView android:id="@+id/textFieldslider2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a segmented Slider Control example."
          android:textStyle="bold"
          android:layout_below="@id/slide_bar_1_text"
          android:paddingBottom="10dp"/>    
    <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider2"
          att:max_value="100"
          att:min_value="-100"
          att:divide_in="4"
          att:current_value="-50"/>
    <TextView android:id="@+id/slide_bar_2_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_2"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
    <TextView android:id="@+id/textFieldslider3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is a disabled Slider Control example."
          android:textStyle="bold"
          android:paddingBottom="10dp"
          android:layout_below="@id/slide_bar_2_text"/>         
    <com.att.widgets.lib.control.SliderControl
          android:id="@+id/slider_bar_3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/textFieldslider3"
          att:max_value="20"
          att:min_value="0"
          att:divide_in="2"
          att:current_value="5"/>
        
    <TextView android:id="@+id/slide_bar_3_text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/slider_bar_3"
          android:text="Value: 10"
          android:paddingBottom="10dp"/>
  

</RelativeLayout>

Activity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.att.widgets.lib.control.SliderControl;

public class SliderControlActivity extends Activity implements OnClickListener{

    private SliderControl slider_1, slider_2, slider_3;
    private TextView sliderText_1, sliderText_2, sliderText_3;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
        slider_1 = (SliderControl)findViewById(R.id.slider_bar_1);
        slider_2 = (SliderControl)findViewById(R.id.slider_bar_2);
        slider_2.setStep(50.0f);
        slider_3 = (SliderControl)findViewById(R.id.slider_bar_3);
      
        sliderText_1 = (TextView)findViewById(R.id.slide_bar_1_text);
        sliderText_2 = (TextView)findViewById(R.id.slide_bar_2_text);
        sliderText_3 = (TextView)findViewById(R.id.slide_bar_3_text);
      
        slider_2.setProgressFillColor(true);
        slider_3.setEnabled(false);
      
        slider_1.setOnClickListener(this);
        slider_2.setOnClickListener(this);
        slider_3.setOnClickListener(this);
      
    }

    @Override
    public void onClick(View v) {
        if (v == slider_1) {
            sliderText_1.setText("Value: " + slider_1.getCurrent());
        } else if (v == slider_2){
            sliderText_2.setText("Value: " + slider_2.getCurrent());
        } else if (v == slider_3){
            sliderText_3.setText("Value: " + slider_3.getCurrent());
        }
    }
}