How do you add code to get input fro the back button (i.e. if back button is pressed... then do this)
You should press the "thanks" button if want continued help
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
...if ( keyCode == KeyEvent.KEYCODE_BACK) {
......//Do something
......return true;
...}
...return super.onKeyDown(keyCode, event);
}
do you mean for back button on a keyboard or the backbutton soft key on most android devices? If its the latter you would override the onBackPresed() function.
Code:
@Override
public void onBackPressed()
{
//bla
super.onBackPressed();
}
Related
Basiclly I need help making it so people can longpress on a button and choose to set as ringtone or notfication tone.
here is some of my code I use so far for the sound:
Code:
MediaPlayer mpButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
martino2k6 said:
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
Click to expand...
Click to collapse
sorry but still kind of confused because I added the following before you mentioned this and got a force close:
Code:
Button money = (Button) findViewById(R.id.money);
Button registerForContextMenu;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
I hate to bump an old thread but I am trying to add notifications and ringtones to a soundboard, I was able to successfully use the example here:
http://stackoverflow.com/questions/...ode-only-setting-first-button-as-ringtone-etc
but I have only been able to get it to save the audio from the first button. Could anyone point out how to get it working for each sound rather than just the first?
I would not rely on "==" returning what you think you want....use .equals() instead.
And I would probably just call one method and pass in the item id and let that method handle your processing. function1 and function2 are redundant.
I wonder how exactly you thought that just showing a toast with the Ringtone id will save it in device settings. in function1() and function2() you must add handlers to get resource from the id, set that stream as the desired ringtone, notification.
vamp6x6x6x said:
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
Click to expand...
Click to collapse
This is I'm guessing a beginner problem but here i go. I have a text game I've been toying around with in c# using the console to output the text of the game. Recently i got an android tablet and downloaded AIDE to see if I could make something similar since Java and C# are similar. So far I've gotten four buttons and made a simple app that outputs a different line of text per button press. What im now having trouble with is using a loop similar to the ones I use in the C# game. I'm trying to avoid creating a different activity for every screen if possible. Its probably easier just to show the code im using. When ran the buttons dont show and it just stalls. Any help would be greatly appreciated.
public class MainActivity extends Activity
{
int choice;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Buttons();
Gameloop(choice);
// super.finish();
}
public void Buttons()
{
Button bt1 = (Button) findViewById(R.id.b1);
Button bt2 = (Button) findViewById(R.id.b2);
Button bt3 = (Button) findViewById(R.id.b3);
Button bt4 = (Button) findViewById(R.id.b4);
bt1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 1;
}
});
bt2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 2;
}
});
bt3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 3;
}
});
bt4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 4;
}
});}
public void Gameloop(int choice)
{
boolean Game = true;
boolean game = true;
TextView gameV = (TextView) findViewById(R.id.game);
gameV.setText("Loop Test");
do
{
do
{
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 2");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 2");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 2");
gameV.setTextSize(45);
}
if (choice == 4)
{
game = false;
}
}
while (game == true);
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 1");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 1");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 1");
gameV.setTextSize(45);
}
if (choice == 4)
{
Game = false;
}
}
while (Game == true);
}
put your code in [.code] brackets so its easier for us to read.
i dont know c#, but java would be more if/else statements, not endless ifs.
and where you have the (onclick, choice=3) sections, take that out and do yous set texts inside each different button click.
and you do have an xml with the buttons layout right?
You cannot have a loop method like that in Android. It would block the UI Thread and then it wouldn't register a ButtonClick. Android already has a Looper! If you really need to do it like that you can create a second Thread.
But I would just put your .setText(...) in the onClickListeners!
If you have already developed it for C#, why do you not programm it for Android in C#, too?
Have a look at this post: [GUIDE]C# for Android----for Starters----UPDATED
And this forum with more guides related to the C languages (including C#): http://forum.xda-developers.com/forumdisplay.php?f=2198
Btw, please put your code in
Code:
tags for better readability. ;)
Thanks for the replies. My first attempt at using Android was to simply have each of the four button presses return a line of text in the textview. Such as i press button one and the textview shows "Youve pressed button one". I was trying to attempt something similar but with loops. I do have an xml for the layout, my first attempt was within each onclick method for each button had a single textview setText. Here's the code with the loops.
Code:
public class MainActivity extends Activity
{
int choice;
/** Called when the activity is first created. */
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Buttons();
Gameloop(choice);
// super.finish();
}
public void Buttons()
{
Button bt1 = (Button) findViewById(R.id.b1);
Button bt2 = (Button) findViewById(R.id.b2);
Button bt3 = (Button) findViewById(R.id.b3);
Button bt4 = (Button) findViewById(R.id.b4);
bt1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 1;
}
});
bt2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 2;
}
});
bt3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 3;
}
});
bt4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 4;
}
});}
public void Gameloop(int choice)
{
boolean Game = true;
boolean game = true;
TextView gameV = (TextView) findViewById(R.id.game);
gameV.setText("Loop Test");
do
{
do
{
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 2");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 2");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 2");
gameV.setTextSize(45);
}
if (choice == 4)
{
game = false;
}
}
while (game == true);
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 1");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 1");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 1");
gameV.setTextSize(45);
}
if (choice == 4)
{
Game = false;
}
}
while (Game == true);
}
DChar21 said:
Thanks for the replies. My first attempt at using Android was to simply have each of the four button presses return a line of text in the textview. Such as i press button one and the textview shows "Youve pressed button one". I was trying to attempt something similar but with loops. I do have an xml for the layout, my first attempt was within each onclick method for each button had a single textview setText. Here's the code with the loops.
Code:
public class MainActivity extends Activity
{
int choice;
/** Called when the activity is first created. */
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Buttons();
Gameloop(choice);
// super.finish();
}
public void Buttons()
{
Button bt1 = (Button) findViewById(R.id.b1);
Button bt2 = (Button) findViewById(R.id.b2);
Button bt3 = (Button) findViewById(R.id.b3);
Button bt4 = (Button) findViewById(R.id.b4);
bt1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 1;
}
});
bt2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 2;
}
});
bt3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 3;
}
});
bt4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 4;
}
});}
public void Gameloop(int choice)
{
boolean Game = true;
boolean game = true;
TextView gameV = (TextView) findViewById(R.id.game);
gameV.setText("Loop Test");
do
{
do
{
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 2");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 2");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 2");
gameV.setTextSize(45);
}
if (choice == 4)
{
game = false;
}
}
while (game == true);
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 1");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 1");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 1");
gameV.setTextSize(45);
}
if (choice == 4)
{
Game = false;
}
}
while (Game == true);
}
Click to expand...
Click to collapse
i think you can simplify your code in this way, and should work:
Code:
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.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button bt1, bt2, bt3, bt4;
private TextView gameV;
private int choice;
private boolean game;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gameV = (TextView) findViewById(R.id.game);
bt1 = (Button) findViewById(R.id.b1);
bt2 = (Button) findViewById(R.id.b2);
bt3 = (Button) findViewById(R.id.b3);
bt4 = (Button) findViewById(R.id.b4);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
}
[user=439709]@override[/user]
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;
}
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.b1:
choice = 1;
//do something else on b1 click
gameV.setText("You've Pressed Button 1 Loop 2");
gameV.setTextSize(35);
//if needed add conditional statements or what you want
break;
case R.id.b2:
choice = 2;
gameV.setText("You've Pressed Button 2 Loop 2");
gameV.setTextSize(25);
//other code here
break;
case R.id.b3:
choice = 3;
gameV.setText("You've Pressed Button 3 Loop 2");
gameV.setTextSize(45);
//something else here
break;
case R.id.b4:
choice = 4;
game = false;
//do stuff
break;
}
}
}
hope it helps with your app :good:
DChar21 said:
Thanks for the replies. My first attempt at using Android was to simply have each of the four button presses return a line of text in the textview. Such as i press button one and the textview shows "Youve pressed button one". I was trying to attempt something similar but with loops. I do have an xml for the layout, my first attempt was within each onclick method for each button had a single textview setText. Here's the code with the loops.
Code:
public class MainActivity extends Activity
{
int choice;
/** Called when the activity is first created. */
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Buttons();
Gameloop(choice);
// super.finish();
}
public void Buttons()
{
Button bt1 = (Button) findViewById(R.id.b1);
Button bt2 = (Button) findViewById(R.id.b2);
Button bt3 = (Button) findViewById(R.id.b3);
Button bt4 = (Button) findViewById(R.id.b4);
bt1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 1;
}
});
bt2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 2;
}
});
bt3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 3;
}
});
bt4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 4;
}
});}
public void Gameloop(int choice)
{
boolean Game = true;
boolean game = true;
TextView gameV = (TextView) findViewById(R.id.game);
gameV.setText("Loop Test");
do
{
do
{
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 2");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 2");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 2");
gameV.setTextSize(45);
}
if (choice == 4)
{
game = false;
}
}
while (game == true);
if (choice == 1)
{
gameV.setText("You've Pressed Button 1 Loop 1");
gameV.setTextSize(35);
}
if (choice == 2)
{
gameV.setText("You've Pressed Button 2 Loop 1");
gameV.setTextSize(25);
}
if (choice == 3)
{
gameV.setText("You've Pressed Button 3 Loop 1");
gameV.setTextSize(45);
}
if (choice == 4)
{
Game = false;
}
}
while (Game == true);
}
Click to expand...
Click to collapse
I have not been that patient to read your code with full patient. But I think, when you call gameLoop(choice) in onCreate. that value of choice has not been initialized. And when you declare your variables inside the Listeners. The compiler wont loop or go back to the onCreate method. So basically you should call gameLoop() inside your listener method.
Example:
Code:
bt2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
choice = 2;
Gameloop(choice);
}
});
This code can be made a little more precisely. You can use inner classes and switches... instead of using a buch of if-elses.
Again, I have not been patient enough, to go through the complete code. I may have been mistaken..
{ Hope it helps ! }
Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.
Hi Im just starting to learn android code and I dont understand why this doesnt work. Just for as a test I wanted to create 2 pages (or 1 page and 1 popup page but i thought 2 pages would be easier to make), and a way to get from one to the other, which I thought could be solved easly with a button on each page which takes me to the other page.
So I started with this:
HTML:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.layout2);
} });
Basicually I start in a layout called activity_my which has a button and when I press it I get to the next page called layout2. I thought I could just make a similiar function to get back to my original page:
HTML:
final Button button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
But now I get error for whatever reason
Hi,
Basically in android, a 'page' is an Activity (class where you attach a XML layout).
So you must create 2 activity with 2 layout.
To switch activity use this foloowing code :
HTML:
Intent intent = new Intent(this, YourSecondActivity.class);
startActivity(intent);
Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.
JonanomisK said:
Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.
Click to expand...
Click to collapse
Code:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples
Jonny said:
Code:
Button button = (button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples
Click to expand...
Click to collapse
Thanks! I'll try implementing that later.