I need to change the colour of a layout using the seek bar, from 0xC4DF9B to 0xA2CC61, say & am really struggling. Here is what I have so far:
XML:
<LinearLayout
android:id="#+id/tile1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#C4DF9B">
</LinearLayout>
Java:
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int seekValue = seekBar.getProgress();
tile1.setBackgroundColor() // ???
}
Related
I use SeekBar in my app with ticks. Currently this SeekBar is defined in xml file:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Base.Widget.AppCompat.SeekBar.Discrete"/>
and it looks ok but I want to define this element programmatically so I've added this instead:
SeekBar seekBar = new SeekBar(getContext(), null, R.style.Widget_AppCompat_SeekBar_Discrete);
but then the seekBar is invisible. Does anyone know what's wrong?
It's visible if I use only:
SeekBar seekBar = new SeekBar(getContext();
but i need ticks.
this works for me:
First, create a layout (e.g. layout_seekbar.xml):
<?xml version="1.0" encoding="utf-8"?>
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Widget.AppCompat.SeekBar.Discrete" />
then, you can programmatically add the seekbar in your code:
SeekBar bar = (SeekBar)LayoutInflater.from(this).inflate(R.layout.layout_seekbar, null);
bar.setMax(10);
...
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
...
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
myLinearLayout.addView(bar);
greetings, hope that helps
Inside the ImageView there is a picture.
I wish to zoom in the picture by adjusting the width and length of the ImageView.
Codes are listed here:
mZoomInBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
double zoomInRatio = (mZoomInBar.getProgress() / 25.00d) + 1;
mMapBed.getLayoutParams().height = (int)(zoomInRatio * mapOriginalHeight);
mMapBed.requestLayout();
mMapBed.getLayoutParams().width = (int)(zoomInRatio * mapOriginalWidth);
mMapBed.requestLayout();
}
});
The height is now zoom-able, but width don't even change a bit. I don't know why.
By the way, the xml part of the ImageView looks like this:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/nycsubway"
android:id="#+id/map">
</ImageView>
</HorizontalScrollView>
</ScrollView>
Any idea? Thanks a lot!
EDIT:
I've found out what the problem is: the layout.
If I put HorizontalScrollView before ScrollView, Width IS zoom-able, but Height is NOT zoom-able; ScrollView before HorizontalScrollView, Height IS zoom-able, but Width is NOT.
How to solve this issue...?
PROBLEM SOLVED!
Turns out, the image should be displayed using android:src, and android:adjustViewBounds="true" required in xml.
Try this
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_connt"
>
I have set property of seek bar . it reach at one process but its process not reach at 1 as like in image below
sbpassangers.setProgress(1); //set this at onCreate
xml :
<SeekBar
android:id="#+id/sBpassangers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/tvPassengers"
android:layout_toRightOf="#+id/ivPhone"
android:layout_weight="2"
android:paddingBottom="3dp"
android:paddingLeft="2dp"
android:paddingTop="3dp" />
java code :
sbpassangers.setProgress(1);
sbpassangers.setMax(sb1.getProgress());
tvtaxipassangers.setText(Integer.toString(1));
//sb1.setMax(9);
//sb1.setProgress(2); // Set it to zero so it will start at the left-most edge
sbpassangers.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress);
if(progress == 0){
seekBar.setProgress(1);
}
tvtaxipassangers.setText(Integer.toString(seekBar.getProgress()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
should be like this :
please help to solve this
Interchange the two lines
From
sbpassangers.setProgress(1);
sbpassangers.setMax(sb1.getProgress());
to
sbpassangers.setMax(sb1.getProgress());
sbpassangers.setProgress(1);
You cannot set the progress of seekbar unless you Defines the maximum value the progress can take.
The seekBar is an essential component which we are using on our application in customized way.
please make a demo project and follow the following code to your MainActivity
SeekbarActivity.java
package com.example.seekbarwidget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekbarActivity extends Activity {
TextView textview;
SeekBar seekbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seekbar);
textview = (TextView)findViewById(R.id.textView);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
seekbar.setMax(10);
seekbar.setProgress(5);
//initControls();
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//add here your implementation
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//add here your implementation
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
textview.setText(" value = " +Integer.toString(progress));
}
});
}
}
the activity_seekbar.xml is having following layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="125dp"
android:indeterminate="false"
android:max="10"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:progress="5"
android:progressDrawable="#drawable/styled_progress"
android:secondaryProgress="5"
android:thumb="#drawable/thumbler_small" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show seekbar value"
android:textColor="#CD2134"
android:textSize="27px"
android:textStyle="bold" />
</LinearLayout>
finally the styled_progress.xml inside drawable folder is having the lines
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+android:id/SecondaryProgress"
android:drawable="#drawable/progress_cyan"/>
<item
android:id="#+android:id/progress"
android:drawable="#drawable/progress_red"/>
</layer-list>
Custom Seek Bar
For exact answer you will have to provide more information. Taking a wild guess I would say that you need to set also the Max value,
setMax(int)
More information at http://developer.android.com/reference/android/widget/ProgressBar.html
have a SeekBar and I get the value of it whenever I scroll it. the position should then be the factor, how big the text in my spinner should be.
So I want to change the text size of my spinner items whenever I scroll not the 50sp as in my layout
My custom_list_row.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textViewRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textSize="50sp" />
My Adapter
ArrayAdapter<Language> adapter = new ArrayAdapter<Language>(this, R.layout.custom_list_row,
currentLanguages);
spinnerLanguage.setAdapter(adapter);
the Language class is a Object with a toString method.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
resizeViews(progress);
}
private void resizeViews(int progress){
textViewFirstName.setTextSize(20+progress);
//how can I do this for a spinner here?
}
You can use this.
private void resizeViews(int progress){
textViewFirstName.setTextSize(20+progress);
//this might be helpful
for(int i=0;i<spinnerLanguage.getChildCount();i++){
((TextView)spinnerLanguage.getChildAt(i)).setTextSize(20+progress);
}
}
I would like to create a seekbar for an Android app that allows the user to select a value between -5 and 5 (which maps to "strongly disagree" and "strongly agree"). How do I make a seekbar with discrete values? or is there a better UI widget I could use for this?
Thanks.
The Seekbar works great for discrete values. We use a Seekbar for discrete data as shown below. To show which item is selected, we just change the font of the selected text view so it is bigger. You could also highlight by changing the background color or something. It works pretty well. You will want to call setMax(11) on the seek bar, and then in your code you need to translate between the range (0 through 11) and (-5 through 5) appropriately.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:text="-5"
android:id="#+id/answerNegative5"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="-4"
android:id="#+id/answerNegative4"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="-3"
android:id="#+id/answerNegative3"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
.....
</LinearLayout>
<SeekBar android:id="#+id/intensitySlider"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
If you want to implement discrete seekbar with number of gaps without using third party library then use style property of seekbar.
<SeekBar
android:id="#+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:thumb="#drawable/ic_location"
android:theme="#style/Widget.AppCompat.SeekBar.Discrete" />
I don't know what is the issue here, you add a seekbar having a range of 0-10. Then you can map these values to -5 if you substract -5 from the selected value.
EDIT
add android:max="10" to the xml definiton and you get a fixed size seekbar.
You maybe should consider to add a textview to denote the current selected value's textual representation such as: Strongly Disagree.
To update this view, subscribe to onProgressChanged event and progress parameter will give you the chosen number.
SeekBar s = (SeekBar) findViewById(R.id.SeekBar);
s.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
}
}
Just use the material components library with something like:
<com.google.android.material.slider.Slider
android:stepSize="1"
android:valueFrom="-5"
android:valueTo="5"
/>
I hope this code surely helpes 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));
}
});
}
1-st step : set maxVal to 10;
2-nd step :
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
switch(seekBar.getId())
{
case R.id.mySeekBar:
int prValue = progress - 5;
editText.setText(String.valueOf(preValue));
break;
}
}
<SeekBar
android:id="#+id/seekbar"
style="#style/SeekBarWithoutSteps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:layout_marginTop="#dimen/margin_10"
android:max="4"
android:maxHeight="#dimen/margin_5"
android:minHeight="#dimen/margin_5"
android:paddingLeft="#dimen/margin_10"
android:paddingRight="#dimen/margin_10"
android:progressBackgroundTint="#color/colorGray"
android:progressTint="#color/colorGreen"
android:theme="#style/Widget.AppCompat.SeekBar.Discrete"
android:thumb="#drawable/filled_green"
android:thumbOffset="15dp" />