android app crashes after several clicks on a button - android

could anyone help to solve the below problem:
I created a simple activity contains 2 buttons, every time i start clicking randomly on the buttons (even when slow clicking), the app crashes when the total clicks reach exactly 30 clicks.
Java:
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View button_1=(findViewById(R.id.button_1));
button_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AudioManager am = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_RING);
final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level * 14);
mToneGenerator.stopTone();
mToneGenerator.startTone(ToneGenerator.TONE_DTMF_1, 100);
}
});
final View button_2=(findViewById(R.id.button_2));
button_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AudioManager am = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_RING);
final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level * 14);
mToneGenerator.stopTone();
mToneGenerator.startTone(ToneGenerator.TONE_DTMF_9, 100);
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button_1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button_2"
android:layout_below="#+id/button_1"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Related

Toggle Button in Android

Can anyone help or share a link on how to implement this toggle button in android:
I've tried using this:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Active"
android:textOff="Completed"/>
but it's not having the desired output.
It has only one button and it changes when i click on it. What i want is two buttons side by side as shown in the picture above.
With some research, i found a better way with the new Material Design. We need to use the MaterialButtonToggleGroup which is provided in the Android material library:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:checkedButton="#id/active">
<com.google.android.material.button.MaterialButton
android:id="#+id/active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Active"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/completed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Completed"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Hope this helps other persons as well...
Custom Implementation ----
Colors
<color name = "background">#5cc0fa</color>
<color name = "white">#FFFFFF</color>
XML layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/background"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textColor="#color/white"
android:background="#color/background"
android:text="All" />
<Button
android:id="#+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textColor="#color/background"
android:background="#color/white"
android:text="Done" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Inside Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
int var = 0;
Button btnAll;
Button btnDone;
LinearLayout linearLayout;
public void toggle() {
if(var == 0) {
var = 1;
btnAll.setBackgroundColor(getResources().getColor(R.color.white));
btnAll.setTextColor(getResources().getColor(R.color.background));
btnDone.setBackgroundColor(getResources().getColor(R.color.background));
btnDone.setTextColor(getResources().getColor(R.color.white));
} else {
var = 0;
btnAll.setBackgroundColor(getResources().getColor(R.color.background));
btnAll.setTextColor(getResources().getColor(R.color.white));
btnDone.setBackgroundColor(getResources().getColor(R.color.white));
btnDone.setTextColor(getResources().getColor(R.color.background));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linear_layout);
btnAll = findViewById(R.id.btn_all);
btnDone = findViewById(R.id.btn_done);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
btnAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
}
}

Fragments and Layouts

When trying to run our app, we consistently have errors with conflicting fragments not letting us run the activity. Here's a screencast of the problem I am facing
As you can see, the app itself contains a multitude of problems, but my primary focus is getting the clicker to respond on the click. Here is the class page for the clicker activity:
package com.example.android.Chapsnat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ContactsFragment extends AppCompatActivity {
Button btnClick;
Button btnReset;
TextView txtCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_contacts);
btnClick = (Button)findViewById(R.id.buttonClick);
btnReset = (Button)findViewById(R.id.buttonReset);
txtCount = (TextView)findViewById(R.id.textViewCount);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String countValue = txtCount.getText().toString();
int intCountValue = Integer.parseInt(countValue);
intCountValue++;
txtCount.setText(String.valueOf(intCountValue));
} });
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtCount.setText(String.valueOf(0));
}
});
}
}
And this is the xml page it is being built in, which is also a fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.Chapsnat.ContactsFragment">
<TextView
android:id="#+id/textViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="0"
android:textColor="#android:color/holo_purple"
android:textSize="40sp" />
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonClick"
android:layout_below="#+id/textViewCount"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="171dp"
android:id="#+id/buttonReset"
android:layout_below="#+id/textViewCount"
android:layout_alignStart="#+id/buttonClick" />
</RelativeLayout>
Is there any way you can help? If so please let me know.

How to move current time position from 0.00 sec when played

Here is the java file:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AudioPlayer extends AppCompatActivity {
ImageView mIvPlayControl;
TextView mTvTotalDuration;
TextView mTvCurrentPosition;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player2);
mIvPlayControl= (ImageView) findViewById(R.id.iv_play);
mTvTotalDuration= (TextView) findViewById(R.id.tv_total);
mTvCurrentPosition= (TextView) findViewById(R.id.tv_progress);
mp = MediaPlayer.create(AudioPlayer.this, R.raw.sleep_away);
int mCurrentPosition=mp.getCurrentPosition();
int duration=mp.getDuration(); // miliseconds time
//convert into seconds
duration=duration/1000;
mCurrentPosition=mp.getCurrentPosition()/1000;
Log.e("value","value: "+duration);
mTvTotalDuration.setText(String.valueOf(duration));
mTvCurrentPosition.setText(String.valueOf(mCurrentPosition));
mIvPlayControl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.pause();
mIvPlayControl.setImageResource(R.mipmap.play_small);
} else {
mp.start();
mIvPlayControl.setImageResource(R.mipmap.pause_small);
}
}
});
And here is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center"
>
<TextView
android:id="#+id/tv_progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00"
/>
<ImageView
android:id="#+id/iv_play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/play_small"
/>
<TextView
android:id="#+id/tv_total"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="0.00"
/>
</LinearLayout>
</LinearLayout>
How do i move the beginning time 0 and onwards when played? Do i have to add seek bar to get moving time? Can somebody please help here? The song can be played but timer doesn't work. And more details or questions are in the comments.
Suppose mTvCurrentPlayTime is TesxtView representing current play-time on the screen then you can use following code to update the view:
Handler mHandler = new Handler();
//Make sure you update TextView on UI thread
AudioPlayer.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if(mp!= null){
int mCurrentPosition = mp.getCurrentPosition() / 1000;
mTvCurrentPosition.setText(String.valueOf(mCurrentPosition));
}
mHandler.postDelayed(this, 1000);
}
});

