Android. i am a newbie I have 6 images. One image view, two buttons previous and next. Have written the program, but when executing, crashes. I have written the logic of how to change the images in image view, I have removed all the syntax errors. here is my Mainactivity.java code
package com.example.hm2_koppineedi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageView;
Button previous,next;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
imageView= (ImageView)findViewById(R.id.imageView1);
previous = (Button)findViewById(R.id.button1);
previous.setOnClickListener((OnClickListener) this);
next = (Button)findViewById(R.id.button2);
next.setOnClickListener((OnClickListener) this);
}
public void onClick(View view)
{
int a=0;
switch (view.getId())
{
case R.id.button2:
if (a == 0)
{
imageView.setImageResource(R.drawable.hdimage);
a = 1;
}
else if (a == 1)
{
imageView.setImageResource(R.drawable.elephant);
a = 2;
}
else if (a == 2)
{
imageView.setImageResource(R.drawable.giraffe);
a = 3;
}
else if (a == 3)
{
imageView.setImageResource(R.drawable.horse);
a = 4;
}
else if (a == 4)
{
imageView.setImageResource(R.drawable.lion);
a = 5;
}
else if (a == 5)
{
imageView.setImageResource(R.drawable.tiger);
a = 6;
}
else if (a == 6)
break;
case R.id.button1:
a--;
button2.performClick();
next.performClick();
break;
}
}
}
Wrong code:
previous.setOnClickListener((OnClickListener) this);
next.setOnClickListener((OnClickListener) this);
Use this:
previous.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
);
I think you need to override the OnClick method, and make sure it is in your activity.
public class myActivity extends Activity {
...
#Override
public void onClick(View v) {
// Do something
}
...
}
You should not have to cast "this", but call...
setOnClickListener(this);
...from inside one of your activity methods (for instance : OnCreate), so that "this" be the activity. That way, all calls to "Click" in the context of your activity will be sent to your OnClick method.
You have lots of examples on this site to show that.
Related
I used below code to close app after back button pressed. Some time ago it worked but I tried to use it again and have:
Error:(88, 13) error: class, interface, or enum expected.
If I remove this code app can be build, I don't see where is problem?
Here is the MainActivity where onBackPressed is implemented:
package com.example.chab.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.a);
ImageView image1 = (ImageView) findViewById(R.id.b);
ImageView image2 = (ImageView) findViewById(R.id.c);
ImageView image3 = (ImageView) findViewById(R.id.d);
ImageView image4 = (ImageView) findViewById(R.id.e);
ImageView image5 = (ImageView) findViewById(R.id.f);
ImageView image6 = (ImageView) findViewById(R.id.g);
Picasso.with(this).load("http:/1.jpeg").into(image);
Picasso.with(this).load("http://1.jpeg").into(image1);
Picasso.with(this).load("http://1.jpeg").into(image2);
Picasso.with(this).load("http://1.jpeg").into(image3);
Picasso.with(this).load("http://1.jpeg").into(image4);
Picasso.with(this).load("http://1.jpeg").into(image5);
Picasso.with(this).load("http://1.jpeg").into(image6);
Button btnOne = (Button) findViewById(R.id.Btn);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Activitydwa.class);
startActivity(intent);
}
});
}
} //THIS BRACKET MUST BE MOVED TO THE END OF CODE!
private Boolean exit = false;
#Override
private void super.onBackPressed() {
if (exit) {
this.finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
EDIT:
Solved.
Bracket before Boollean must be moved to the end of code. Then all works. Thank you.
Your first problem is that you have your method implemented outside of the class. In java, methods need to belong to a class, interface, or enum. Double check your brackets and move your method to the inside of your class brackets. Your second problem is that you have the wrong signature for the onBackPressed method. See the code below:
This is what you have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
}
private Boolean exit = false;
#Override
private void super.onBackPressed() {
// ...
}
This is what you need:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
private Boolean exit = false;
#Override
public void onBackPressed() {
// ...
}
}
Try to put
super.onBackPressed()
before finish()
Try this.finish() OR
Try super.onBackPressed(); OR
call NavUtils.navigateUpFromSameTask(this); to get back to previous screen/ activity
I Have countDownTimer() method and want to print a certain toast if the user presses the button when the timer is at 10 or 9 seconds left. I am trying to read the time by reading the textview of the timer in the gameButton() method but it only prints the fail toast and not the pass toast. So why can I not do this? And also how can I get it to work? Thanks.
import android.app.Activity;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameScreen extends Activity {
private TextView time;
private Button start;
private Button cancel;
private Button gameButton;
private CountDownTimer countDownTimer;
private View.OnClickListener btnClickListener = new View.OnClickListener(){
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.start_ID :
start();
break;
case R.id.cancel :
cancel();
break;
case R.id.gameButton_ID :
gameButton();
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
start = (Button) findViewById(R.id.start_ID);
start.setOnClickListener(btnClickListener);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(btnClickListener);
time = (TextView) findViewById(R.id.time);
gameButton = (Button) findViewById(R.id.gameButton_ID);
gameButton.setOnClickListener(btnClickListener);
}
public void start(){
time.setText("15");
countDownTimer = new CountDownTimer(15 * 1000, 1000) {
#Override
public void onTick(long millsUntilFinished){
time.setText("" + millsUntilFinished / 1000);
/* gameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//if(time.getText() == "10" || time.getText() == "9" ){
Toast.makeText(new GameScreen(), "This is my Toast message!", Toast.LENGTH_LONG).show();
//}
}
}); */
}
public void onFinish(){
time.setText("Done !");
}
};
countDownTimer.start();
}
private void cancel(){
if(countDownTimer != null){
countDownTimer.cancel();
countDownTimer = null;
}
}
private void gameButton(){
if(time.getText() == "10" || time.getText() == "9" ){
Toast.makeText(getApplicationContext(), "PASS", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_SHORT).show();
}
}
}
Use equals method not == operator
time.getText().equals("10") || time.getText().equals("9")
See What is the difference between == vs equals() in Java?
I am a beginner in android.
My Scenario
I have a screen A which has 2 buttons
Button A
Button B.
When I open my application screen A opens up with the above 2 buttons, when I click on Button B a textview and Edittext is displayed.
What I want ?
When the back button is pressed the textview and edittext should hide and when I press back again, I should exit out of Screen A.
What have I tried so far ?
Is my below code correct for what I want ?
Main Activity.xml
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView title;
EditText userinput;
Button buttonA,buttonB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
userinput = (EditText)findViewById(R.id.userinput);
title = (TextView)findViewById(R.id.title);
buttonA = (Button)findViewById(R.id.buttonA);
buttonB = (Button)findViewById(R.id.buttonB);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
}
#Override
public void onBackPressed() {
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.buttonA:
break;
case R.id.buttonB:
title.setVisibility(View.VISIBLE);
userinput.setVisibility(View.VISIBLE);
break;
}
}
}
I referred this and this link but did not understand. If some one could help me in achieving me want I want 1
When the back button is pressed the textview and edittext should hide
and when I press back again,
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE) {
super.onBackPressed();
return;
}
title.setText(null);
userinput.setText(null);
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
Do like this.
#Override
public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
{
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
else
{
finish();
}
}
Hop it will do what you want.
Change the code to below
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
super.onBackPressed();
}
I am trying to play a sound on button click and I am trying to make it call the .start() function only but the problem is I set the i parameter in the onclick function and so it does not pass the i to the onclicklistner function any ideas how to fix that ??
here is my code
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
//Creating Sound arrays
int i=0;
MediaPlayer[] mediaplayer = new MediaPlayer[120];
Button button[] = new Button [120];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
//Initialize Button Array
for(int x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(this);
}
//Creating Media player array
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
button[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.akali:
i=0;
break;
case R.id.alistar:
i=1;
break;
}
mediaplayer[i].start();
}
} );
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Inside your Listener, you want to use a variable which is declared inside your Listener.
You are trying to use a variable (i) which is declared in code which is not run when the Listener is run.
new View.OnClickListener() {
#Override
public void onClick(View v) {
int idx;
switch(v.getId()) {
case R.id.akali:
idx=0;
break;
case R.id.alistar:
idx=1;
break;
}
mediaplayer[idx].start();
}
}
Or invoke the respective start() method right in the case.
Dump the 'i' declared. Replace method with this (the "Button theButton" needs to stay 'final').
for(int z=0;z<120;z++)
{
final Button theButton = button[z];
theButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
// do what you want.
}
});
}
I believe your onCreate should be created like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize the mediaplayer
for(int z=0;z<120;z++)
{
mediaplayer[z]=null;
}
mediaplayer[0]= MediaPlayer.create(this,R.raw.akali);
mediaplayer[1]= MediaPlayer.create(this,R.raw.alistar);
//Initialize Button Array
for( x=0;x<120;x++)
{
button[x]=new Button(this);
button[x].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaplayer[x].start();
}
} );
}
}
So basically you just need to make several button on the fly which produces different sound when clicked. Try this code:
Button myButton[] = new Button[100];
MediaPlayer mySound = new MediaPlayer();
for (int i=0; i<100; i++) {
myButton[i] = new Button(this);
myButton[i].setId(i+1);
myButton[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Resources res = context.getResources();
int soundId = res.getIdentifier("sound" + v.getId(), "raw", context.getPackageName());
mySound.create(getApplicationContext(), soundId);
mySound.start();
}
});
}
I have created an app that suppose to toggle the button background color on every click. Here is the code:
package com.example.flash.light;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button screen = (Button) findViewById(R.id.screen);
Drawable background = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(background);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
}
}
});
}
Click on the button doesn't response.
Thanks
I belive the problem here is that the resource returned by your
getResources().getDrawable(int id)
is different everytime you call it. Android just constructs new instance of drawable class instead of returning old one. (At least I belive that there is no caching in this case)
Comparing three different instances with == operator will never return true.
Second thing is an obvious bug in the code:
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(**newBackground**);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(**newBackground**);
}
instead of background you should have newBackground there.
Try this :
public class MainActivity extends Activity {
private boolean isBlack = false;
#Override
public void onCreate(Bundle savedInstanceState) {
// your code
final Button screen = (Button) findViewById (R.id.screen);
isBlack = true;
screen.setBackgroundColor(Color.BLACK);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (isBlack){
screen.setBackgroundColor(Color.WHITE);
isBlack = false;
}
else{
screen.setBackgroundColor(Color.BLACK);
isBlack = true;
}
}
});
}
}