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 was wondering if someone knows or has a way to dynamically load all fonts .ttf and .otf from assets folder and classify it by family, i have a static way (which is not completely done), i have a method that returns a List with all the font families, hardcoded, so i have this other method to return a typeface given the font family (string) given and it's style:
public Typeface getTypeface(Context context, String name, int textStyle)
{
String fontFileName = "";
switch (name)
{
case "ComicNeue-Angular":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "ComicNeue":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "Impact":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "LiberationSans":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "Roboto":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
}
return Typeface.createFromAsset(context.getAssets(), fontFileName);
}
i want to add all fonts and classify it by family, so i can load a spinner with all font families. so i don't have to add lines to this code if i add another font family to the folder. (I have to add like 7 more font families to the project)
any ideas?
thanks in advance!
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.
I set a different color for each row in a ListView and I want to Drag and Drop each row along with the corresponding background color.
the way to set different row background is showing below, now I want to drag row to change with other row and each row's background must be exchange too
public static class DragListAdapter extends ArrayAdapter<String>{
public DragListAdapter(Context context, List<String> objects) {
super(context, 0, objects);
}
public List<String> getList(){
return list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item, null);
TextView textView = (TextView)view.findViewById(R.id.drag_list_item_text);
textView.setText(getItem(position));
switch(position){
case 0:
textView.setBackgroundResource(R.drawable.gray);
break;
case 1:
textView.setBackgroundResource(R.drawable.white);
break;
case 2:
textView.setBackgroundResource(R.drawable.Pink);
break;
case 3:
textView.setBackgroundResource(R.drawable.Cyan);
break;
case 4:
textView.setBackgroundResource(R.drawable.Gainsboro);
break;
case 5:
textView.setBackgroundResource(R.drawable.Magenta);
break;
case 6:
textView.setBackgroundResource(R.drawable.MediumSpringGreen);
break;
case 7:
textView.setBackgroundResource(R.drawable.SteelBlue);
break;
case 8:
textView.setBackgroundResource(R.drawable.Yellow);
break;
case 9:
textView.setBackgroundResource(R.drawable.LightSteelBlue);
break;
}
return view;
}