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

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();
}

Related

Button click testing

how can I use Junit to test a button click, everything happens in Android studio in java?
I am very bad at Junit testing
public class ActivityMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
Intent intent = new Intent(ActivityMenu.this, Level_Menu.class);
startActivity(intent);
finish();
}catch (Exception e){
}
}
});}

Java Android: the button does not work after returning to the first layout

when I press two buttons the picture changes but when I move with the 3 button to the 2nd layout and I go back to the first picture it doesn't change anymore
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button changeLayout;
private Button exit;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
imageView = findViewById(R.id.imageView);
changeLayout = findViewById(R.id.changeLayoutBt);
exit = findViewById(R.id.exitBtn);
}
public void changeImage(View view) {
switch (view.getId()){
case R.id.button1:
imageView.setImageResource(R.drawable.orange);
break;
case R.id.button2:
imageView.setImageResource(R.drawable.java);
break;
}
}
public void changeLayout(View view) {
setContentView(R.layout.second_layout);
}
public void exit(View view) {
setContentView(R.layout.activity_main);
}
}
`
In order to create new layout got to File>New>Activity>Empty Activity
Change your changeLayout code to this one
public void changeLayout() {
startActivity(new Intent(MainActivity.this, Main2Activity.class);
}
Main2Activity.class is your newly created java class. It would be Main2Activity.java or what ever you named it.
Inside your onCreate() method. Just below
changeLayout = findViewById(R.id.changeLayoutBt);
Paste this code
changeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLayout();
}
});

Android Studio / How to apply SharedPreferenced to my code

I'm doing Quiz in Android Studio. The game is divided into two categories. After passing the first category, the second button (opening the second category) changes the status from setEnable "false" to "true". How to use the SharedPreferenced method in my code that the changes related to the second button (.setEnable) will be saved after closing the application.
Last level of first category
public class win extends AppCompatActivity implements View.OnClickListener{
Button win1;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_winflagi);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
win1 = (Button) findViewById(R.id.winflagi);
win1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==win1)
{
Intent myIntent = new Intent(win.this, Activity2.class);
myIntent.putExtra("isEnabled", "enabled");
startActivity(myIntent);
}
}
}
Class containing a buttons for two categories ...
button3 opened the first category
entrycity opens the second category
public class Activity2 extends AppCompatActivity implements View.OnClickListener{
Button button3;
Button entrycity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
entrycity = (Button) findViewById(R.id.entrycity);
entrycity.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menu);
if (v == button3) {
startActivity(new Intent(Activity2.this, flagi1.class));
Bungee.zoom(this);
mp.start();
}
if (v== entrycity){
Intent intent=getIntent();
String isEnabled = intent.getStringExtra("isEnabled");
if(isEnabled==null||isEnabled.equals("disabled")){
entrycity.setEnabled(false);
}
else{
entrycity.setEnabled(true);
startActivity(new Intent(this, cities1.class));
}
}
}
}
Set a boolean preference when you are enabling the Button.
else{
entrycity.setEnabled(true);
getSharedPreferences("MY_PREF", MODE_PRIVATE).edit().putBoolean("isEnabled",true).apply();
startActivity(new Intent(this, cities1.class));
}
and in your onCreate set the enabled status of the button based on the preference :
entrycity.setEnabled(getSharedPreferences("MY_PREF", MODE_PRIVATE).getBoolean("isEnabled",false));

Coin flip app mistake in code (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();
}
}
});
}
}

Multiple buttons in MainActivity Android studio

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
noteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Notes.class);
}
});
resuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Results.class);
}
});
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
}
});
When I run the application the buttons don't work. If I set the buttons as so..
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
Button resuBtn= (Button) findViewById(R.id.resuBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.
You should place:
this.noteBtn = (Button) findViewById(R.id.noteBtn);
this.notBtn.setOnClickListener(this);
this.resuBtn = (Button) findViewById(R.id.resuBtn);
this.resuBtn.setOnClickListener(this);
this.timeBtn = (Button) findViewById(R.id.timeBtn);
this.timeBtn.setOnClickListener(this);
in onCreate() instead of onClick(). Make the buttons belong to the class, so you can reference them in onClick(). You should not be setting new onClickListeners in onClick(), but should rather have a switch statement based on the clicked view's id to determine which button was pressed.
this code
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button timeBtn = (Button) findViewById(R.id.timeBtn);
...
}
is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called.
the correct way to do it is like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.but1);
Button resBtn = (Button) findViewById(R.id.resBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agendaBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewTimeTable) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.
I will suggest reading more about the Android Activity life cycle you can find it
here

Categories