Coin flip app mistake in code (Java) - java

I have tried to make this app where the coin flips on the click of the button. Everything seems to be right but when I run it on my Android, It doesn't show the picture from Imageview and it crashes after a few button clicks.
Can someone see what is wrong in this code?
I have already checked are the images in right location, and their names.
public class MainActivity extends AppCompatActivity {
Button bt1;
ImageView nov;
int strana;
Random r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.bt1);
nov = (ImageView) findViewById(R.id.novcic);
r = new Random();
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
strana = r.nextInt(2);
if (strana == 0) {
nov.setImageResource(R.drawable.pismo);
Toast.makeText(MainActivity.this, "Pismo!", Toast.LENGTH_SHORT).show();
} else if (strana == 1) {
nov.setImageResource(R.drawable.glava);
Toast.makeText(MainActivity.this, "Glava!", Toast.LENGTH_SHORT).show();
}
}
});
}
}

Related

Android java button value check

I'm trying to learn basic stuff in android studio with java. I need to create 1 activity where we need a Button and a text to input. When the button is clicked to check is the input value is between 3 words "Primary, Secondary , Higher). Here is the mainactivity code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private int check = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// Check for valid education
// if yes -> next view
if (check == 0 ) {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
} else if ( check == 1) {
startActivity(new Intent(this, Activity2.class));
}
}
}
Follow this way
add input field in you xml file above button view .
<EditText
android:id="#+id/edu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your education" />
In your mainactivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private EditText edu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edu = findViewById(R.id.edu);
button = findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v){
String check_edu= edu.getText().toString().toLowerCase();
if ( check_edu.equals("primary") || check_edu.equals("secondary") || check_edu.equals("higher") ){
startActivity(new Intent(this, Activity2.class));
} else {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
}
});
}
}

I made an app in which if you click a button an audio will play and if you click twice it will stop but the stop function is working only once

Hi I am new to coding and I am making an app in which if you click a button an audio will play and if you double click the button the audio will stop. But the stop function is only working once. How can I solve it. Here's the Code.
public class MainActivity extends
AppCompatActivity {
private Button rrjk_btn,
gdsph_btn, aspamm, stop_btn;
int counter = 0;
#Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.radhasoami_rakshak_jeev_ke);
rrjk_btn = findViewById(R.id.rrjk_btn);
rrjk_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 2)
{
mediaPlayer1.pause();
}
mediaPlayer1.start();
counter++;
}
});
//
MediaPlayer mediaPlayer2 = MediaPlayer.create(MainActivity.this, R.raw.guru_dhara_sheesh_par_haath);
gdsph_btn = findViewById(R.id.gdsph_btn);
gdsph_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.start();
}
});
//
aspamm = findViewById(R.id.gdsph_btn);
MediaPlayer mediaPlayer3 = MediaPlayer.create(MainActivity.this, R.raw.ae_satguru_pita_aur_malik_mere);
aspamm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer3.start();
}
});
}
}
Your counter int is always increasing so it will be equal to 2 once.
Check this code:
if (counter == 2){
mediaPlayer1.pause();
counter = 0;
}
mediaPlayer1.start();
counter++;
Anyway, this code doesn't make any sense if you want to know if the user is double clicking the button. Check this:
Implement double click for button in Android

How can I display a second wallpaper by clicking second button?

I've recently started developing apps. I want to display the wallpaper that I choose for users.
The first button works perfectly, but I don't know what code I need to use for the second button.
The same code does not work.
Here's my code:
import android.widget.Toast;
import java.io.IOException;
public class MainActivity<bitmap2> extends AppCompatActivity {
Button button1, buttons2, buttons3, buttons4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
buttons2 = (Button) findViewById(R.id.buttons2);
buttons3 = (Button) findViewById(R.id.buttons3);
buttons4 = (Button) findViewById(R.id.buttons4);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setWallpaper();
}
});
}
private void setWallpaper() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.str1);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Good appetite", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}

How to use accelerometer to press a button in android?

I am trying to use accelerometer to press a button in android studio for my dice app. Does anyone know how to hook it up to a button, so that when I shake my phone the dice will roll? I've tried to use SensorEventListener but couldn't get it to work. Thanks for any feedback/suggestion.
PS. I'm a noob.
Here is my java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rollButton;
rollButton = findViewById(R.id.rollButton);
final ImageView leftDice = findViewById(R.id.diceLeft);
final ImageView rightDice = findViewById(R.id.diceRight);
final ImageView topDice = findViewById(R.id.diceTop);
final int [] diceArray = {
R.drawable.perspective_dice_one,
R.drawable.perspective_dice_two,
R.drawable.perspective_dice_three,
R.drawable.perspective_dice_four,
R.drawable.perspective_dice_five,
R.drawable.perspective_dice_six,
};
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Dice", "You pressed the button!");
Random randomNumberGenerator = new Random();
int number = randomNumberGenerator.nextInt(6);
Log.d("Dice", "The random number is: " + number);
leftDice.setImageResource(diceArray[number]);
number = randomNumberGenerator.nextInt(6);
rightDice.setImageResource(diceArray[number]);
number = randomNumberGenerator.nextInt(6);
topDice.setImageResource(diceArray[number]);
}
});
}
}

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

Categories