I have created two classes, actually both of them extends Activity. What I am trying to do is to call a method from the second class.
What I am trying to do is calling the method from second class then implemented in first class, unfortunately I did not have success in that.
I need your help to solve this problem. Thank you
My first class:
package com.math4kids;
import android.app.Activity;
import android.os.Bundle;
public class testing002 extends Activity {
private Sounds myotherclass;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numeracy);
myotherclass.Randomsoundforrightanswer();
}
}
The second class:
package com.math4kids;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
public class Sounds extends Activity {
MediaPlayer cool, good, perfect, sweet, excellent, goodthinking, greatjob,
notbad, thatstheway, youdidit, yes, again, wrong, sorry,
sundfornum01, sundfornum02;
public Random random = new Random();
public Sounds(Context context){
super.getApplicationContext();
}
public void Randomsoundforrightanswer() {
cool = MediaPlayer.create(this, R.raw.cool);
good = MediaPlayer.create(this, R.raw.good);
perfect = MediaPlayer.create(this, R.raw.perfect);
sweet = MediaPlayer.create(this, R.raw.sweet);
excellent = MediaPlayer.create(this, R.raw.excellent);
goodthinking = MediaPlayer.create(this, R.raw.goodthinking);
greatjob = MediaPlayer.create(this, R.raw.greatjob);
notbad = MediaPlayer.create(this, R.raw.notbad);
thatstheway = MediaPlayer.create(this, R.raw.thatstheway);
youdidit = MediaPlayer.create(this, R.raw.youdidit);
yes = MediaPlayer.create(this, R.raw.yes);
switch (random.nextInt(11)) {
case 0:
cool.start();
break;
case 1:
good.start();
break;
case 2:
perfect.start();
break;
case 3:
sweet.start();
break;
case 4:
excellent.start();
break;
case 5:
goodthinking.start();
break;
case 6:
greatjob.start();
break;
case 7:
notbad.start();
break;
case 8:
thatstheway.start();
break;
case 9:
youdidit.start();
break;
case 10:
yes.start();
break;
}
}
}
Make a simple normal java file then define these methods in that class.
import java.util.Random;
import android.media.MediaPlayer;
public class Sounds {
Context context;
MediaPlayer cool, good, perfect, sweet, excellent, goodthinking, greatjob,
notbad, thatstheway, youdidit, yes, again, wrong, sorry,
sundfornum01, sundfornum02;
public Random random = new Random();
public Sounds(Context context){
this.context = context;
}
public void Randomsoundforrightanswer() {
cool = MediaPlayer.create(context, R.raw.cool);
good = MediaPlayer.create(context, R.raw.good);
perfect = MediaPlayer.create(context, R.raw.perfect);
sweet = MediaPlayer.create(context, R.raw.sweet);
excellent = MediaPlayer.create(context, R.raw.excellent);
goodthinking = MediaPlayer.create(context, R.raw.goodthinking);
greatjob = MediaPlayer.create(context, R.raw.greatjob);
notbad = MediaPlayer.create(context, R.raw.notbad);
thatstheway = MediaPlayer.create(context, R.raw.thatstheway);
youdidit = MediaPlayer.create(context, R.raw.youdidit);
yes = MediaPlayer.create(context, R.raw.yes);
switch (random.nextInt(11)) {
case 0:
cool.start();
break;
case 1:
good.start();
break;
case 2:
perfect.start();
break;
case 3:
sweet.start();
break;
case 4:
excellent.start();
break;
case 5:
goodthinking.start();
break;
case 6:
greatjob.start();
break;
case 7:
notbad.start();
break;
case 8:
thatstheway.start();
break;
case 9:
youdidit.start();
break;
case 10:
yes.start();
break;
}
}
}
Call methods of regular java file in activity like this.
import android.app.Activity;
import android.os.Bundle;
public class testing002 extends Activity {
private Sounds myotherclass;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numeracy);
new Sounds().Randomsoundforrightanswer(this);
}
}
Why should you do it?
Why Sounds class extending Activity?
Please read once again the official documentation Activity.
If you did it cause you need a context just pass it like a parameter to the Sounds class.
And you also need to visit Android development guide too
Only one Activity is instantiated at a time. You should not try to call one Activity from another.
Instead, you should create a third class which contains the method you want to call.
public class SoundManager{
private context;
public SoundManager(Context context){
context.context = context;
}
public void Randomsoundforrightanswer() {
cool = MediaPlayer.create(context, R.raw.cool);
good = MediaPlayer.create(context, R.raw.good);
perfect = MediaPlayer.create(context, R.raw.perfect);
sweet = MediaPlayer.create(context, R.raw.sweet);
excellent = MediaPlayer.create(context, R.raw.excellent);
goodthinking = MediaPlayer.create(context, R.raw.goodthinking);
greatjob = MediaPlayer.create(context, R.raw.greatjob);
notbad = MediaPlayer.create(context, R.raw.notbad);
thatstheway = MediaPlayer.create(context, R.raw.thatstheway);
youdidit = MediaPlayer.create(context, R.raw.youdidit);
yes = MediaPlayer.create(context, R.raw.yes);
switch (random.nextInt(11)) {
case 0:
cool.start();
break;
case 1:
good.start();
break;
case 2:
perfect.start();
break;
case 3:
sweet.start();
break;
case 4:
excellent.start();
break;
case 5:
goodthinking.start();
break;
case 6:
greatjob.start();
break;
case 7:
notbad.start();
break;
case 8:
thatstheway.start();
break;
case 9:
youdidit.start();
break;
case 10:
yes.start();
break;
}
}
}
However, you are going to have to do more work with the MediaPlayer. You should read the documentation for it before proceeding. The code I've shown gives you the basics of what you need to do but it will not work.
Finally, best advice I can give you is to learn the basics of Java and OOP before you proceed.
Unless testing002 class is actually an Activity you want to use as an Activity, you should move the randomsound... function to a seperate class.
Like the sounds class but not an Activity. If you define the function in that class you can construct in another and call it.
Related
So I have multiple Views(a combination of multiple text boxes and images) and I have attached an onClickListener to the views and then attached an onClick method to them and after that I have used a switch statement within an onClick for the same.
ConstraintLayout[] ll_but = new ConstraintLayout[10];
ll_but[0] = findViewById(R.id.success_stories_tile);
ll_but[1] = findViewById(R.id.misc_but3_layout);
ll_but[2] = findViewById(R.id.misc_but2_layout);
ll_but[3] = findViewById(R.id.faqs_tile);
ll_but[4] = findViewById(R.id.mohfw_ll2);
ll_but[5] = findViewById(R.id.mohfw_ll3);
ll_but[6] = findViewById(R.id.mohfw_ll4);
ll_but[7] = findViewById(R.id.mohfw_ll5);
int[] btnToAdd = new int[]{0, 1, 2, 3, 4, 5, 6 , 7};
for (int i = 0; i < btnToAdd.length; i++) {
ll_but[btnToAdd[i]].setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i = null;
switch (view.getId()){
case R.id.flipperLeft:
viewFlipper.stopFlipping();
viewFlipper.setInAnimation(anim1);
viewFlipper.setOutAnimation(anim4);
viewFlipper.showNext();
break;
case R.id.flipperRight:
viewFlipper.stopFlipping();
viewFlipper.setInAnimation(anim2);
viewFlipper.setOutAnimation(anim3);
viewFlipper.showPrevious();
break;
case R.id.misc_but2_layout:
i = new Intent(homeActivity.this, ChatActivity.class);
startActivity(i);
break;
case R.id.misc_but3_layout:
i = getMythIntent(this);
startActivity(i);
break;
case R.id.success_stories_tile:
i = getSuccessStoriesIntent(this);
startActivity(i);
break;
case R.id.faqs_tile:
i = getFAQsIntent(this);
startActivity(i);
break;
case R.id.mohfw_ll2:
case R.id.mohfw_ll3:
case R.id.mohfw_ll4:
case R.id.mohfw_ll5:
i = getTwitterIntent ( this );
startActivity(i);
break;
default:
i = null;
break;
}
}
This is working for me but I would really like to know if there is a better implementation for the same.
Android Studio now has something like Butterknife implemented in their IDE now, therefore, you can just use <YourLayoutName>Binding binding = <YourLayoutName>Binding.inflate(); in your onCreateView()
As example:
LoginFragmentBinding binding = LoginFragmentBinding.inflate(inflater, container, false);
With that, you can use for example binding.miscBut3Layout.setOnClickListener(this); instead of the getViewById that can return an exception.
Another approach for your Listeners is to put them in the XML with the view with the attribute android:onclick="" with this, you don't need to create an array for the views.
I'm going to create the app which will take random 3 numbers from array String [] powerBalls with 5 numbers and will switch the image with picture to every number and then show the number and picture(e.g. string "1" from array powerballs will have the image with the name "o1"). The problem is that on the display I get the numbers and can see the picture as long as switch case will find the correct image. Then the app shutting down. The images are all the time visible and they should appear only with the random number. What is wrong with this code?
I have really no idea what I'm doing wrong and could not find some example for this program.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zufallsgenerator);
bindViews();
mGenerateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
generateRandomNumbers();
}
});
}
public void generateRandomNumbers(){
Collections.shuffle(Arrays.asList(powerBalls));
mFirstNumber.setText(powerBalls[0]);
mSecondNumber.setText(powerBalls[1]);
mThirdNumber.setText(powerBalls[2]);
mFourthNumber.setText(powerBalls[3]);
mFifthNumber.setText(powerBalls[4]);
mSixthNumber.setText(powerBalls[5]);
switch (powerBalls[0]) {
case "1":
imageViewDice1.setImageResource(R.drawable.o1);
imageViewDice1 = (ImageView)findViewById(R.id.image_view_dice1);
break;
case "2":
imageViewDice2.setImageResource(R.drawable.o2);
imageViewDice2 = (ImageView)findViewById(R.id.image_view_dice2);
break;
case "3":
imageViewDice3.setImageResource(R.drawable.o3);
imageViewDice3 = (ImageView)findViewById(R.id.image_view_dice3);
break;
case "4":
imageViewDice4.setImageResource(R.drawable.o4);
imageViewDice4 = (ImageView)findViewById(R.id.image_view_dice4);
break;
case "5":
imageViewDice5.setImageResource(R.drawable.o5);
imageViewDice5 = (ImageView)findViewById(R.id.image_view_dice5);
break;
case "6":
imageViewDice6.setImageResource(R.drawable.o6);
imageViewDice6 = (ImageView)findViewById(R.id.image_view_dice6);
break;
}
switch (powerBalls[1]) {
case "1":
imageViewDice1.setImageResource(R.drawable.o1);
imageViewDice1 = (ImageView)findViewById(R.id.image_view_dice1);
break;
case "2":
imageViewDice2.setImageResource(R.drawable.o2);
imageViewDice2 = (ImageView)findViewById(R.id.image_view_dice2);
break;
case "3":
imageViewDice3.setImageResource(R.drawable.o3);
imageViewDice3 = (ImageView)findViewById(R.id.image_view_dice3);
break;
case "4": // and so on...*****
and on the end of the java class:
public void bindViews(){
mFirstNumber = (TextView)findViewById(R.id.firstNumber);
mSecondNumber = (TextView)findViewById(R.id.secondNumber);
mThirdNumber = (TextView)findViewById(R.id.thirdNumber);
mFourthNumber = (TextView)findViewById(R.id.fourthNumber);
mFifthNumber = (TextView)findViewById(R.id.fifthNumber);
mSixthNumber = (TextView)findViewById(R.id.sixthNumber);
mPowerBall = (TextView)findViewById(R.id.powerBall);
mGenerateButton = (Button)findViewById(R.id.mgenerateButton);
The example of the xml file with views:
<TextView
android:id="#+id/firstNumber"
android:layout_weight="1"
android:fontFamily="serif"
android:textSize="24sp"
android:padding="4dp"
android:layout_margin="8dp"
android:text="01"
android:textColor="#color/startblue"
android:layout_width="0dp"
android:layout_height="wrap_content" />
Thank you in advance for your suggestions and answers. Cheers
I'm making a Quizz App for Android with 10 Questions, all of them with 4 Radio Buttons, and one button at the end to show the score. The problem is when I choose the correct answer it gives 5 points, but if I check another radio button the points will stay 5 and if I press again it sums 5. What is the best way to code this?
Here is the code:
package com.example.android.quizproject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void firstRadioButtons (View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.questionOneA:
if (checked)
points += 0;
break;
case R.id.questionOneB:
if (checked)
points += 0;
break;
case R.id.questionOneC:
if (checked)
points += 5;
break;
case R.id.questionOneD:
if (checked)
points += 0;
break;
}
}
public void showScore (View view) {
TextView scoreTextView = (TextView) findViewById(R.id.score);
scoreTextView.setText(" " + points);
}
}
You can make use of a counter vvariable which checks if the question has been previousy answered or not. Modify part of your code to this
public void firstRadioButtons (View view){
boolean checked = ((RadioButton) view).isChecked();
int count=0;
switch (view.getId()) {
case R.id.questionOneA:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
case R.id.questionOneB:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
case R.id.questionOneC:
if (checked){
points += 5;
count+=1;}
break;
case R.id.questionOneD:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
}
}
Actually, the way you described it, it's common sense. If you click the right answer once, it will set it to 5, but if you press any other it will add 0 to it.
In general, it will print out 5 since you got the answer correct once, and the other questions are set to 0. There's really nothing to fix here, it's kind of common sense that your variable wouldn't read other than 5. Just like Abhriya said, you could add a counter increment value as done in ( her / his ) example.
I have some code that starts text to speech and begins playing a sound when a button is pressed. Everything is working fine except for one detail. When two buttons are pressed consecutively, the sounds begin to overlap. Is there a way to cut off the mediaplayer every time a button is pressed? I have used noise1.pause(), noise1.stop(), and noise1.reset() but nothing works. I was thinking about using mediaplayer complete listener but I'm not sure how I could implement this. Any help would be appreciated. Thanks a lot!!!
public void onClick(View view) {
Resources res = getResources();
final TextView tv = (TextView) findViewById(R.id.color_text);
final MediaPlayer noise1 = MediaPlayer.create(this, R.raw.one);
final MediaPlayer noise2 = MediaPlayer.create(this, R.raw.two);
final MediaPlayer noise3 = MediaPlayer.create(this, R.raw.three);
switch (view.getId()) {
case R.id.one_button:
String oneString = res.getString(R.string.One);
tv.setText(oneString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null, null);
}
noise1.start();
break;
case R.id.two_button:
String twoString = res.getString(R.string.Two);
tv.setText(twoString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null, null);
}
noise2.start();
break;
case R.id.three_button:
String threeString = res.getString(R.string.Three);
tv.setText(threeString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null, null);
}
three.start();
break;
}
Define and initialize the MediaPlayer instances outside the onClick(...) method and the stop() method should work.
Here is what I mean:
MediaPlayer noise1, noise2, noise3;
TextView tv;
public void onCreate(....){
.....
tv = (TextView) findViewById(R.id.color_text);
noise1 = MediaPlayer.create(this, R.raw.one);
noise2 = MediaPlayer.create(this, R.raw.two);
noise3 = MediaPlayer.create(this, R.raw.three);
....
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.one_button:
...
noise1.start();
noise2.stop();
noise3.stop();
break;
case R.id.two_button:
...
noise1.stop();
noise2.start();
noise3.stop();
break;
case R.id.three_button:
...
noise1.stop();
noise2.stop();
noise3.start();
break;
}
It doesn't work now because you're trying to stop a MediaPlayer instance that you've just created not the one that was created and started on the previous button press.
I don't have reputation for commenting, so have this instead.
The created MediaPlayer objects are within the scope of the method, which means as soon as onClick returns, the objects have no reference, and hence, cannot be stopped.
The solution is to move the objects higher in the scope, either having references in the encompassing object or storing the MediaPlayers in a collection. Then, at the start of the onClick method, stop the old ones before creating new ones and overriding the reference.
Unless there is some caveat with Android, this should work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hello please help me with call
package com.paradox02.dell.dormation01;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class GifView2 extends View{
public void Load2() {
int riad = 0;
switch (riad) {
case 0:
Obr0();
riad++;
break;
case 1:
Obr1();
riad++;
break;
case 2:
Obr2();
riad++;
break;
case 3:
Obr3();
riad++;
break;
case 4:
Obr4();
riad++;
break;
case 5:
Obr5();
riad++;
break;
case 6:
Obr6();
riad++;
break;
case 7:
Obr7();
riad++;
break;
case 8:
Obr8();
riad++;
break;
case 9:
Obr9();
riad++;
break;
case 10:
Obr10();
riad++;
break;
case 11:
Obr11();
riad++;
break;
case 12:
Obr12();
riad++;
break;
case 13:
Obr13();
riad++;
break;
case 14:
Obr14();
riad++;
break;
case 15:
Obr15();
riad++;
break;
case 16:
Obr16();
riad++;
break;
}
}
}
I need launch procedure Load2 in another activity (On start in index activity)
but I don't know how.
or, please give mi some links where I can read about this problem.
Or give mi advice for another solution my problem.
Please help me.
Thanks.
Add the following constructors to your GifView2 class:
public GifView2 (Context context) {
super(context);
}
public GifView2(Context context, AttributeSet attrs) {
super(context, attrs);
}
Then, in your index Acitivty class (assuming your class does extend Activity, or AppCompatActivity, etc.:
new GifView2(this).Load2();