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);
}
}
Related
I have 3 spinner for country, timezone and language with the respective labels arranged one below another in below manner
Label1 Spinner1
Label2 Spinner2
Label3 Spinner3
When Click on say Spinner1, I want entire row i.e. Label and spinner to change their background color, Say if I take it in a LinearLayout So on spinner click The background should change to blue
I want probably a xml way of changing background of whole layout and not programmatic as I will have to code for each spinner
Can someone tell me how do I change entire layout color on just spinner click
I tried Spinner.setOnItemSelectedListener, but that is hitting only when I select some item, but I want the layout to change color even on press of Spinner
Spinner in layout
layout.xml
<LinearLayout
android:id="#+id/linearlay"
android:layout_width="320dp"
android:layout_height="40dp"
android:background="#drawable/bg_spinner_pressed">
<TextView
android:id="#+id/label"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="Label"
android:textColor="#color/textColorPrimary"
android:textSize="14dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupBackground="#color/colorPrimary"
android:textColor="#android:color/white"
android:textSize="20sp"/>
</LinearLayout>
Settings.java file
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("Techie","----------------------------citizenship1----------------------------");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("Techie","----------------------------citizenship2----------------------------");
}
});
I expect the Layout with id linearlay should change its primary color white to change to Blue on spinner click. please help
Call setonTouchListener and handle down and up events to change the colors
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("Techie", "----------------------------Action down----------------------------");
layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color1));
break;
case MotionEvent.ACTION_CANCEL:
Log.e("Techie", "----------------------------Cancel----------------------------");
layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color2));
break;
}
// allow target view to handle click
return false;
}
});
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() // ???
}
I am having a problem with the spinner item colour.
I am fetching the Spinner items from database.
I changed the spinner item colour.
But when I select a Spinner item, the colour will change to white again.
Here is my code:
public class PersonalInformation extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info)
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,R.array.spinner_array,R.layout.spinner_text);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
And Here is my Spinner xml file code:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="20dp">
</TextView>
Make custom xml file for your spinner item.
spinner_item.xml:
give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set drop down resource.it will take spinner_item.xml only to show your items in spinner.
How to change spinner text size and text color?
I have created an xml page that holds 2 textviews and a seekbar all without ids.
The class CustomSeekBar creates these objects using the xml page as a basic structure.
You can see space for the textviews on my emulator, but I am having a hard time figuring out to set the text. Obviously I am missing something, because there is no way for the CustomSeekBar class to be able to tell which textview I want to set the text for.
How do I set the text of each individual view without giving each textview a hardcoded ID?
The reason I say without a hardcoded ID, is because if each textview is named, then when one textview's text needs to be changed, won't all the textview's texts, with that ID, change?
How would I call the specific textview ID since my customseekbar class is in a composite relationship with the activity?
Activity that calls everything.
public class ColorsActivity extends ListActivity {
/** Called when the activity is first created. */
//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;
//List that will host our items and allow us to modify that array adapter
ArrayList<CustomSeekBar> seekBarArrayList=null;
// TextView myValueText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbarlist);
//Initialize ListView
ListView lstTest= getListView();
//Initialize our ArrayList
seekBarArrayList = new ArrayList<CustomSeekBar>();
//Initialize our array adapter
seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars, seekBarArrayList);
CustomSeekBar red = new CustomSeekBar(this, "red", 1);
//CustomSeekBar blue = new CustomSeekBar(this, "blue");
//CustomSeekBar green = new CustomSeekBar(this, "green");
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(seekBarAdaptor);
seekBarArrayList.add(red);
//seekBarArrayList.add(blue);
//seekBarArrayList.add(green);
Amarino.connect(this, "00:11:11:21:05:53");
}
}
CustomSeekBar class
public class CustomSeekBar implements SeekBar.OnSeekBarChangeListener {
Context myContext;
TextView myValue;
TextView myLabel;
SeekBar mySeekBar;
CustomSeekBar(Context context, String label, int ID){
myContext = context;
myValue = new TextView(myContext);
mySeekBar = new SeekBar(myContext);
myValue.setText(label);
mySeekBar.setOnSeekBarChangeListener(this);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
myValue.setText(progress);
Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress);
}
public void onStopTrackingTouch(SeekBar seekBar){
}
public void onStartTrackingTouch (SeekBar seekBar){
}
}
seekbarlist.xml holds my list view for the custom list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
seekbars.xml is the structure of each custom list item (CustomSeekBar)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBarLayout">
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="255"/>
</LinearLayout>
why would you have to name them the same ID name why not
#+id/textview1 and #+id/textview2 and then reference the two text boxes in your code I don't understand what is stopping you?
You can use IDs as #bmporter12 said. You can have duplicate IDs, provided that Android has a place to start looking from when you tell it to findViewById. So, in your adapter, in getView(), you would inflate your new row from seekbars.xml and then do row.findViewById(R.id.textView1) and row.findViewById(R.id.textView2).
If you need to set it from outside the adapter, then depending on where you're getting the signal to set a TextView, either your CustomSeekBar could ask the Activity for its entry at a particular position in the adapter or it could use the View parameter passed in an onClick callback.
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" />