private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (p < 20) {
sb.setProgress(p);
}
}
});
}
So the problem is that the code I have above changes the text size of a text view. However when going back and forth the state isn't saved so the text size is then reset. I am really new to android and would appreciate any guidance or help on how I can achieve this. I basically have a tutorial app and the text needs to change from the settings menu which has a seekbar allowing the user to select what text size they want. Any Ideas?
You can do either in this way :
1)you can save the value of p in shared pref and when you come next time you will check the value of p and set it on textview
2)you can save the value in onSavedInstance,but the problem is that when user kill the app from background you are going to loose the value
if you have set sb.setMax(20); then you dose not need to
if (p < 20) {
sb.setProgress(p);
}
code is useless user cant seek more then 20
you have code like that
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(CONTEXT);
int p = SP.getInt("key", p);
tutorialText.setTextSize(p);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sb.setProgress(p);
Editor edit = SP.edit();
edit.putInt("key", p);
edit.commit();
}
});
}
Related
I'm trying to build an apps to capture picture from usb camera, using UVCCamera from https://github.com/saki4510t/UVCCamera
But, i didn't know, how to implement image adjustment setting (like Adjust Brightness, Contrast, White Balance) in this library.
I've tried to using seekbar to adjust brightness setting, and this is my code :
final UVCCamera camera = new UVCCamera();
private final OnSeekBarChangeListener mSeekBarChangeListener = new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (mCameraHandler.isOpened()) //When USB Camera, Connected
{
camera.setBrightness(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
};
And, if i try to change value of seekbar, the value has changed, but it doesn't change the brightness level.
Can anybody explain me, how to change the image adjustment in this library or give me correction about my code?
Any answers will be apreciate from me
Regards, and have a good day everyone :)
I updated the files in my project from the new version of the library (libuvccamera, usbCameraCommon) and modified the code from the example 8. I gave a sample code. Here is an example of the code that I got.
private SeekBar.OnSeekBarChangeListener seekBarChangeListener =
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
br = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (isActive()) {
setValue(seekBar.getProgress());
}
}
// TODO Auto-generated method stub
};
private int setValue( final int value) {
return mCameraHandler != null ? mCameraHandler.setValue(value) : 0;
}
private boolean isActive() {
return mCameraHandler != null && mCameraHandler.isOpened();
}
And edit AbstractUVCCameraHandler.java
public int setValue( final int value) {
checkReleased();
final CameraThread thread = mWeakThread.get();
final UVCCamera camera = thread != null ? thread.mUVCCamera : null;
if (camera != null) {
camera.setBrightness(value);
return camera.getBrightness();
}
throw new IllegalStateException();
}
i have a app in which brightness is save in profile. profile active when selected wifi is connected . i save the setting of brightness with shareprefrences.when i save and connected to wifi in the seekbar show value same which i set on it but its brightness does not increase or decrease which i set in setting . brightness increase or decrease only when i change it manually with seekbar.please tell me where i do mistake here is the code of activity.
public class Profile1Activity extends Activity {
//TextViews to show details of volume and brightness
private TextView tVBrightness, tVVolume;
//SeekBars to set volume and brightness
private SeekBar sbVolume, sbBrightness = null;
//Variable to store brightness value
private int brightness = 1;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
EditText e1;
Button b1;
public static final String MyPREFERENCESSS = "MyPrefsss";
public static final String HOMEWIFI = "homewifi";
Context context = this;
SharedPreferences sharedpreferences;
public static final String BRIGHTNESSS = "brightnessProfile1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile1);
sbBrightness = (SeekBar) findViewById(R.id.sbBrightness1);
tVBrightness = (TextView) findViewById(R.id.tVBrightness);
e1 = (EditText) findViewById(R.id.ed2);
b1 = (Button) findViewById(R.id.but2);
sharedpreferences = getSharedPreferences(MyPREFERENCESSS, Context.MODE_PRIVATE);
final String homewifi = sharedpreferences.getString(HOMEWIFI, "");
e1.setText(homewifi);
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
});
////////////////////////////////////////////////////////////////////////////////////
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo conn = wifiManager.getConnectionInfo();
//Toast.makeText(MainActivity.this, con.getSSID()+"",Toast.LENGTH_LONG).show();
if (conn.getSSID().toString().equalsIgnoreCase("\"" + homewifi + "\"")) {
initializeControls1();
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
}
}
handler.postDelayed(this, 100);
}
}, 100);
}
private void initializeControls1() {
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
sbBrightness.setMax(255);
//Set the seek bar progress to 1
sbBrightness.setKeyProgressIncrement(1);
try
{
//Get the current system brightness
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
sbBrightness.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
sbBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
run1();
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set the minimal brightness level
//if seek bar is 20 or any value below
if(progress<=20)
{
//Set the brightness to 20
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
tVBrightness.setText("Brightness: "+(int)perc +" %");
}
});
}
public void run1(){
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
// Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
}
set brightness also at a time you set value of seekbar in prefrance
Add this code... only this much code is needed to increase and set default value of phone brightness to seekbar when your profile fragment or activity loads:
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
// if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}
I got an edit text and also a rating bar in my program. I am checking if the user has more than 3 characters in the edit text and for rating bar it should have something greater than 0.5 star (basically to check if they at least clicked on any of them).
final EditText commentbox = (EditText)findViewById(R.id.comment);
final RatingBar rating = (RatingBar)findViewById(R.id.rating);
final Button feedbackbutton = (Button)findViewById(R.id.submit);
final TextWatcher watcher = new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(commentbox.length() >= 3 && rating.getNumStars() >=0.5){
feedbackbutton.setEnabled(true);
feedbackbutton.setBackgroundColor(Color.parseColor("#369742"));
} else {
feedbackbutton.setEnabled(false);
feedbackbutton.setBackgroundColor(Color.parseColor("#ffcacaca"));
}
}
};
commentbox.addTextChangedListener(watcher);
rating.setOnRatingBarChangeListener((this));
So as you can see it has to meet those condition in order for a button to become enabled and also to change its background colour. However soon as I write more than 3 characters in the edit text, the button enables itself and changes its colour. It's not looking for the rating bar condition.
Can someone help me please
To get rating from rating value:
float ratingValue = rating.getRating();
Add listener :
rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar arg0, float rateValue, boolean arg2) {
// TODO Auto-generated method stub
Log.d("Rating", "your selected value is :"+rateValue);
}
});
feedbackbutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//Getting the rating and displaying it on the toast
String rating=String.valueOf(rating.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
Hope it solves your problem.
I am trying to take the value of a seekBar and if that value is within a certain range to change the textView.
if (olivine.getProgress()==50) {
rock.setText("Olivine rich");
};
This is what I have tried but it does not change the textView.
olivine.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar bar) {
int value = bar.getProgress();
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onProgressChanged(SeekBar bar,
int paramInt, boolean paramBoolean) {
olivineper.setText("" + paramInt + "%");
}
});
The above puts the seekBar value into a textView. I have multiple seekBars in the activity if that makes a difference.
Why don't you put your logic into the OnSeekBarChangeListener?
public void onProgressChanged(SeekBar bar, int paramInt, boolean paramBoolean) {
if (paramInt==50) {
rock.setText("Olivine rich");
};
}
So I have code that adjusts System Brightness and I have properly implemented the seek bar but when I adjust the seekbar the Screen Brightness does not change. I looked into a few other posts and the solution seems to be to create a Dummy class that opens and closes really quickly. If I do that though then I feel like there would be a ton of lag because the screen would be refreshing constantly as the seekbar is moving.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ContentResolver cr = getContentResolver();
try{
int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
}
catch (Settings.SettingNotFoundException a)
{
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
Also another problem with the refresh is that I am running this brightness control in a screen where there is a camera preview and killing the camera each time the dummy screen loads would take some time and further increase the lag
I did see this post but I couldnt figure out how to implement it. Since I am not using preferences
I have a seekbar in my preferences to update screen brightness. Is there a way to refresh the UI to see the live change?
I solved the problem with the following code it now works as intended!
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (float)arg1/100;
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
Use my following code it perfectly running :
in your whatever xml file :
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:max="10"
android:progressDrawable="#drawable/progress"
android:scaleType="fitCenter"
android:thumb="#drawable/thumb" />
note : take attributes as much you need in seekbar.
in your whatever activity use following method and call that :
private void setSeekbar()
{
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}