Change the image transparency using a SeekBar - java

I have to do an Android Studio project that it has to change the transparency of an ImageView using a SeekBar.
I have this program:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int seekTransparent;
ImageView color;
SeekBar seekBarTransparent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color = (ImageView)findViewById(R.id.changeColor);
seekBarTransparent = (SeekBar) findViewById(R.id.seekBar);
updateNow();
seekBarTransparent.setOnSeekBarChangeListener(seekBarChangeListener);
}
private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new
SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
updateNow();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast in uso",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast non in uso",
Toast.LENGTH_SHORT).show();
}
};
private void updateNow(){
seekTransparent = seekBarTransparent.getProgress();
color.getBackground().setAlpha(192 + seekTransparent * 0x10000);
}
}
I run the application but on my smartphone it doesn't work anyone can help me?

This is the xml of the seekbar
<SeekBar
android:id="#+id/opacitybar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="10"
android:progress="10"
android:layout_alignTop="#+id/opacitylbl"
android:layout_centerHorizontal="true" />
And this is the code of the Activity
logoseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int opacity = 100; // from 0 to 255
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
im_move_zoom_rotate.setAlpha(progresValue * 25);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can change the values if you want

Related

MediaPlayer App is crashing when i try to run

I'm beginner in android development and tried to create a simple mediaplayer app which plays media from local source.
But it's crashing when i try to open it.
This is my java code.
package com.example.media;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
Button play,pause;
SeekBar s;
MediaPlayer m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m=MediaPlayer.create(this,R.raw.live);
play=findViewById(R.id.button3);
pause=findViewById(R.id.button4);
s.findViewById(R.id.seekBar);
s.setMax(m.getDuration());
s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)m.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m.pause();
}
});
}
}
You need to initialize the seekbar object instead of calling the findViewById method.
So just replace the dot with equal sign :
s=findViewById(R.id.seekBar);

music is not playing smoothly when updating seekbar every 1 second

After adding Timer() method seekbar should be updated every second, it is getting updated but their is a small pause like updating seekbar require a time to update... this has cause seekbar is not running smoothly gives a feel that there is a lag...
package com.example.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
AudioManager audioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this,R.raw.senorita);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int myMaxValume= audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int myCurrentValume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar valumeRocker = findViewById(R.id.seekBar);
valumeRocker.setMax(myMaxValume);
valumeRocker.setProgress(myCurrentValume);
valumeRocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//timeline part of music app
final SeekBar timeline = findViewById(R.id.timeline);
timeline.setMax(mediaPlayer.getDuration());
//set onChange listner on timeline
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//customise timeline seekbar
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}
, 0, 1000);
}
public void playMe(View view){
mediaPlayer.start();
}
public void pauseMe(View view){
mediaPlayer.pause();
}
}
if i remover Timer method.. every this is fine ... i guess i have done something woring in this ...
help me out
thank you in advance..
You need to check 'fromUser' variable is true or not on seekbar progress changed. Because you will get seekbar change callback in both cases, when the user change manually and when Timer update seekbar progress.
Updated code:-
package com.example.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
AudioManager audioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this,R.raw.senorita);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int myMaxValume= audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int myCurrentValume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar valumeRocker = findViewById(R.id.seekBar);
valumeRocker.setMax(myMaxValume);
valumeRocker.setProgress(myCurrentValume);
valumeRocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//timeline part of music app
final SeekBar timeline = findViewById(R.id.timeline);
timeline.setMax(mediaPlayer.getDuration());
//set onChange listner on timeline
timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//customise timeline seekbar
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timeline.setProgress(mediaPlayer.getCurrentPosition());
}
}
, 0, 1000);
}
public void playMe(View view){
mediaPlayer.start();
}
public void pauseMe(View view){
mediaPlayer.pause();
}
}

Dynamically change the status of a ProgressBar based on two TextFields

