Android button slow action - java

I have a button that leads to another page. And whenever I click it I get this info code in logcat :
12-07 16:09:45.073: I/ActivityManager(273): Displayed com.example.prva/.button: +1s764ms
Seconds and ms vary of course each time between 1-3 seconds. The problem is that I noticed that it takes a while for that button to open that page. It has some kind of pause or whatever and this is the only relevant thing I have found in the logcat that could be connected to it. How can I fix this, why is this button acting "slow"?
This is where the button code is :
package com.example.prva;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Meni_Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnv = (Button) findViewById(R.id.buttonv);
btnv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Meni_Splash.this, button.class));
}
});
}
}
And this is the class that opens :
package com.example.prva;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class button extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
final RadioButton rb1, rb2, rb3;
rb1 = (RadioButton) findViewById(R.id.radio1);
rb2 = (RadioButton) findViewById(R.id.radio2);
rb3 = (RadioButton) findViewById(R.id.radio3);
Button btn = (Button) findViewById(R.id.buttonplay);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rb1.isChecked())
{
MPRadio1.start();
}
else
{
if(rb2.isChecked())
{
MPRadio2.start();
}
else
{
if(rb3.isChecked())
{
MPRadio3.start();
}
}
}
}
}
);}}
I don't know what thing could make it so slow from these activities?

Your code looks pretty decent to be honest. Not sure entirely what could be causing it to intialise slowly.
But there are two areas to look at.
The first, most likely, is your layout loading:
setContentView(R.layout.button);
I dont imagine your layout to be complex though. But if it is, aka, lots of nested views (linear layouts within other linear layouts), or lots of views (textviews etc) in general on the page, then it could be taking a while to "inflate" the Layout.
Alternatively and less likely, is that MediaPlayer.create takes a fair while to load. The reason I suggest this, is I have no idea how it works, as I've not used it before.
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
The best thing to do, would be to profile it with the DDMS profiler. Or put a timer around it, and print the results to logcat.
Also, on a quick note, is it just 2-3 seconds loading? And is it really that bad for what its trying to do?

Related

Change action of a button according to text displayed

I would like to know how to change the action of a button according to what text is display in a TextView. I am using Android Studio.
I have tried to use an 'if statement' but that doesn't seem to work. So, I want a different sound to play according yo what text is displayed in the TextView.
package com.msp.exampleapplication;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
Button playsound_button;
MediaPlayer mySound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder = (TextView) findViewById(R.id.placeholder);
playsound_button = (Button) findViewById(R.id.playsound_button);
placeholder.setText(getIntent().getStringExtra("message"));
}
public void playSound(View view) {
if (placeholder.equals("BMW M4")) {
mySound = MediaPlayer.create(this, R.raw.sound);
mySound.start();
}
else if (placeholder.equals("BMW M5")) {
mySound = MediaPlayer.create(this, R.raw.sound2);
mySound.start();
}
}
}
So, if the text of the TextView (placeholder) is "BMW M4", then when the button is clicked, it must play R.raw.sound. And if "BMW M5" is displayed in the TextView (placeholder), then R.id.sound2 must play.
But as I said, I've attempted to use the if statement and when I click the button, no sound plays at all.
Yes, as #Akshay Bhat 'AB' said, you must do this:
if (placeholder.getText().toString().equals("BMW M4")) {
mySound = MediaPlayer.create(this, R.raw.sound);
mySound.start();
}
else if (placeholder.getText().toString().equals("BMW M5")) {
mySound = MediaPlayer.create(this, R.raw.sound2);
mySound.start();
}
Refer this post: how to get text from textview
There's one error in the comment tho, its toString() and not toString
You need to check the text in the TextView and not the reference of the TextView itself.
placeholder.getText().toString().equals("BMW M4")

Multiple activities make changes in a single layout

I'm just started to make my first android app and I'm trying to get more familiar with the basic principles of android developing. So, in no time my MainActivity exploded with lines of code. To make my code more maintainable, i'm trying to put pieces of code in different activities. Also according to the design principles of android: Don't Overload a Single Activity Screen
Now I'm struggling to use different activities with a single XML layout. I found some similar cases here like: this one But i'm also reading here that I should use fragments. I can't see how to work this out properly.
The specific problem I encounter with my code is that the second activity should change the background of the imageview to normal with the setImageResource, but it doesn't.
My code:
package com.test.scores;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageButton btn1, btn2;
int varMinusScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
varMinusScore = 1;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn1.setImageResource(R.drawable.btn01p);
}
switch (v.getId()) {
case R.id.btn2:
varMinusScore = 2;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn2.setImageResource(R.drawable.btn02p);
}
}
}
And the second activity:
package com.test.scores;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
public class ResetImageResources extends Activity {
private ImageButton btn1, btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn1.setImageResource(R.drawable.btn01);
btn2.setImageResource(R.drawable.btn02);
finish();
}
}
Activities are absolutely isolated from each others. The same XML file which you are setting as a content of each activity doesn't mean that it same/shared instance of layout. You should think not in terms of layouts, but in terms of activities.
In your case you just start second Activity, change background of buttons here, then go back and see first Activity. Any changes in second Activity would not be mirrored somewhere else. That's it.
Try this:
Insert a button to finish in second activity. Use finish() under the button interface button.setOnClickListener(new OnClickListener(){} ); then you'll clearly notice the difference between the backgrounds. Only if you click you can go back to main activity.

