It works well when I try to Change text size in (EditText) by putting a constant value in code <<<<
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Text = (EditText)findViewById(R.id.editText);
SeekBar f = (SeekBar)findViewById(R.id.seekBar);
Text.setTextSize(55);
Text.setText("Hello");
}
But I Can't get it to work by using SeekBar:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Text = (EditText)findViewById(R.id.editText);
SeekBar f = (SeekBar)findViewById(R.id.seekBar);
Text.setTextSize((float)f.getProgress());
Text.setText("Hello");
}
You can change the text size using this listener:
f.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
Text.setTextSize(progress);
}
Related
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
}
});
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
}
};
}
I need the code to show and hide the notification bar during an activity.
I try this code:
public void goFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
public void goNonFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
This doesn't work after the content is adding...
Remove the following line from Android Manifest file.
android:theme="#android:style/Theme.Black.NoTitleBar" and check .
Hope it will help you.
This is the code i have used to check it .
public class Main extends Activity {
Button magic,button1,button2;
TextView display;
int random;
private Long startTime=System.currentTimeMillis();
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ssss);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
goFullscreen();
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
goNonFullscreen();
}
});
}
public void goFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
public void goNonFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
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
}
}
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
}
});
}