I'm trying to change the status of a ProgressBar while the user is typing a text on either text fields.
For example , the user starts typing on the first TextView, the ProgressBar should change it's value, if he deletes the text, it should reset to the previous value which is 30
package com.example.phill.jokes;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Entry extends AppCompatActivity {
private ProgressBar pr;
private Handler rHandler = new Handler();
private TextView tv ;
private TextView tv2 ;
private boolean titleboolean;
private boolean jokeboolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
pr = (ProgressBar) findViewById(R.id.progressBar);
tv=(TextView) findViewById(R.id.textView9) ;
tv2=(TextView) findViewById(R.id.textView6) ;
new Thread(new Runnable() {
#Override
public void run() {
while (true)
{
android.os.SystemClock.sleep(300);
rHandler.post(new Runnable() {
#Override
public void run() {
pr.setProgress(30);
if(tv.getText().toString().matches(""))
{
titleboolean=true;
}
else
titleboolean=false;
if(tv2.getText().toString().matches(""))
{
jokeboolean=true;
}
else
jokeboolean=false;
if(titleboolean)
{
pr.setProgress(60);
}
if(jokeboolean)
{
pr.setProgress(100);
}
}
});
if(titleboolean && jokeboolean)
break;
}
rHandler.post(new Runnable() {
#Override
public void run() {
if(titleboolean && jokeboolean){
tv2.setVisibility(View.INVISIBLE);
tv.setVisibility(View.VISIBLE);
}
;
}
});
}
}).start();
}
}
The value of the ProgressBar never changes.
Any Ideas?
Let me explain you in detail
if(titleboolean && jokeboolean)
break;
when your both text fields clears it calls break statement after that your thread stops running you can achieve this in another way
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ProgressBar;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity
{
private ProgressBar pr;
private EditText tv;
private EditText tv2;
private boolean titleboolean;
private boolean jokeboolean;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
pr=(ProgressBar)findViewById(R.id.progressBar);
tv = (EditText) findViewById(R.id.tv);
tv2 = (EditText) findViewById(R.id.tv2);
TextView tV = new TextView(this);
tV.addTextChangedListener(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) {
setProgress();
}
});
tv2.addTextChangedListener(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) {
setProgress();
}
});
}
public void setProgress() {
pr.setProgress(30);
if (tv.getText().toString().matches("")) {
titleboolean = true;
} else
titleboolean = false;
if (tv2.getText().toString().matches("")) {
jokeboolean = true;
} else
jokeboolean = false;
if (titleboolean) {
pr.setProgress(60);
}
if (jokeboolean) {
pr.setProgress(100);
}
}
}
Use EditText instead TextView with TextChangedListener.
TextView does not allow to type text.
Also infinite cycle with pauses is bad approach, use EditText.addTextChangedListener to handle text after it have changed

Surface view Black Screen Home Button

