How can I get this code implemented with my ActionBar Tab setup? - android

I recently changed my application to have the action bar tabs to switch between Fragments. I coding I have done for the first fragment which is on the main activity is working fine, but on the second Tab it displays the items in its corresponding XML file but, none of my Java code seems to be working. I want 3 seek bars to change the number of a Text View, I had it working when I wasn't using Fragments, but just cant get it to work.
Do i have to put the code in a certain place of this file?
public class CountdownFragmentTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.countdown, container, false);
}
}
And this is what i want to add to this file:
SeekBar seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
SeekBar seekBar3 = (SeekBar)findViewById(R.id.seekBar3);
final TextView seekBar1Value = (TextView)findViewById(R.id.seekbar1value);
final TextView seekBar2Value = (TextView)findViewById(R.id.seekbar2value);
final TextView seekBar3Value = (TextView)findViewById(R.id.seekbar3value);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar1, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar1Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar2, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar2Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar3, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar3Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}

Related

SeekBar Android

How to hide/Show CheckBox,EditText on onProgressChanged of SeekBar.
First when Actvity is Created that time Hide a CheckBox,EditText. & when we Changed the Progress of SeekBar Show the CheckBox,EditText
In oncreate() hide your edittext and checkbox.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edittext.setVisibility(View.GONE);
checkbox.setVisibility(View.GONE);
}
And In setOnSeekBarChangeListener's onProgressChanged() show your checkbox and edittext. show below:-
seekbarObj.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean isUSer) {
// TODO Auto-generated method stub
edittext.setVisibility(View.VISIBLE);
checkbox.setVisibility(View.VISIBLE);
}
});
You can add seekBar listener and then do operations as per you need based on the progress value
SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);
sk.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
//Change visibility here based on "progress" value
}
});

Android SeekBar: "setOnSeekBarChangeListener", = expected after this token

I'm pretty new to android, and I'm trying to implement SeekBar,
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(new BatteryBar());
private class BatteryBar implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// To be implemented....
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
but I always get error
Syntax error on token "setOnSeekBarChangeListener", = expected after this token
according to Android SeekBar setOnSeekBarChangeListener or http://webtutsdepot.com/2011/12/03/android-sdk-tutorial-seekbar-example/ it should be correct way, how to do it.. Thanks for any help
try to do it like this without creating new class
batteryBar.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
}
}
One thing I noticed with the code you posted is that you're trying to implement your Seekbar listener as an inner class. If you want your Activity to implement the the OnSeekBarChangeListener it should look this :
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class example extends Activity implements OnSeekBarChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(this);
//this references your class that is implementing OnSeekBarChangeListener
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
If you don't have any specific reasoning for using an inner class then I would suggest using the code below. You will have access to the same methods. It also removes the need for an inner class. This also gives you the advantage of having mutliple OnSeekBarChangeListener() as different variables for different seekbars. If you implement OnSeekBarChangeListener() on a class then you have to go through handling different cases for different Seekbars.
SeekBar.OnSeekBarChangeListener BatteryBar = 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
}
};
I hope this helps
UPDATE:
public class demo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(BatteryBar);
}
SeekBar.OnSeekBarChangeListener BatteryBar = 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
}
};
}

Android app: how to turn off listener and turn it back on again?

say for example I have a progress bar and in onCreate activity I am setting up onProgressChanged listener.
sbt.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
//recalcpriceornot=false;
// TODO Auto-generated method stub
//Toast.makeText(ProductDetailsInfoActivity.this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
//if (recalcpriceornot)
{ GetItemPriceCalculation(progress);
recalctotalprice();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if(progress <= 100){
setProgressBarColor(sbt,Color.GREEN);
}else{
setProgressBarColor(sbt,Color.RED);
}
}
});
My other controls (price box) adjust progress and progress bar adjusts price box therefor it will be going in a loop.
i can turn off listener by
sbt.setOnSeekBarChangeListener(null);
but to turn it back on do I have to re-write the whole part for listener. is There a better way of doing it??
thanks a lot
Instead of inline creating the SeekBar.OnSeekBarChangeListener, create a member variable of type SeekBar.OnSeekBarChangeListener. When you want it to turn it on, you can pass the variable to it. When you want to turn it off, you can pass it the null.
private SeekBar sbt; //Not sure where this gets initialized in your code.
private SeekBar.OnSeekBarChangeListener mListener = new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
//recalcpriceornot=false;
// TODO Auto-generated method stub
//Toast.makeText(ProductDetailsInfoActivity.this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
//if (recalcpriceornot)
{ GetItemPriceCalculation(progress);
recalctotalprice();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if(progress <= 100){
setProgressBarColor(sbt,Color.GREEN);
}else{
setProgressBarColor(sbt,Color.RED);
}
}
});
void enableListener()
{
sbt.setOnSeekBarChangeListener(mListener);
}
void disableListener()
{
sbt.setOnSeekBarChangeListener(null);
}
You can also let this object implement/extends the OnSeekBarChangeListener and not keep a variable to the listener. In which case the enable will pass in "this".
Your activity can implement SeekBar.OnSeekBarChangeListener and use this to reference the listener when you want to turn on the SeekBar:
import android.app.Activity;
import android.database.SQLException;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private int progressChanged = 0;
private SeekBar sbt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbt = (SeekBar) findViewById(R.id.seekBar1);
// Turns OFF
sbt.setOnSeekBarChangeListener(null);
// Turns ON
sbt.setOnSeekBarChangeListener(this);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// 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) {
// recalcpriceornot=false;
// TODO Auto-generated method stub
// Toast.makeText(ProductDetailsInfoActivity.this,
// String.valueOf(progress), Toast.LENGTH_SHORT).show();
try {
// if (recalcpriceornot)
{
GetItemPriceCalculation(progress);
recalctotalprice();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressChanged = progress;
if (progress <= 100) {
setProgressBarColor(sbt, Color.GREEN);
} else {
setProgressBarColor(sbt, Color.RED);
}
}
}
Yes. You can store the listener in an instance variable and then reuse it in multiple places in your code.
For example, create an instance variable in your class like this:
private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Toast.makeText(ProductDetailsInfoActivity.this,"seek bar progress:"+progressChanged,
// Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
}
// other methods, etc
});
Then you can set the listener whenever you feel like it:
// remove listener
sbt.setOnSeekBarChangeListener(null);
// reattach it later
sbt.setOnSeekBarChangeListener(mSeekBarChangeListener);
Instead of implementing the listener inline you can implement it separately.
private SeekBar.OnSeekBarChangeListener mySeekBarListener = new SeekBar.OnSeekBarChangeListener() {
};
Now every time you need to set a listener, go with:
sbt.setOnSeekBarChangeListener(mySeekBarListener);
Edit:
You can also separate the listener implementation out into another class. Then you can create an object of that class and use it in your set method. Another way to do the same thing if you'd like

