Seekbar with custoim Listview in android
In this tutorial I'll help you to understand Seekbar in Listview. In this example, we’ll see how to create and customize Seekbar in custom Listview.
In Android, a Seekbar is a
ProgressBar
element’s extension that
allows the selection of integer values using a natural user
interface. SeekBar
has a thumb that can
be slided in order to choose a value between 0 and some maximum that
can be set from the developer. A typical usage of Seekbar is your
device brightness control and volume control.We can add a Seekbar widget using element.
android:max
property is basically used to set a maximum integer value for
selection using Seekbar . android:progress
property is basically used to set a integer value for Seekbar
progress.In this tutorial I've create an data.xml file and put it in the assets folder. Firstly I've parse the data from that xml file and then set the data in custom listview using Seekbar .
Put data.xml in assets folder
data.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<PurchaseOrder>
<ItemDetail>
<ID>1</ID>
<ProductName>TV</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>2</ID>
<ProductName>Fridge</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>3</ID>
<ProductName>Heater</ProductName>
<Price>8</Price>
</ItemDetail>
<ItemDetail>
<ID>4</ID>
<ProductName>Laptop</ProductName>
<Price>8</Price>
</ItemDetail>
<ItemDetail>
<ID>5</ID>
<ProductName>Lamp</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>6</ID>
<ProductName>Geyser</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>7</ID>
<ProductName>Toaster</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>8</ID>
<ProductName>Game
Console</ProductName>
<Price>8</Price>
</ItemDetail>
<ItemDetail>
<ID>9</ID>
<ProductName>Mixer</ProductName>
<Price>5</Price>
</ItemDetail>
<ItemDetail>
<ID>10</ID>
<ProductName>Washing</ProductName>
<Price>8</Price>
</ItemDetail>
</PurchaseOrder>
layout_seekitems.xml
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:background="#fff"
android:orientation="horizontal"
android:padding="5dp"
>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
>
<TextView
android:id="@+id/seekName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="name"
/>
<TextView
android:id="@+id/seektxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0
hrs" />
</LinearLayout>
<SeekBar
android:id="@+id/seekbarList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll1"
android:layout_marginBottom="5dp"
android:max="20"
android:layout_marginTop="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:progress="0"
android:progressDrawable="@drawable/progressbar"
android:secondaryProgress="0"
android:thumb="@drawable/seek_handler"
/>
</RelativeLayout>
activity_seek.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fafafa"
>
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#ccc"
android:dividerHeight="0dp"
android:visibility="visible"
>
</ListView>
</RelativeLayout>
progressbar.xml
<?xml
version="1.0"
encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@android:id/background">
<shape
android:shape="line"
>
<corners
android:radius="5dp"
/>
<stroke
android:width="2dp"
android:color="#D0CFCF"
/>
<gradient
android:angle="270"
android:endColor="#D0CFCF"
android:startColor="#D0CFCF"
/>
</shape>
</item>
<item
android:id="@android:id/secondaryProgress">
<clip>
<shape
android:shape="line"
>
<corners
android:radius="5dp"
/>
<stroke
android:width="2dp"
android:color="#9E9E9E"
/>
<gradient
android:angle="270"
android:endColor="#9E9E9E"
android:startColor="#9E9E9E"
/>
</shape>
</clip>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape
android:shape="line"
>
<corners
android:radius="5dp"
/>
<stroke
android:width="2dp"
android:color="#9E9E9E"
/>
<gradient
android:angle="270"
android:endColor="#9E9E9E"
android:startColor="#9E9E9E"
/>
</shape>
</clip>
</item>
</layer-list>
Here are the java code files.
OrderXMLHandler,java
package
com.pack.seekbar;
import
java.util.ArrayList;
import
org.xml.sax.Attributes;
import
org.xml.sax.SAXException;
import
org.xml.sax.helpers.DefaultHandler;
public
class
OrderXMLHandler extends
DefaultHandler {
boolean
currentElement
= false;
String
currentValue
= "";
String
cartId;
String
customerId;
String
email;
ProductInfo
productInfo;
ArrayList<ProductInfo>
cartList;
public
String getCartId() {
return
cartId;
}
public
String getCustomerId() {
return
customerId;
}
public
String getEmail() {
return
email;
}
public
ArrayList<ProductInfo> getCartList() {
return
cartList;
}
public
void
startElement(String uri, String localName, String qName,
Attributes
attributes) throws
SAXException {
currentElement
= true;
if
(qName.equals("PurchaseOrder")){
cartList
= new
ArrayList<ProductInfo>();
}
else
if
(qName.equals("ItemDetail"))
{
productInfo
= new
ProductInfo();
}
}
public
void
endElement(String uri, String localName, String qName)
throws
SAXException {
currentElement
= false;
if
(qName.equalsIgnoreCase("ID"))
productInfo.setSeqNo(currentValue.trim());
else
if
(qName.equalsIgnoreCase("ProductName"))
productInfo.setItemName(currentValue.trim());
else
if
(qName.equalsIgnoreCase("ImagesName"))
productInfo.setImgName(
currentValue.trim());
else
if
(qName.equalsIgnoreCase("Price"))
productInfo.setPrice(currentValue.trim());
else
if
(qName.equalsIgnoreCase("ItemDetail"))
cartList.add(productInfo);
currentValue
= "";
}
public
void
characters(char[]
ch, int
start, int
length)
throws
SAXException {
if
(currentElement)
{
currentValue
= currentValue
+ new
String(ch, start, length);
}
}
}
ProductInfo.java
package
com.pack.seekbar;
public
class
ProductInfo {
String
seqNo =
null;
String
itemName =
"";
String
price = "";
String
id = "";
public
String getId() {
return
id;
}
public
void
setId(String id) {
this.id
= id;
}
public
String getSeqNo() {
return
seqNo;
}
public
void
setSeqNo(String seqNo) {
this.seqNo
= seqNo;
}
public
String getItemName() {
return
itemName;
}
public
void
setItemName(String itemNumber) {
this.itemName
= itemNumber;
}
public
String getPrice() {
return
price;
}
public
void
setPrice(String price) {
this.price
= price;
}
}
ItemList.java
package
com.pack.seekbar;
public
class
ItemList {
public
String ID
;
public
String Name
;
public
int
Price ;
public
int
value ;
public
String getID() {
return
ID;
}
public
void
setID(String iD) {
ID
= iD;
}
public
String getName() {
return
Name;
}
public
void
setName(String name) {
Name
= name;
}
public
int
getPrice() {
return
Price;
}
public
void
setPrice(int
price) {
Price
= price;
}
public
int
getSeekValue() {
return
value;
}
public
void
setSeekValue(int
valuee) {
value
= valuee;
}
}
ItemDetail.java
package
com.pack.seekbar;
public
class
ItemDetail {
public
String ID
;
public
String Name
;
public
int
Price ;
public
int
value ;
public
String getID() {
return
ID;
}
public
void
setID(String iD) {
ID
= iD;
}
public
String getName() {
return
Name;
}
public
void
setName(String name) {
Name
= name;
}
public
int
getPrice() {
return
Price;
}
public
void
setPrice(int
price) {
Price
= price;
}
public
int
getValue() {
return
value;
}
public
void
setValue(int
valuee) {
value
= valuee;
}
}
SeekActivity.java
package
com.pack.seekbar;
import
java.io.InputStream;
import
java.util.ArrayList;
import
javax.xml.parsers.SAXParser;
import
javax.xml.parsers.SAXParserFactory;
import
org.xml.sax.InputSource;
import
org.xml.sax.XMLReader;
import
android.app.Activity;
import
android.content.Context;
import
android.content.res.AssetManager;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ListView;
import
android.widget.SeekBar;
import
android.widget.SeekBar.OnSeekBarChangeListener;
import
android.widget.TextView;
import
com.pack.moneyhoney.utill.ItemDetail;
import
com.pack.moneyhoney.utill.ItemList;
public
class
SeekActivity
extends
Activity {
ArrayList<ItemList>
ItemList ;
SeeklistAdapter
adapter;
public
static
ListView listView;
public
static
ArrayList<ItemDetail> ItemDetailList
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seek);
listView
= (ListView) findViewById(R.id.list2);
ItemDetailList
= new
ArrayList<ItemDetail>();
ItemList
= new
ArrayList<ItemList>();
parseXML();
}
private
void
parseXML() {
AssetManager
assetManager = getBaseContext().getAssets();
try
{
InputStream
is = assetManager.open("data.xml");
SAXParserFactory
spf = SAXParserFactory.newInstance();
SAXParser
sp = spf.newSAXParser();
XMLReader
xr = sp.getXMLReader();
OrderXMLHandler
myXMLHandler = new
OrderXMLHandler();
xr.setContentHandler(myXMLHandler);
InputSource
inStream = new
InputSource(is);
xr.parse(inStream);
ArrayList<ProductInfo>
cartList = myXMLHandler.getCartList();
for(ProductInfo
productInfo: cartList){
ItemList
mList = new
ItemList();
mList.setID(productInfo.getId()
);
mList.setName(
productInfo.getItemName() );
mList.setPrice(
Integer.parseInt( productInfo.getPrice() ) );
mList.setSeekValue(
0 );
ItemList.add(mList);
}
is.close();
adapter
= (new
SeeklistAdapter(SeekActivity.this,
ItemList));
listView.setAdapter(adapter);
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
class
SeeklistAdapter extends
BaseAdapter {
ArrayList<ItemList>
names = new
ArrayList<ItemList>();
Context
context;
private
LayoutInflater inflater
= null;
public
SeeklistAdapter(Context c, ArrayList<ItemList> itemNameList) {
this.names
= itemNameList;
context
= c;
inflater
= (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public
int
getCount() {
//
TODO
Auto-generated method stub
return
names.size();
}
@Override
public
Object getItem(int
position) {
//
TODO
Auto-generated method stub
return
position;
}
@Override
public
long
getItemId(int
position) {
//
TODO
Auto-generated method stub
return
position;
}
@Override
public
View getView(final
int
position, View contextview, ViewGroup parent) {
//
TODO
Auto-generated method stub
View
View = inflater.inflate(R.layout.layout_seekitems,
null);
SeekBar
seekbar = (SeekBar) View.findViewById(R.id.seekbarList);
final
TextView seekVal = (TextView) View.findViewById(R.id.seektxt);
TextView
seekName = (TextView) View.findViewById(R.id.seekName);
if(
names.get(position).getSeekValue()
== 0 ){
seekbar.setProgress(
0 );
seekVal.setText(""
+ names.get(position).getSeekValue()
+ " hrs");
}
else
{
seekbar.setProgress(
names.get(position).getSeekValue());
seekVal.setText(""
+ names.get(position).getSeekValue()
+ " hrs");
}
seekName.setText(""
+ names.get(position).getName());
seekbar.setTag(Integer.valueOf(position));
seekbar.setId(position);
seekbar.setOnSeekBarChangeListener(new
OnSeekBarChangeListener() {
@Override
public
void
onStopTrackingTouch(SeekBar seekBar) {
int
id = seekBar.getId();
String
name = names.get(id).getName();
ItemDetail
itemDtl = new
ItemDetail();
itemDtl.setID(""
+ id );
itemDtl.setName(""
+ name);
itemDtl.setPrice(
names.get(id).getPrice()
* names.get(id).getSeekValue()
);
itemDtl.setValue(
names.get(id).getSeekValue()
);
ItemDetailList.add(itemDtl);
}
@Override
public
void
onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public
void
onProgressChanged(SeekBar seekBar, int
progress,
boolean
fromUser) {
//
TODO
Auto-generated method stub
seekVal.setText(""
+ progress + " hrs");
names.get(position).setSeekValue(progress);
}
});
return
View;
}
}
}
Here is the output of the program.
This was an example of SeekBar in Android.
Have a nice day
Happy coading !!
Comments
Post a Comment