I am creating a brightness control dialog. However the problem I am having is that when I put textview then seekbar it causes this error:
android.widget.SeekBar cannot be cast to android.widget.TextView
For some reason it seems to work when the positioning on .xml follows seekbar then textview.
Here is some code:
LayoutInflater li_brightness = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View v_brightness = li_brightness.inflate(R.layout.options_dialog_brightness, (ViewGroup)findViewById(R.id.ll_brightness_layout));
bright_textView = (TextView)v_brightness.findViewById(R.id.brightness_text);
brightBar = (SeekBar)v_brightness.findViewById(R.id.seekbar_brightness);
confirm_brightness = (Button)v_brightness.findViewById(R.id.button_brightness);
cResolver = getContentResolver();
window = getWindow();
brightBar.setMax(100);
brightBar.setProgress(50);
brightBar.setKeyProgressIncrement(1);
bright_textView.setText("50".toString());
brightBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
float BackLightValue = (float)progress/100;
if(BackLightValue <= 0){
bright_textView.setText(String.valueOf(BackLightValue));
}
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
});
Try cleaning your project (Project > Clean from Eclipse or ant clean from the command line), and see if that clears up your problem. Sometimes the build system gets a bit out of sync with itself in terms of the R values, and this sort of error is one of the symptoms.
Related
I want to resize image by using seekbar. Seekbar is working but its not giving any good results. With the following code, the image is resized but just when you change the progress of seekbar for the first time. Please help
Seekbar listener
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
// TODO Auto-generated method stub
progresValue = progress;
resizeImage(image, progresValue);
}
});
Function to resize image
public void resizeImage(Bitmap bitmap, int progress) {
Bitmap bitmapSource = bitmap;
float bHeight = bitmapSource.getHeight();
float bWidth = bitmapSource.getWidth();
float factorH = progress / (float) bHeight;
float factorW = progress / (float) bWidth;
float factorToUse = (factorH > factorW) ? factorW : factorH;
Bitmap bm = Bitmap.createScaledBitmap(bitmapSource,
(int)(bWidth * factorToUse), (int)(bHeight * factorToUse),
false);
qImage.setImageBitmap(bm);
}
final SeekBar sk1 = (SeekBar) findViewById(R.id.seekBar1);
sk1.setMax(500);
sk1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(progress,progress);
img.setLayoutParams(params);
img.setMinimumWidth(progress);
img.setMinimumHeight(progress);
}
});
In my app I added a SeekBar which controls the screen brightness. It works some what fine .
I get the device's screen brightness in Oncreate() and set the progress of the SeekBar.
But when I change the seekbar progress , when I set it to the minimum(ie. to the zero position) the device gets stuck and I had to restart my device to get it working again.
What is wring with my code. Please help me
in oncreate() I have the following code,
try {
BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);
brightnessscrollbar.setProgress(BackLightValue);
System.out.println("Baclightvalue is "+BackLightValue);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekbarclicklistener
brightnessscrollbar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
});
I came to know that Device gets stuck at the value zero
So I gave a condition on onProgresschanged as folows
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
if(arg1>5){
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
}
And it works now, like a charm.
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
progress = progressValue;
if(progress <1 ) {
progress = 1;
}
android.provider.Settings.System.putInt(getActivity().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getActivity().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = progress / (float)255;
window.setAttributes(layoutParams);
}
I am using a seekbar, and I want to add certain marks in the seekbar with text to use it as a scale. The selected interval points should be highlighted.
The corresponding code is here:
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setMax(max);
seekbar.setProgress(50);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
value.setText("SeekBar value is " + progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
To make the above seek bar you first have to add a measuring scale as a background drawable. Then you'll have to make textboxes for each interval on the measuring scale.
<SeekBar
....
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb"
....
/>
Then
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = ((int)Math.round(progress/interval))*interval;
seekBar.setProgress(progress);
if(progress==5) textBox5.setTextColor(Color.RED);
else if(progress==10) textBox10.setTextColor(Color.RED);
//......... for all the text boxes.
}
Actually, all The above solutions force to select the last progress point, which is not logic.
To make it mire accurete, Seek bar must detect which is the nearest dot and jump to it.
So below is the solution,
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progressStep = getMax() / dotsCount;
int lastDotProgress = Math.round(getProgress() / progressStep) * progressStep;
int nextDotProgress = lastDotProgress + progressStep;
int midBetweenDots = lastDotProgress + (progressStep / 2);
if (getProgress() > midBetweenDots)
seekBar.setProgress(nextDotProgress);
else
seekBar.setProgress(lastDotProgress);
}
You can use same behavior in onProgressChanged() if you want to prevent seeking between dots
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int s_size=5;
progress = ((int)Math.round(progress/s_size))*s_size;
seekBar.setProgress(progress);
textview.setText(progress + "");
}
what you want to settext get easily get from progress and
textview.settext() value
I have a seekbar for example: from 0 to 10. How can I make if I want to set seekbar to 5 as default position. So in this example seekbar should start from middle.
Use this one
android:progress="5"
Like "progress", it inherits some of it's attributes from ProgressBar. You find all the attributes at http://developer.android.com/reference/android/widget/SeekBar.html
To get Seekbar at the value of 5
In the xml layout use the max value as 10
android:max="10"
In the java for seekbar to get at progress 5
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setProgress(5);
thats it :)
I hope this code surely helps you.
Try this...
float discrete=0;
float start=0;
float end=100;
float start_pos=0;
int start_position=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=-10; //you need to give starting value of SeekBar
end=10; //you need to give end value of SeekBar
start_pos=5; //you need to give starting position value of SeekBar
start_position=(int) (((start_pos-start)/(end-start))*100);
discrete=start_pos;
SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
seek.setProgress(start_position);
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete), Toast.LENGTH_SHORT).show();
}
#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
// To convert it as discrete value
float temp=progress;
float dis=end-start;
discrete=(start+((temp/100)*dis));
}
});
}
default value By xml :
android:progress="5"
max value by xml
android:max="10"
final Timer timer = new Timer();
seekBar_efficiency.setMax(40);
seekBar_efficiency.setProgress(35);
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar_efficiency.setProgress(20);
timer.cancel();
}
}, 200, 50);
i have made a seekbar zoom controler for my map view as follows
myZoomBar = (SeekBar)findViewById(R.id.zoombar);
SetZoomLevel();
myZoomBar.setOnSeekBarChangeListener(myZoomBarOnSeekBarChangeListener);
and
private SeekBar.OnSeekBarChangeListener myZoomBarOnSeekBarChangeListener =
new SeekBar.OnSeekBarChangeListener(){
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
SetZoomLevel();
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private void SetZoomLevel()
{
int myZoomLevel = myZoomBar.getProgress()+1;
mc.setZoom(myZoomLevel);
Toast.makeText(this,
"Zoom Level : " + String.valueOf(myZoomLevel),
Toast.LENGTH_LONG).show();
};
my question is how can i set the default view level to 15? As it is right now when the map opens it is zoomed out all the way.
try below code
MaView mapView = (MapView)findViewById(R.id.my_map);
MapController mapController = mapView.getController();
mapController.setZoom(15);
Use setZoom() method of MapController class in your MapActivity .