seekbar onProgressChange value updates extremely slow in toast - android

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Toast.makeText(this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
changeColor(viewOne, progress);
changeColor(viewTwo, progress);
changeColor(viewThree, progress);
changeColor(viewFour, progress);
changeColor(viewFive, progress);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
I am trying to update the value of the seekbar and display it in a toast. however, the toast updates the value extremely slowly. I would not like to use a textbox. Is there a way to accomplish this?

Using popup window.You can customize the view of popup window, like a textview to show the progress.
In method onStartTrackingTouch, show the popup window.
In method onStopTrackingTouch, dismiss the popup window.
In method onProgressChanged, change the progress.
Sample code:
package com.example.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private SeekBar seek;
private PopupWindow window;
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar) findViewById(R.id.seek);
seek.setOnSeekBarChangeListener(this);
window = new PopupWindow(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textview = new TextView(this);
textview.setTextSize(16);
textview.setBackgroundColor(Color.BLACK);
textview.setTextColor(Color.WHITE);
window.setContentView(textview);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
textview.setText("progress : " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.showAsDropDown(seek);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.dismiss();
}
}
Hope it helps.

Declare the toast at class level
Toast mToast;
Initialize the toast in constructor or onCreate
protected void onCreate(Bundle savedInstanceState) {
...
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
then when you need to show toast
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mToast.setText("progress : " + progress);
mToast.show();
}
now your toast updates will be instantaneous.

Related

Compiletime error while overriding OnstopTrackingTouch for setOnSeekBarChangeListener

I am trying to define setOnSeekBarChangeListener method which gives me an error to implement OnStopTouchTracking meethod. When I try to override it it throws an error saying cannot override method from superclass.
Am I missing any libraries to include for seekbar event listeners? or should the class be declared abstract?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
public class task4_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
setContentView(R.layout.task4_layout);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void OnstartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void OnstopTrackingTouch(SeekBar seekBar)
{
}
});
}
}
check methods inside listener, all methods are wrong.
It should be onStartTrackingTouch instead of OnstartTrackingTouch
It should be onStopTrackingTouch instead of OnstopTrackingTouch
you have written capital letter in starting of all the methods.

Android seekbar progress not update

I am using seekBar for soundpool volume control on my app, I use PopupWindow for seekbar and everything works fine except, whenever I change or pause my activity, and I open seekbar popupwindow again, the seekbar is no longer the value I last had saved. But it always returns to its default value 100%, even I use SharedPreferences what works well, but only if I do not leave current activity.
public class ActivityMain extends Activity implements OnTouchListener, OnMenuItemClickListener {
SoundManager snd;
OnSeekBarChangeListener barChange1;
private int soundID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_activity);
}
public void onRestart() {
super.onRestart();
}
public void onResume() {
super.onResume();
final Button btnOpenPopup = (Button)findViewById(R.id.button6);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
barChange1 = new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
snd.setVolume((float)progress/100.0f);
}
};
SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1);
volbar1.setMax(100);
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100);
volbar1.setProgress(value);
volbar1.setOnSeekBarChangeListener(barChange1);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 60, 20);
}});
{
}
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundManager(getApplicationContext());
soundID = snd.load(R.raw.sound_1);
ImageView img01 = (ImageView) findViewById(R.id.imageView11);
img01.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
snd.play(soundID);
return false;
}
});
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
#Override
protected void onPause() {
super.onPause();
snd.unloadAll();
}
}
Put this onResume()
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100)
volbar1.setProgress(value);
I am doing the same thing. Its working for me. Here is my code. It does not have all the Sound stuff.
package com.titlesource.testproject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;
public class ActivityMain extends AppCompatActivity {
SeekBar.OnSeekBarChangeListener barChange1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_main);
}
public void onRestart() {
super.onRestart();
}
public void onResume() {
super.onResume();
final Button btnOpenPopup = (Button)findViewById(R.id.button6);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
barChange1 = new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
};
SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1);
volbar1.setMax(100);
int value = 0;
SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 100);
volbar1.setProgress(value);
volbar1.setOnSeekBarChangeListener(barChange1);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 60, 20);
}});
{
}
}
}

shared preferences : float