How to access seek bar unimplemented methods within onCreate() for android?

I have customized seek bar in my application. I can drag the seek bar and access the unimplemented methods too. And my code is
public class Income extends Activity implements OnSeekBarChangeListener, OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
net_sales1.setOnSeekBarChangeListener(this);
cogs.setOnSeekBarChangeListener(this);
operating.setOnSeekBarChangeListener(this);
other_revenue.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
seek_count=0;
progress1=progress-150;
verify = fromUser;
if (fromUser)
{
if (progress >= 0 && progress <= seekBar.getMax())
{
progressString = String.valueOf(10*progress-150);
}
}
seek_id=seekBar.getId();
switch(current_spinner)
{
case R.id.mySeekingBar1 :
//tablelay.removeView(tableLayout);
seek_txt1.setText(progressString);
int seekbar1_val = Integer.parseInt(seek_txt1.getText().toString());
progress_of_spinner1 = progress;
break;
case R.id.mySeekingBar2 :
seek_txt2.setText(progressString);
progress_of_spinner2 = progress;
break;
case R.id.mySeekingBar3 :
seek_txt3.setText(progressString);
progress_of_spinner3 = progress;
break;
case R.id.mySeekingBar4 :
seek_txt4.setText(progressString);
progress_of_spinner4 = progress;
break;
}
net_sales1.setProgress(progress_of_spinner1);
cogs.setProgress(progress_of_spinner2);
operating.setProgress(progress_of_spinner3);
other_revenue.setProgress(progress_of_spinner4);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
int progress2 = progress1;
originalProgress=seekBar.getProgress();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
progress2=progress1;
}
Now my query is, is it possible to access all unimplemented methods of seek bar within onCreate() method of an activity? Can any body tell me? Thanks in advance.
You want to use in
onCreate
than you need to remove
implements OnSeekBarChangeListener
and below line
net_sales1.setOnSeekBarChangeListener(this);
cogs.setOnSeekBarChangeListener(this);
operating.setOnSeekBarChangeListener(this);
other_revenue.setOnSeekBarChangeListener(this);
and give separate
OnSeekBarChangeListener
for all seekbar than and than you use in onCreate
But in this for all seek bar all method override separately.
See below code for this
net_sales1.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
}
});
cogs.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
}
});
operating.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
}
});
other_revenue.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
}
});
yes, its possible, use as below:
Either set onSeekBarChangeListener to all seek bars and and provide definition of unimplemented inline. As below:
public class Income extends Activity implements OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
net_sales1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
cogs.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
operating.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});// Do same as cogs
other_revenue.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});//Do Same as cogs
}
}
Another option to create a listener object, in onCreate method, and set this listener to all seekbar, as below:
public class Income extends Activity implements OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
OnSeekBarChangeListener listener= new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
net_sales1.setOnSeekBarChangeListener(listener);
cogs.setOnSeekBarChangeListener(listener);
operating.setOnSeekBarChangeListener(listener);// Do same as cogs
other_revenue.setOnSeekBarChangeListener(listener);//Do Same as cogs
}
}

Seekbar error in loading the progress on start of the activity

On the start of the activity I have set the progress to 8 ;
so the thumb is on 8 but the background is not on 8 , After changing the seekbar it working fine but on start of the activity it's not...
Thanks in advance...
Code is:-
public static int time=8;
oncreate(){
SeekBar sb=(SeekBar)findViewById(R.id.SB);
TextView tv=(TextView)findViewById(R.id.tv);
sb.setMax(20);
sb.setProgress(time);
sb.setOnSeekBarChangeListener(new SeekBar.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
time=progress;
tv.setText(time+"");
}
});
}
Try to set the max value first. Then try to set the progress.

Categories

Resources