0

I am current working through a SeekBar example located here. This example has one seek bar, but I wanted to see what would happen if I added two more seek bars.

While the listeners all seem to still work, I want to add the name of the SeekBar to the toast that shows up while tracking the the touch bar. However, I can't just add the name of the variable to the toast ( the passed in seekBar is a widget ) and this doesn't fit the function call of toString().

How would I add the name of the seekBar that I'm focusing on to the Toast?

MainActivity.java:

package com.javatpoint.seekbar;
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.widget.SeekBar;  
import android.widget.SeekBar.OnSeekBarChangeListener;  
import android.widget.Toast;  
public class MainActivity extends Activity implements OnSeekBarChangeListener{  
    SeekBar seekBar1, seekBar2, seekBar3;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);  

      seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
      seekBar1.setOnSeekBarChangeListener(this);
      seekBar2=(SeekBar)findViewById(R.id.seekBar2);
      seekBar2.setOnSeekBarChangeListener(this);
      seekBar3=(SeekBar)findViewById(R.id.seekBar3);
      seekBar3.setOnSeekBarChangeListener(this);
}  
@Override  
public void onProgressChanged(SeekBar seekBar, int progress,  
        boolean fromUser) {
    //This is where I want to print out the variable name
    Toast.makeText(getApplicationContext(), "seekbar progress: "+progress, Toast.LENGTH_SHORT).show();
}  
@Override  
public void onStartTrackingTouch(SeekBar seekBar) {  
    Toast.makeText(getApplicationContext(),seekBar +"seekbar touch started!", Toast.LENGTH_SHORT).show();
}  
@Override  
public void onStopTrackingTouch(SeekBar seekBar) {  
    Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
}  
@Override  
public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.main, menu);  
    return true;  
 }  
}  

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="120dp" />
<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_marginTop="50dp"
    android:layout_below="@id/seekBar1"/>
<SeekBar
    android:id="@+id/seekBar3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_marginTop="50dp"
    android:layout_below="@id/seekBar2"/>

Projava
  • 21
  • 6

1 Answers1

0

Someone posted a link to this post last night detailing the use of tags to solve my problem, but unfortunately they either removed their post or it was deleted before I could mark it as the correct answer.

I was able to use the info in that post to set a string as the tag for each seekBar, and then pass that into a new function that took the tag as well as the progress from the seekbar to show different toasts for each seekbar. Here was the final version of the MainActivity

package com.javatpoint.seekbar;
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.widget.SeekBar;  
import android.widget.SeekBar.OnSeekBarChangeListener;  
import android.widget.Toast;  
public class MainActivity extends Activity implements OnSeekBarChangeListener{  
SeekBar seekBar1, seekBar2, seekBar3;
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
    seekBar1.setOnSeekBarChangeListener(this);
    seekBar1.setTag("1");
    seekBar2=(SeekBar)findViewById(R.id.seekBar2);
    seekBar2.setOnSeekBarChangeListener(this);
    seekBar2.setTag("2");
    seekBar3=(SeekBar)findViewById(R.id.seekBar3);
    seekBar3.setOnSeekBarChangeListener(this);
    seekBar3.setTag("3");
}  
@Override  
public void onProgressChanged(SeekBar seekBar, int progress,  
        boolean fromUser) {
    changeValue(seekBar.getTag(), progress);
}  
@Override  
public void onStartTrackingTouch(SeekBar seekBar) {  
    Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();
}  
@Override  
public void onStopTrackingTouch(SeekBar seekBar) {  
    Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
}  
@Override  
public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.main, menu);  
    return true;  
}
public void changeValue(Object a, int progress){
    if(a=="1"){
        Toast.makeText(getApplicationContext(), "seekbar1 progress: "+progress, Toast.LENGTH_SHORT).show();
    }
    if(a=="2"){
        Toast.makeText(getApplicationContext(), "seekbar2 progress: "+progress, Toast.LENGTH_SHORT).show();
    }
    if(a=="3"){
        Toast.makeText(getApplicationContext(), "seekbar3 progress: "+progress, Toast.LENGTH_SHORT).show();
    }
}

}

Projava
  • 21
  • 6