something is wrong with it
package com.sahandxx3.Countries;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
/**
* Created by Setude on 17-Dec-15.
*/
public class Setting extends Activity {
SharedPreferences sh;
TextView txtflag,txt_test;
SeekBar sbflag;
int progress = 10;
float size=10;
float txt_size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
init();
txtflag.setText("Flag size : "+sbflag.getProgress());
sbflag.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
txt_size=sh.getFloat("size",0);
progress= i;
size=(progress*5)+5;
txtflag.setText("Flag size : "+progress);
txt_test.setTextSize(size);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
txtflag.setText("Flag size : "+progress);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
txtflag.setText("Flag size : "+progress);
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
SharedPreferences.Editor edit=sh.edit();
edit.putFloat("size",size);
edit.commit();
}
});
}
private void init() {
txtflag= (TextView) findViewById(R.id.txtflag);
sbflag= (SeekBar) findViewById(R.id.sbflag);
txt_test= (TextView) findViewById(R.id.btn_test);
}
}
so my problem is with this line:
txt_size=sh.getFloat("size",0);
When I try to change the seekbar,It stops working.
then I want to use txt_size in txt_test();
My best guess is that your SharedPreferences instance sh is null at the moment you try to access it. Try to move sh=PreferenceManager.getDefaultSharedPreferences(Setting.this); to your onCreate().
Put below code on onCreate():
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
Your onCreate() method should looks like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
..
..
}
Hope this will help you.
I think you should use
float size=10f;
txt_size=sh.getFloat("size",0f);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sh=PreferenceManager.getDefaultSharedPreferences(this);
}

Android Seek Bar Saved Preferences

Hi there I am trying to get an integer that will be given by a SeekBar using the .getprogress to go into an Integer which will get saved by the preference and will go to my toast code at another side of my application to make things easier heres my code already.
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import tk.mizzeeboy.freestorageconverter.R;
public class SettingsActivity extends Activity{
SeekBar cl;
TextView t;
String lolly , finlly;
int hehe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.333);
cl = (SeekBar) findViewById(R.id.seekB3r1);
t = (TextView) findViewById(R.id.textV3w1);
cl.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
t.setTextSize((cl.getProgress()) * getResources().getDisplayMetrics().density);
hehe = cl.getProgress();
finlly.valueOf(hehe);
SharedPreferences mypreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mypreferences.edit();
editor.putString("TeamName", finlly);
editor.commit();
}
});
}
}
and in my other activity I want to open and read it heres the code for that
SharedPreferences mypreferences = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
String teamnamestring = mypreferences.getString("TeamName", "Please Enter :)");
Toast andEggs = Toast.makeText(BitActivity.this, teamnamestring , Toast.LENGTH_SHORT);
andEggs.show();
But my problem is that it I am getting the Please Enter Sign everytime and it will not pick up my integer can someone help?
What TGMCians is saying is that you put hehe as Int in the preferences (editor.putInt("TeamName", hehe);), in first Activity then get it as String in the other Activity (mypreferences.getString("TeamName",...). This will cause ClassCastException. Either, you put it as a String from the begining or get it as an Int in the second Activity.
Try:
int val=mypreferences.getInt("TeamName",-1);
String teamnamestring="";
if(val==-1){
teamnamestring="Please Set Up Your Custom Text Size!";}
else {teamnamestring=String.ValueOf(val);}
Toast andEggs = Toast.makeText(BitActivity.this, teamnamestring , Toast.LENGTH_SHORT);
andEggs.show();

SeekBar prog not running

A Simple SeekBar Program is not running.I'm not sure what i'm missing.
The java file is given as
package com.tree;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class tree extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SeekBar sb=(SeekBar)findViewById(R.id.sb);
final TextView tv=new TextView(this);
sb.setOnSeekBarChangeListener(new SeekBar.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
tv.setText(progress);
}
});
}
}
and the layout xml file is given as
Any help would be appreciated.The error thrown is "that the Program has stopped unexpectedly".
The problem is in tv.setText(progress);.
setText(int) expects a resource identifier, not an integer.
Instead use tv.setText(Integer.toString(progress)); to convert to a string first.

Categories

Resources