I want to make the "a" variable to take the value of the seekbar and use it for the "sleep" param. Now, 'a' is 100. Can this be done with int? What I want: a = the value of the SeekBar as int value instead of 100. I have tried different methods but didn't have any luck. I have also attached the xml code.
public class Blink1 extends AppCompatActivity {
Handler handler ;
ConstraintLayout blink1 ;
int i = 0;
int colors[] = {Color.BLACK, Color.WHITE};
Runnable runnable = new Runnable() {
#Override
public void run () {
blink1.setBackgroundColor(colors[i]);
if(i==1){
i=0;
}
else i++;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
setContentView(R.layout.activity_blink1);
handler = new Handler();
blink1 = (ConstraintLayout) findViewById(R.id.blink2);
final int a;
a=100;
Thread myTread = new Thread() {
public void run() {
try {
while (true) {
sleep(a);
handler.post(runnable);
}
} catch (InterruptedException e) {
}
}
};
myTread.start();
}
}
the xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/blink2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.studios.minimal.blink.Blink1">
<SeekBar
android:id="#+id/seekBar7"
android:max="1000"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.903" />
</android.support.constraint.ConstraintLayout>
Try this
SeekBar seekBar=findViewById(R.id.seekBar7);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
a=seekBar.getProgress();
}
});
Related
I want to make a music player in my fragment page for my android app using android studio. There is no error to the code however when i run the code the fragment page is not appearing the audio player. i follow the code from youtube: https://www.youtube.com/watch?v=rJ3XbXtjNaE&t=385s
this is the code
fragment_med1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:background="#drawable/scene"
tools:context=".Med1Fragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_baseline_music_note_24"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/player_position"
android:text="00:00"
android:textStyle="bold"
android:textColor="#android:color/white"/>
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/seek_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/player_duration"
android:text="00:00"
android:textStyle="bold"
android:textColor="#android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_rew"
android:src="#drawable/ic_baseline_fast_rewind_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_play"
android:src="#drawable/ic_baseline_play_circle_filled_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_pause"
android:src="#drawable/ic_baseline_pause_circle_filled_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_ff"
android:src="#drawable/ic_baseline_fast_forward_24"/>
</LinearLayout>
</LinearLayout>
Med1Fragment.java
public class Med1Fragment extends Fragment {
TextView playerposition, playerduration;
SeekBar seekBar;
ImageView btnrew, btnplay, btnpause, btnff;
MediaPlayer mediaPlayer;
Handler handler = new Handler();
Runnable runnable;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerposition = playerposition.findViewById(R.id.player_position);
playerduration = playerduration.findViewById(R.id.player_duration);
seekBar = seekBar.findViewById(R.id.seek_bar);
btnrew = btnrew.findViewById(R.id.bt_rew);
btnplay = btnplay.findViewById(R.id.bt_play);
btnpause = btnpause.findViewById(R.id.bt_pause);
btnff = btnff.findViewById(R.id.bt_ff);
mediaPlayer = MediaPlayer.create(getActivity(), R.raw.meditation2);
runnable = new Runnable() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
handler.postDelayed(this, 500);
}
};
int duration = mediaPlayer.getDuration();
String sDuration = convertFormat(duration);
playerduration.setText(sDuration);
btnplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnplay.setVisibility(View.GONE);
btnpause.setVisibility(View.VISIBLE);
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
handler.postDelayed(runnable,0);
}
});
btnpause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnpause.setVisibility(View.GONE);
btnplay.setVisibility(View.VISIBLE);
mediaPlayer.pause();
handler.removeCallbacks(runnable);
}
});
btnff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentposition = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if(mediaPlayer.isPlaying() && duration != currentposition){
currentposition = currentposition + 5000;
playerposition.setText(convertFormat(currentposition));
mediaPlayer.seekTo(currentposition);
}
}
});
btnrew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentposition = mediaPlayer.getCurrentPosition();
if(mediaPlayer.isPlaying() && currentposition > 5000){
currentposition = currentposition - 5000;
playerposition.setText(convertFormat(currentposition));
mediaPlayer.seekTo(currentposition);
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mediaPlayer.seekTo(progress);
}
playerposition.setText(convertFormat(mediaPlayer.getCurrentPosition()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
btnpause.setVisibility(View.GONE);
btnplay.setVisibility(View.VISIBLE);
mediaPlayer.seekTo(0);
}
});
}
#SuppressLint("DefaultLocale")
private String convertFormat(int duration){
return String.format("%02d:%02d"
, TimeUnit.MILLISECONDS.toMinutes(duration)
, TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_med1, container, false);
}
}
I am always confused to write the code for fragment because the usual code i get/learn from (eg:youtube) are written for activity instead of fragment.
really appreciate some help. thank you.
I am building a simple video player Ui and I have a : seekbar and two buttons to controle the video view, when I click on any point of seekbar every thing is ok and video view progress will be the same with seekbar and the buttons is working too ! but,when I handle the seekbar thumb and move it ,then leave it, every thing messing up and the seekbar and the buttons will never work again, so I am not getting an Exception error it's just will not work again. why that happening?
Activity
public class Player extends AppCompatActivity {
View controller;
Handler handler;
SeekBar seekBar;
VideoView videoView;
ImageButton play_btn, pause_btn;
ImageButton forward_btn, replay_btn;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
handler = new Handler();
videoView = (VideoView) findViewById(R.id.videoView);
controller = findViewById(R.id.player_controller); // the controller View
hideController(controller);
forward_btn = (ImageButton) findViewById(R.id.playerController_forward_10_Button);
replay_btn = (ImageButton) findViewById(R.id.playerController_replay_10_Button);
forward_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
forward_10_sec();
}
});
replay_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reply_10_sec();
}
});
play_btn = (ImageButton) findViewById(R.id.playerController_playButton);
pause_btn = (ImageButton) findViewById(R.id.playerController_pauseButton);
play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play();
}
});
pause_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pause();
}
});
seekBar = findViewById(R.id.playerController_seekBar); // video seekBar
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
videoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (controller.getVisibility() == View.INVISIBLE)
showController(controller);
else
hideController(controller);
}
});
Intent intent = getIntent();
if (intent != null)
setVideoSource(intent);
}
private void play() {
if(!videoView.isPlaying()) {
videoView.start();
play_btn.setVisibility(View.INVISIBLE);
pause_btn.setVisibility(View.VISIBLE);
}
}
private void pause() {
if(videoView.isPlaying()) {
videoView.pause();
pause_btn.setVisibility(View.INVISIBLE);
play_btn.setVisibility(View.VISIBLE);
}
}
private void forward_10_sec() {
if (videoView.canSeekForward())
videoView.seekTo(videoView.getCurrentPosition() + 1000);
}
private void reply_10_sec() {
if (videoView.canSeekBackward())
videoView.seekTo(videoView.getCurrentPosition() - 1000);
}
// to show the controllers
private void showController(View controller) {
controller.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
hideController(controller);
}
}, 6000);
}
// to hide the controllers
private void hideController(View controller) {
controller.setVisibility(View.INVISIBLE);
}
// to set video source from the main app or file manager
private void setVideoSource(Intent intent) {
... some codes to set path ...
play();
showController(controller);
updateSeekBarProgress();
}
// to move seekBar to real video progress
private void updateSeekBarProgress() {
Player.this.runOnUiThread(updateSeekBar);
}
private Runnable updateSeekBar = new Runnable() {
#Override
public void run() {
seekBar.setMax(videoView.getDuration() / 1000);
seekBar.setProgress(videoView.getCurrentPosition() / 1000);
seekBar.postDelayed(this,500);
}
};
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i*1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
}
activity_player
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".Player">
<VideoView
android:id="#+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="#+id/player_controller"
layout="#layout/player_controller" />
</androidx.constraintlayout.widget.ConstraintLayout>
player_controller
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_black">
<ImageButton
android:id="#+id/playerController_playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/play_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/pause_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_replay_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/replay_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/playerController_playButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_forward_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/forward_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/playerController_playButton"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="#+id/playerController_seekBar"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:progressBackgroundTint="#color/white"
android:progressTint="#color/white"
android:secondaryProgressTint="#color/light_purple"
android:thumbTint="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/playerController_playButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
so, in a simple way : I want to set video view progress like seekbar progress with any way that I move the seek bar with.
(now it's just works when click on it and when handle and move it , nothing works again).
thanks...
It's finally has been done with that :
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i * 1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
handler.removeCallbacks(updateSeekBar);
pause();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
videoView.seekTo(seekBar.getProgress()*1000);
play();
updateSeekBarProgress();
}
};
I have a workking PDF downloader and PDFRenderer. I am using openFileInput method to keep the file secure from scanners. My problem is the Bitmap generated is so small it cannot be read. I tried to use like every example recommended but I cannot get it to load the APK too many logcats to list. There are none with this version. Device is Galaxy S6 I only have AMD computers so no emulators here and NO SDCard recommendations for security reasons I am using APP Specific storage
Screenshot of PDFRenderer Bitmap:
XML code:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.madjobber.pdfdemo.PDFRender">
<ImageView
android:id="#+id/image"
android:layout_width="371dp"
android:layout_height="437dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/previous"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="previous"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/next"
android:layout_marginRight="8dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/next"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="next"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/image"
app:layout_constraintVertical_bias="0.0" />
Downloader Java:
public class MainActivity extends AppCompatActivity {
Button btn;
String fileDownloadPath = "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf";
String saveFilePath = "";
ProgressDialog dialog = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this,"","File is Being Downloaded",true);
new Thread(new Runnable() {
#Override
public void run() {
downloadFile(fileDownloadPath,saveFilePath);
}
}).start();
}
});
}
public void downloadFile(String fileDownloadPath,String saveFilePath)
{
try {
URL u = new URL(fileDownloadPath);
URLConnection con = u.openConnection();
int lengthofContent=con.getContentLength();
DataInputStream DIStream=new DataInputStream(u.openStream());
byte[] buffer=new byte[lengthofContent];
DIStream.readFully(buffer);
DIStream.close();
DataOutputStream DOStream=new DataOutputStream(new FileOutputStream(getApplicationContext().getFilesDir() + "/" + "PDF.pdf"));
DOStream.write(buffer);
DOStream.flush();
DOStream.close();
hideProgressIndicator();
}
catch (FileNotFoundException e){
hideProgressIndicator();
}
catch (IOException e) {
}
catch (Exception e){
}
}
private void hideProgressIndicator() {
runOnUiThread(new Runnable() {
#Override
public void run() {
dialog.dismiss();
}
});
}
public void PDFRenderer (View view) {
Intent intent = new Intent(MainActivity.this, PDFRender.class);
startActivity(intent);
}
}
PDFRenderer JAVA:
public class PDFRender extends AppCompatActivity {
private ImageView imageView;
private int currentPage = 0;
private Button next,previous;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfrender);
next =(Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentPage++;
render();
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentPage--;
render();
}
});
render();
}
private void render() {
//open PDF from internal storage and pass the variable to renderer
String Path = getApplicationContext().getFilesDir().getPath();
File file = new File( Path +"/PDF.pdf");
try{
imageView = (ImageView) findViewById(R.id.image);
int REQ_WIDTH = imageView.getWidth();
int REQ_HEIGHT = imageView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
if (currentPage < 0){
currentPage = 0;
}else if (currentPage > renderer.getPageCount()) {
currentPage = renderer.getPageCount() - 1;
}
Matrix m = imageView.getImageMatrix();
Rect rect =new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);
renderer.openPage(currentPage).render(bitmap, rect, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imageView.setImageMatrix(m);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}catch (Exception e) {
e.printStackTrace();
}
}
Any Help will be greatly appreciated as even with cheater glasses I cannot read the PDF. Thank you in advance!!
It is my first time working with Android Studio and my Java skills are basically non-existent so I need a little help with a project I am working on.
I need to make a timer that repeats after a certain break. The user should decide how many times should timer repeat and what is the duration of break.
So far I did the standard countdown timer but no matter what i try i can't make it repeat
That is the working code I have so far
Interface:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asus.timer.MainActivity"
android:padding="20dp"
android:orientation="vertical"
>
<EditText
android:id="#+id/editTextSession"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:padding="20dp"
android:hint="Session (seconds)"
android:textAlignment="center"
/>
<EditText
android:id="#+id/editTextBreak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:padding="20dp"
android:hint="Break (seconds)"
android:textAlignment="center"
/>
<EditText
android:id="#+id/editTextRepeats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:padding="20dp"
android:hint="Repeats"
android:textAlignment="center"
/>
<Button
android:id="#+id/buttonStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start!"
android:padding="20dp"
/>
<TextView
android:id="#+id/textViewTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:padding="20dp"
android:textAlignment="center"
android:textSize="60dp"
android:paddingTop="40dp"
android:gravity="center"
/>
Functionality
public class MainActivity extends AppCompatActivity {
EditText editTextSession;
EditText editTextBreak;
EditText editTextRepeats;
Button buttonStart;
TextView textViewTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextSession =(EditText) findViewById(R.id.editTextSession);
editTextBreak =(EditText) findViewById(R.id.editTextBreak);
editTextRepeats =(EditText) findViewById(R.id.editTextRepeats);
buttonStart =(Button) findViewById(R.id.buttonStart);
textViewTimer =(TextView) findViewById(R.id.textViewTimer);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editTextSession.getText().toString();
int seconds = Integer.valueOf(editTextSession.getText().toString());
CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000) {
#Override
public void onTick(long millis) {
textViewTimer.setText("time:" + (int) (millis / 1000));
}
#Override
public void onFinish() {
extViewTimer.setText("Finished!");
}
}.start();
}
}
);
Did You try Integer.parseInt(String value) instead of Integer.valueOf(String value)? Maybe it will make a diffrence. You can add an exception if someone type wrong value.
if you're looking for another way to create timer, you can do something like this with Runnable() :
private TextView timeCounterTextView;
private int smsTimeOut = 10; // 10 SECONDS
private Runnable timeCounterRunnable = new Runnable() {
#Override
public void run() {
if (smsTimeOut == 1) {
//Do whatever when timeout
}
smsTimeOut--;
timeCounterTextView.setText(String.valueOf(smsTimeOut));
handler.postDelayed(this, 1000);
}
};
and whenever you want to start countdown initialize and start timer with init() method like below :
private void init(){
handler.removeCallbacks(timeCounterRunnable);
smsTimeOut = 10;
handler = new Handler();
handler.post(timeCounterRunnable);
}
anytime you want to repeat you can call init() again.
hope this helps.
Use this
timer.start();
And stop it with:
timer.cancel();
Example:
String text = editTextSession.getText().toString();
int seconds = Integer.valueOf(text);
//get how many repeat
String repeatText = editTextSession.getText().toString();
int repeat = Integer.valueOf(repeatText);
int count= 0;
CountDownTimer countDownTimer = new CountDownTimer(seconds * 1000, 1000) {
#Override
public void onTick(long millis) {
textViewTimer.setText("time:" + (int)(millis / 1000));
}
#Override
public void onFinish() {
extViewTimer.setText("Finished!");
count++;
//check if count still less than repeat number
if(count < repeat){
Runnable r = new Runnable() {
public void run() {
countDownTimer.start();
}};
new Handler().postDelayed(r,2000); //delay repeat timer 2 seconds
}
else{
countDownTimer.cancel();
count = 0;
}
}
}.start();
I have made a progress bar in Android but it is not at all working. What am I doing wrong? I have just started to learn android. Below is my code.
MainActivity.java-
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int mporgress=0;
EditText time;
private Handler mHandler = new Handler();
private static final int PROGRESS = 0x1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Startbuttononclick(View view){
Button startbutton=(Button) findViewById(R.id.button);
startbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mporgress = Integer.parseInt(time.getText().toString());
progressBar.setProgress(mporgress);
new Thread(new Runnable() {
public void run() {
while (mporgress < 100) {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(mporgress);
mporgress = doWork();
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
}
});
}
}
}).start();
}
});
}
public void doprogress(View view) {
}
public int doWork(){
mporgress++;
return mporgress;
}
}
Activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.progressbar.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the time:"
android:inputType="number"
android:id="#+id/editText" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="500dp"
android:layout_height="200dp"
android:id="#+id/progressBar4"
android:layout_gravity="center"
android:scrollbarSize="500dp" />
<Button
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_gravity="center"
android:onClick="Startbuttononclick" />
</LinearLayout>
Check out this image below-
- https://i.stack.imgur.com/2TBIb.png
Note- Enter the time in this image is an EditText with a hint and not a TextView.
It is appearing but not working.
I have done these changes so far.-
https://i.stack.imgur.com/UujpB.png
But still the progress bar is still continuously rotating.
Seems you forgot to initialize EditText -time and ProgressBar. Init it inside onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar =(ProgressBar) findViewById(R.id.progressBar4);
time = (EditText) findViewById(R.id.editText); //here
}
Also inside button click the bar:
progressBar = new ProgressBar(this);
mporgress = Integer.parseInt(time.getText().toString());
................
.............