Thursday 15 March 2012

Update Progress Bar using Buttons OR Spinner


(UI Layout)showdata.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp" />
    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:max="100"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:progress="0"/>
    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignParentLeft="true"
        android:text="-" />
    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignParentRight="true"
        android:text="+" />
</RelativeLayout>


Activity( ShowSpinner .java)

package com.surya.spinner;

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

public class ShowSpinner extends Activity implements OnClickListener {
ProgressBar bar;
Button plus, minus;
Spinner spinner;
int i;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdata);
plus = (Button) findViewById(R.id.button2);
plus.setOnClickListener(this);
minus = (Button) findViewById(R.id.button1);
minus.setOnClickListener(this);
bar = (ProgressBar) findViewById(R.id.progress);
String aList[] = getResources().getStringArray(R.array.values);
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, aList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Object item = parent.getItemAtPosition(pos);
Toast.makeText(getApplicationContext(), "selcted" + item,
Toast.LENGTH_SHORT).show();
bar.setProgress(Integer.parseInt(item.toString()));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

}

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

i = bar.getProgress();
switch (v.getId()) {
case R.id.button1:
if (i != 0)
i = i - 10;
setValues();
break;
case R.id.button2:
if (i != 100)
i = i + 10;
setValues();
break;

}
}

void setValues() {
for (int j = 0; j <= 10; j++) {
if (i == (j * 10)) {
spinner.setSelection(j);
bar.setProgress(i);
}
}
}

}

Strings.Xml
<resources>
<string-array name="values">
        <item>0</item>
        <item>10</item>
        <item>20</item>
        <item>30</item>
        <item>40</item>
        <item>50</item>
        <item>60</item>
        <item>70</item>
        <item>80</item>
        <item>90</item>
        <item>100</item>
    </string-array>
</resources>

Output Screenshot:



Please Add valuable Comments on this.........








No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...