I need help making my game pause and resume

I have a game where you click on an image in the centre, and a value increases. When the app is opened, I've made a splash come up before the main activity starts (the clicking screen). However, every time I back out of the app, and click the icon again, it goes through the splash, goes to the main screen, and starts the game again, setting the value back to zero.
My Java for the Splash:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
#Override
public void onCreate(Bundle splashBundle) {
// TODO Auto-generated method stub
super.onCreate(splashBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(2000);
Intent mainIntent = new Intent("com.bipbapapps.leagueclickerapp.CLICKER");
startActivity(mainIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
My Java for the MainClass which is then run:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount = 0.0f;
Button minionClick;
TextView textGoldCount;
String textTotal;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
//Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
//String which will display at the top of the app
textTotal = goldCount + " Gold";
//Setting TextView to the String
textGoldCount.setText(textTotal);
//Setting onClickListener
minionClick.setClickable(true);
minionClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
break;
}
}
}
Anyone have any idea how to allow my game to pause and resume when it's minimized? Also, is there a way so that when the app is destroyed (properly closed), and restarted, the values for variables are kept? Would appreciate your help.
Inside onBackPress of the second activity, you should store the score in shared preferences. Every time you come on the onCreate of Splash activity, retrieve the score value and check if it is set to 0 then show splash screen else goto main activity with current score.

Unreachable code in Eclipse Android

I'm really new in android and java, but im trying to make an android app.
Im trying to make something were you can just type in your name and then it should view it by a push on a button. Eclipse is giving me the "unreachable code" error. I was wondering if you guys could see what i was doing wrong. The error is given in the two rules with final in front of it. If i remove one of these rules it will just move the error to the other rule.
Thank you in advance,
Marek
package com.tutorial.helloworld2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
}
Move return true to the end of method as the method execution will end on this statement and no code below will be executed.
it is due to return statement, return will return a true and exit the entire method you have currently written code in. you should write this in onCreate() like.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
now you will get your desired result

Set method return type to 'void'. Dont quite understand

Doing a basic android background service app.
Do not quite understand why there is a error (MainActivity.java). Error is at btnStart = (Button)findViewById(R.id.btnStart);
The quick fix provided was set return type to 'void'. Whereas for btnStop there is no error.
package com.example.backgroundservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Service;
public class MainActivity extends Activity implements OnClickListener{
btnStart = (Button)findViewById(R.id.btnStart); (ERROR HERE)
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
startService(serviceIntent);
}
});
btnStop.setonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent serviceIntent = new Intent (MainActivity.this,MyService.class);
stopService(serviceIntent);
}
});
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
Your code should be inside the onCreate method.
Before that method is called, your activity is not initialised, so there's no layout, and you can't find any UI element by id.
EDIT: Something like that would work:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout); // this layout must contain btnStart and btnStop
Button btnStart = (Button) findViewById(R.id.btnStart); // variables are declared then allocated
Button btnStop = (Button) findViewById(R.id.btnStop)
// ...<rest of your code>....
It should be inside onCreate.
Button btnStart,btnStop; // should be delcared. i guess you do not have this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btnStart = (Button)findViewById(R.id.btnStart); // initialize here
btnStop = (Button) findViewById(R.id.btnStop)
...// rest of the code
I guess you have the below outside onCreate (outside any method) and you have not declared btnStart
btnStart = (Button)findViewById(R.id.btnStart);
But even if you declare you need to inflate the layout first ans then initialize button or else you get NUllPointerException.
So Declare the buttons as class member and initialize it in onCreate as shown above
Edit:
Since you already have the listener annonymous inner class there is no need for you to implement OnClickListener and so you can remove this
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
Also your import staments are wrong
Replace this
import android.content.DialogInterface.OnClickListener;
BY
import android.view.View.OnClickListener;
You must override the activity onCreate method and set their content:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theLayoutThatContains_R.id.btnStart);
}
You get the error because you have code belonging to method body inside a class body. Eclipse notices the syntax error and proposes to "fix" it by changing the code to a method declaration. You don't get any further syntax errors as compilation stopped at the first syntax error.
As others have instructed, the correct place for code like this is the activity's onCreate() method.

Categories