How do I get values from a SeekBar?
I have a code of a class that has three SeekBars (PRICEbar)
I want to pass the values of these SeekBars to next Activity (screen)
as Intents
I know how toimplement OnClickListener but how can I extract the
values from the SeekBars?
ex: if PRICEbar is pointing to value 10 ... I need to get the value of 10
Filters.java
public class Filters extends Activity implements OnSeekBarChangeListener{
// declare text objects variables
private SeekBar PRICEbar;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
Button back;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(this);
PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEtextProgress.setText("Price:: Rs "+progress);
seekBar.setMax(100);
}
});
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar == PRICEbar)
PRICEtextProgress.setText("Price:: Rs "+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
The SeekBar class is a subclass of ProgressBar, which contains a getProgress() method.
Calling PRICEbar.getProgress() will return the value you are looking for.
Related
I need to make multiple seek bar in the same Java class. However, I wonder do we need to set each setOnSeekBarChangeListener for each seek bar? The code seems too long if I am doing so (I need to create 10 seek bar in the same class). Is there any other solution which I can shorten the code? Thank you so much for helping. :)
Below is my code
private SeekBar seekbar,seekbar1;
private TextView a,b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schiassess);
seekbar = (SeekBar) findViewById(R.id.delubar);
seekbar1 = (SeekBar) findViewById(R.id.disorbar);
a = findViewById(R.id.a);
b = findViewById(R.id.b);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
a.setText("" + progress);
a.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
b.setText("" + progress);
b.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
Follow the below steps it's simple
JUST IMPLEMENT SeekBar.OnSeekBarChangeListener in your class/fragment
public class Home extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
}
You don't have to write new OnSeekBarChangeListener for Every Seekbar...
Just Implement
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
} to your Activity/Fragment..
Now you can check for the id and perform action according to it
this is my code....
StudentLogin.java class
public class StudentLogin extends Activity implements OnClickListener, OnItemSelectedListener{
Spinner sp1,sp2;
EditText reg;
Button b1,b2,b3;
String a1,a2,studreg,studdept,studyr;
SQLiteDatabase db1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_login);
reg=(EditText) findViewById(R.id.editText1);
sp1=(Spinner)findViewById(R.id.spinner1);
sp2=(Spinner)findViewById(R.id.spinner2);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Cursor c=db1.rawQuery("Select * from StudReg", null); // Im getting an error here
int count=c.getCount();
int flag=0;
c.moveToFirst();
String regno=reg.getText().toString();
String yr=a1;
String dept=a2;
Toast.makeText(getApplicationContext(), regno, Toast.LENGTH_LONG).show();
if(regno.trim().length()>0&&!yr.equals("Select Year")&&!dept.equals("Select Dept"))
{
for(int i=0;i<count;i++)
{
studreg=c.getString(c.getColumnIndex("register"));
studdept=c.getString(c.getColumnIndex("deptmt"));
studyr=c.getString(c.getColumnIndex("year"));
if(regno.equals(studreg)&&yr.equals(studyr)&&dept.equals(studdept))
{
flag=1;
break;
}
c.moveToNext();
}
c.close();
if(flag==1)
{
Intent inn=new Intent(StudentLogin.this,StudyMaterials.class);
startActivity(inn);
}
else
{
Toast.makeText(getApplicationContext(), "Invalid User!\nKindly try again..", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Kindly fill all the fields..", Toast.LENGTH_SHORT).show();
}
}
});
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intt=new Intent(StudentLogin.this,StudentRegister.class);
startActivity(intt);
}
});
b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent int1=new Intent(StudentLogin.this,Update.class);
startActivity(int1);
}
});
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p, View v,
int a, long l) {
// TODO Auto-generated method stub
a1=p.getItemAtPosition(a).toString();
//Toast.makeText(getApplicationContext(), a1, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p, View v,
int a, long l) {
// TODO Auto-generated method stub
a2=p.getItemAtPosition(a).toString();
//Toast.makeText(getApplicationContext(), a2, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
List<String> categories = new ArrayList<String>();
categories.add("Select Department");
categories.add("CSE");
categories.add("IT");
categories.add("CIVIL");
categories.add("MECH");
categories.add("ECE");
categories.add("EEE");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(dataAdapter);
List<String> categories1 = new ArrayList<String>();
categories1.add("Select Year");
categories1.add("I");
categories1.add("II");
categories1.add("III");
categories1.add("IV");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(dataAdapter1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.student_login, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> p, View v, int it,
long l) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
This code validates whether the user is already registered or not..
Please help me solve this error...
you need to initialize database
//dbHelper its a helper class for database
database = dbHelper.getWritableDatabase();
You have given the table name but you are not saying it on which db the table exists(as db1 is not initialized).So follow #Madhur answer to solve this null pointer exception.Also a suggestion when working with db(better to use loaders instead of working on MainThread).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been having issues with formatting, in the sense that my code is not properly formatted. I tried performing right click -> source -> format, a function of my SDK that auto-formats curly braces), and I tried reorganizing things, but my attempts haven't been too successful.
In the following lines
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
It really depends on the IDE you are using and I know from my experience with using Eclipse that it does a nice job of formatting code for you, but only if there are no errors in your code. Check and make sure that you do not have any errors and it should work.
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
You have an anonymous inner class and you didn't close your ) bracket
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
I tried placing the interface in my Activity, my fragment and my FragmentStatePagerAdapter. But in all of them the code never got there. this is what my FragmentStatePagerAdapter looks like since this is my latest try.
FragmentStatePagerAdapter :
public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
Fragment would look like this :
public class SectionFragment extends ListFragment implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
But both didnt work, it would be most usefull to me to use it in a fragment but if that is not possible then the activity or statepager will work too
In AppSectionsPagerAdapter constructor: add this
mViewPager.setOnPageChangeListener(this);
(this assume the ViewPager is an instance variable named mViewPager)
Or, as an alternative in the MainActivity.onCreate() :
mViewPager.setOnPageChangeListener(mAppSectionPageAdapter);
(this assume the AppSectionPageAdapter is in a variable named mAppSectionPageAdapter)
I want to call MyView.circle() method when I click on menu Circle but when I'm clicking on menu it is throwing a NullPointerException, which means that the object is not initialized as I expected.
Here is my code. Where am I going wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,"Circle");
menu.add(0,1,2,"Rect");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case 1:
MyView.circle();
break;
}
return super.onOptionsItemSelected(item);
}
public static class MyView extends View {
private Context context;
static Canvas can=null;
public MyView(Context c) {
super(c);
context=c;
}
#Override
protected void onDraw(Canvas canvas) {
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
//circle(canvas);
}
public static void circle() {
// TODO Auto-generated method stub
can.drawCircle(50, 50, 50, mPaint);
}
}
}
public void static circle(c) {
needs to be changed to
public void static circle(Canvas c) {
Might I recommend spending some time learning Java before writing Android apps?