Android Programming mistake - java

I create an app name of android wallpaper but the problem is that when i clicked on left and right button the next and previous wallpaper does not comes. I tried it lots but failed.
public class Wallpapers extends AppCompatActivity {
WallpaperManager managerr;
Button B;
Button b1;
Button b2;
ImageView m;
int a[]={R.drawable.w1,R.drawable.w2,R.drawable.w3};
int aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallpapers);
click();
}
public void click()
{
m = (ImageView) findViewById(R.id.imageView);
B=(Button)findViewById(R.id.button3);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.w1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if()
{
m.setImageResource(R.drawable.w2);
}
else if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
{
m.setImageResource(R.drawable.w3);
}
}
});
}
}
That's the code.
That's the image of app:

you have set onClickListener only for b1.
change your code follows
int currentPosition = 0;
public void click(){
m = (ImageView) findViewById(R.id.imageView);
B= (Button) findViewById(R.id.button3);
left = (Button) findViewById(R.id.button);
right = (Button) findViewById(R.id.button2);
managerr = WallpaperManager.getInstance(getApplicationContext());
m.setImageResource(R.drawable.m1);
left.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition > 0){
m.setImageResource(previuosDrawable);
currentPosition--;
}
}
}
right.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
if(currentPosition < pictures.length){
m.setImageResource(nextDrawable);
currentPosition++;
}
}
}
}

First of all, if() has no conditions so it can't works add first if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
and then add all the else if you need and finally the else including m.setImageResource(R.drawable.w2);
Second you need is to setOnClickListener to both Button objects because you only set the listener to b1 and not to b2
For what's B?
Why you have an array called a with the drawables if you don't access to it fields? You can access to drawables using a[index] and increasing or reducing the index number from 0 to 3 or from 3 to 0 when you click b1 or b2. It will be better...

Related

How do I pass the string from a textview to another texview on the same activity?

I am working with this bingo callboard project in Android Studio. The principle is when I click a number button, it will show on the first textview. When I click the second number button, the first textview will show the current number pressed and the previous string will be passed to the second textview. How would I do this? Screenshot of app included. A sample of the MainActivity.java code is here:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
Screenshot
Hey so from what I understand you need to update the lst textview with the data of txt textView. Well you can write a common method which can be called from any button click with the new data. something like this:
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
updateTextView("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
updateTextView("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
updateTextView("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
updateTextView("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
updateTextView("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
}
private void updateTextView(String newData){
String oldData = txt.getText().toString();
lst.setText(oldData);
txt.setText(newData);
}
}
This will update your last textview with your txt textview data. YOu can call this method from your button clicks with the new data as a parameter . Hope this helps you
There is nothing wrong with p.matthew13's code. The problem is with the code sequence. I may also have to delete a line on my code because it's already redundant.
The redundant code is txt.setText(""); which contradicts txt.setText(newData);
I have to delete my setText code and replace it with the updateTextView("");
Also, I did place p.matthew13's code before my public void.
And that fixes my problem, after trying to shuffle up the code execution sequence.
Thanks very much!

Two Views, one Animation but there's a problem

This is my code:
AlphaAnimation anim_fadeIn;
Button button, button2;
TextView t, e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
t = findViewById(R.id.text_Splash_t);
e = findViewById(R.id.text_Splash_e);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
anim_fadeIn = new AlphaAnimation(0.0f, 1.0f);
anim_fadeIn.setDuration(1000);
anim_fadeIn.setFillAfter(true);
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
Scenario:
press button1 and text1 will animate (even if you do it some times). Then pressing button2 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate
Scenario 2:
press button2 and text2 will animate (even if you do it some times). Then pressing button1 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate.
How can I avoid this problem
Remove new Thread wrap, just call mAnimate() or mAnimate2() in main thread like this:
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
...
Also mAnimate, mAnimate2 can be optimized like this:
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
What you actually do is you register the same animation to two Views. If you want to only animate one view at the same time you have to clear the Animation for the other View at first otherwise both will start. E.g.
private void mAnimate() {
e.clearAnimation();
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
t.clearAnimation();
e.startAnimation(anim_fadeIn);
}

Setting textview text in a method

I am trying to create a method to determine the winner of the game, not worried about the logic of the finding the winner.
In the code I was attempting to add a getWinner() method that would determine the winner when the counter hit 10 moves. In the getWinner() method I took my textview object tv and wrote tv.setText("worked");. The app would crash. If i skip adding the getWinner() method and just toss in the tv.setText() straight into the code it works though.
Since I am not sure how well I explained what's going on maybe my comments throughout the code can give a better understanding.
public class MainActivity extends AppCompatActivity {
public int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button1.setText("O");
}
else
{
button1.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button2.setText("O");
}
else
{
button2.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button3.setText("O");
}
else
{
button3.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button4.setText("O");
}
else
{
button4.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button5.setText("O");
}
else
{
button5.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button6.setText("O");
}
else
{
button6.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button7.setText("O");
}
else
{
button7.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button8.setText("O");
}
else
{
button8.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner();
}
}
});
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button9.setText("O");
}
else
{
button9.setText("X");
}
counter++;
if (counter == 10)
{
//getWinner(); ---this does not work
//tv.setText("works"); ---this works
}
}
});
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}
Try passing textview obj to the method getWinner and then try to set it.getWinner should be outside oncreate method.
As other answer has pointing out, you're creating a method inside a method which is a code error and you're trying to access a variable inside of another method which is a method scope.
First, we need to resolve the first problem.
If we simplify your code, it will be something like this
public class MainActivity extends AppCompatActivity {
public int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
// get all the view.
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
/**
public void getWinner() {
tv.setText("does not work");
}
**/
}
}
From the above code, you can see that you're trying to create getWinner() inside of onCreate() which is wrong.
It should be:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
final TextView tv = (TextView) findViewById(R.id.winnerTextView);
}
public void getWinner() {
tv.setText("does not work");
}
}
Now the code is working but it will giving you an error because getWinner() can't found the tv variable which is only inside the onCreate() method. So, you need to make it a class scope like this:
public class MainActivity extends AppCompatActivity {
// make it class scope.
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
...
TextView tv = (TextView) findViewById(R.id.winnerTextView);
...
}
public void getWinner() {
tv.setText("does not work");
}
}
Both of the problems happen because you're creating a method with a too much line of code. You should always make your method not more than 15 line. This is the best practice to make your code more maintainable in the future.
Don't do too much logic in a single method. Focus only solving one problem in a method. Always use a Divide et impera principal to separate a bigger problem to many smaller problems.
Try it by putting the getWinner() method outside your onCreate() function and pass the textView reference to it.
I guess it should work.
What you may do is the,
Declare the TextView objects globally.
you can not use the background thread to set text so be careful if you uses any background thread.
Define tv at globally and no need to put getWinner() into on create so pickup it out
public class MainActivity extends AppCompatActivity {
public int counter = 1;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
tv = (TextView) findViewById(R.id.winnerTextView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter % 2 == 0) {
button1.setText("O");
}
else
{
button1.setText("X");
}
counter++;
if (counter == 10)
{
getWinner();
}
}
});
}
public void getWinner() {
tv.setText("does not work");
}
}
make getWinner() method outside of onCreate() and make tv global variable. it should work.

Opening next Activity

I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.

android Clear Button in a simple counter

I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});

Categories

Resources