i am developing one app where just show clouds walking through the screen using surface view everything gone good until push the Button "HOME BUTTON" so when i returned to my app just showing screen in black, the rest of the app works fine just is when you push the "Home Button"
Where is my Code for the surfaceView
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class mysurfaceview extends SurfaceView implements SurfaceHolder.Callback {
private mysurfaceviewThread bbThread = null;
private int xPosition = (getHeight()/2);
private int yPosition = (getHeight()/2);
private int posicion2 = (getWidth()/2)-40;
private int posicion3 = (getWidth()/4);
private int xDirection = 1;
public mysurfaceview(Context ctx,AttributeSet attrs){
super(ctx, attrs);
getHolder().addCallback(this);
}
public void doDraw(Canvas c){
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
c.drawColor(Color.WHITE);
c.drawBitmap(icon, xPosition, yPosition, new Paint());
c.drawBitmap(icon,posicion2,(getHeight()/2),new Paint());
c.drawBitmap(icon,posicion3,(getHeight()/2)+40,new Paint());
}
public void surfaceCreated(SurfaceHolder holder){
if (bbThread!=null) return;
bbThread = new mysurfaceviewThread(getHolder());
bbThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
public void surfaceDestroyed (SurfaceHolder holder){
bbThread.detener();
}
public class mysurfaceviewThread extends Thread{
private boolean corriendo,pausa;
private SurfaceHolder surfaceHolder;
public mysurfaceviewThread(SurfaceHolder surfaceHolder){
this.surfaceHolder = surfaceHolder;
}
public synchronized void pausar() {
pausa = true;
}
public synchronized void reanudar() {
pausa = false;
notify();
}
public void detener() {
corriendo = false;
if (pausa)
reanudar();
}
public void run(){
corriendo=true;
while(corriendo){
xPosition+=xDirection;
posicion2+=2;
posicion3+=1;
if(xPosition==getWidth()){
xPosition = -200; }
if(posicion2==getWidth()){
posicion2 = -200; }
if(posicion3==getWidth()){
posicion3 = -200; }
Canvas c = null;
try {
c = surfaceHolder.lockCanvas();
doDraw(c);
synchronized (surfaceHolder){
}
} finally {
if(c != null) surfaceHolder.unlockCanvasAndPost(c);}
synchronized (this) {
while (pausa) {
try {
wait();
} catch (Exception e) {
}
}
}
}
}
}
public mysurfaceviewThread getThread(){
return bbThread;
}
}
Here is the code for Activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class animadofondo2 extends Activity{
private mysurfaceview Clouds;
private mysurfaceview.mysurfaceviewThread CloudsThread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sufeaviewfondo);
Clouds= (mysurfaceview ) findViewById(R.id.ViewsLaynubes);
}
#Override
protected void onPause() {
super.onPause();
Clouds.getThread().pausar();
Toast.makeText(this, "Pausa", Toast.LENGTH_SHORT).show();
}
#Override
protected void onRestart(){
super.onRestart();
Clouds.getThread().reanudar();
Toast.makeText(this, "Restart", Toast.LENGTH_SHORT).show();
}
#Override protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
}
#Override protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#Override protected void onStop() {
super.onStop();
Nubecitas.getThread().pausar();
Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
}
#Override protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed(){
Lamar(null);
finish();
}
public void Lamar (View view){
Intent i = new Intent(this, main.class);
startActivity(i);
}
}
Here is the XML for the layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.example.proyectsurface.mysurfaceview
android:id="#+id/ViewsLaynubes"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

Android SeekBar extension

I have multiple Seek Bars on my activity (4) and for each there is a textView that suppose to show the progress of each SeekBar. I thought to extend the SeekBar Class and add to it's constructor a TextView Parameter so I can bind the two -> SeekBar and TextView . Here is my extended SeekBar class:
import android.content.Context;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarPlus extends SeekBar {
private TextView numberOfDrills;
public SeekBarPlus(Context context, TextView text) {
super(context);
// TODO Auto-generated constructor stub
numberOfDrills = (TextView) text;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
numberOfDrills.setText(progress);
}
}
What is the Context option?
Now here is my MainActivity:
package com.simplemathgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.simplemathgame.SeekBarPlus;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
SeekBarPlus addSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.add_seek_bar), numberOfAddDrills);
SeekBarPlus subSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.sub_seek_bar), numberOfAddDrills);
SeekBarPlus mulSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.mul_seek_bar), numberOfAddDrills);
SeekBarPlus divSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.div_seek_bar), numberOfAddDrills);
}
}
I don't know how to it wright! Any suggestions would be appreciated!!!
try to use setter instead of constructor in your custom SeekBar:
public void setTextView(TextView text) {
numberOfDrills = text;
}
then in your activity, use
SeekBarPlus subSeekBar = (SeekBarPlus) findViewById(R.id.sub_seek_bar);
subSeekBar.setTextView(numberOfAddDrills);
you can also use normal Seekbar with OnSeekBarChangeListener:
SeekBar subSeekBar = (SeekBar) findViewById(R.id.sub_seek_bar);
subSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) { }
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
numberOfDrills.setText(String.valueOf(progress));
}
});

Categories