Add checkboxes on click

I have an image file on which if clicked anywhere I would like X'd checkboxes to show up. (Right now its set up as check marks). The code that I have is just for connecting page 1 to page 2 with a button click. Here it is
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnclickButtonListener();
}
public void OnclickButtonListener() {
button1 = (Button) findViewById(R.id.nextpage);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText userTextEntry = (EditText)findViewById(R.id.BRinfo);
String userdata = userTextEntry.getText().toString();
int userNumber = Integer.parseInt(userdata);
Intent intent = new Intent(MainActivity.this,Page2.class);
intent.putExtra("para name",userNumber);
startActivity(intent);
}
});
}}
Part of the XML code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="vt.tacticalcombat.MainActivity"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/combat2"
android:layout_alignBottom="#+id/editText17"
android:layout_alignLeft="#+id/last4"
android:layout_alignStart="#+id/last4"
android:layout_alignRight="#+id/tableLayout"
android:layout_alignEnd="#+id/tableLayout"
android:layout_alignTop="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="#+id/nextpage"
android:layout_alignBottom="#+id/tableLayout"
android:layout_alignRight="#+id/otherinfo"
android:layout_alignEnd="#+id/otherinfo"
tools:ignore="HardcodedText,RtlHardcoded"
android:nestedScrollingEnabled="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox28"
android:checked="false"
android:visibility="visible"
android:layout_alignBottom="#+id/rarmtypeinfo"
android:layout_toRightOf="#+id/mvc"
android:clickable="true"
android:layout_toEndOf="#+id/mvc" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_alignBottom="#+id/checkBox28"
android:layout_alignRight="#+id/injury"
android:layout_alignEnd="#+id/injury"
android:layout_alignTop="#+id/larmtype"
android:layout_alignLeft="#+id/checkBox28"
android:layout_alignStart="#+id/checkBox28" />
</RelativeLayout>
I tried using this link! but it did not work. How can I do this? Any help is appreciated

Change a ImageView by a time interval when a button is clicked

I'm trying to make a workout like App. I have a full screen ImageView and a ImageButton with a play icon. I want that when I click on the ImageButton the icon changes to a stop icon immediately, and the ImageView shows the image of the first exercise that will change after lets say 20secs to the image of the second exercise, but if a want to pause the counter I click again on the ImageButton to stop it.
My layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF5722"
android:orientation="vertical"
android:id="#+id/linearLayout">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Inicio"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="8dp"
android:layout_marginLeft="16dp"
android:id="#+id/Boton1"
android:onClick="sendMessage"
android:clickable="true"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:layout_below="#+id/linearLayout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imagenRutina"
android:layout_marginBottom="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff5722"
android:orientation="horizontal"
android:id="#+id/linearLayout3">
</LinearLayout>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/linearLayout3"
android:layout_centerHorizontal="true"
android:background="#ff5722"
android:clickable="true"
android:layout_marginTop="5dp"
/>
</RelativeLayout>
</RelativeLayout>
And this is my java file
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Rutina1Reproduccion extends AppCompatActivity {
ImageButton mPlayButton;
boolean isPlay = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rutina1_reproduccion);
mPlayButton = (ImageButton) findViewById(R.id.imageButton);
background="#drawable/default"
mPlayButton.setBackgroundResource(R.drawable.playicon);
mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
final int[] imageArray = {R.drawable.imagengemelos, R.drawable.imagengluteos, R.drawable.imagenbrazos};
final ImageView imageView = (ImageView) findViewById(R.id.imagenRutina);
final Handler handler = new Handler();
#Override
public void onClick(View v) {
if (isPlay) {
v.setBackgroundResource(R.drawable.playicon);
Runnable runnable = new Runnable() {
int i = 0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if (i > imageArray.length - 1) {
i = 0;
}
handler.postDelayed(this, 3000); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
}else{
v.setBackgroundResource(R.drawable.stopicon);
}
isPlay = !isPlay; // reverse
}
};
public void sendMessage(View view){
Intent intent=new Intent(Rutina1Reproduccion.this,Rutina1.class);
startActivity(intent);
}}
The error I'm getting its fatal exception whenIi open the activity.
If I also want to play some sound when the ImageButton is clicked, how can I do it?

Categories

Resources