Hi, I need to change the text inside of a button after it has been pressed. How would I do this?
Thanks
//in your activity... get the button:
Button button = (Button) findViewById(R.id.myButton);
//make a click listener/handler:
OnClickListener myClickListener = new OnClickListener() {
...public void onClick(View v) {
......button.setText("New Text");
...}
};
//set the listener:
button.setOnClickListener(myClickListener);
I keep getting errors for the last line "button.setOnClickListener(myClickListener); "
Nevermind I got it... THANKS A MILLION!
Related
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();
}
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
Hey guys,
Is there any way to get the Android browser to handle key presses the same way a normal browser would? I'm mostly thinking about the arrow keys here. Usually, it just moves to the next link on the page. For example, try loading slides.html5rocks.com on an android device. Pushing the arrow keys should move to the next slide but instead it just highlights the links on the first slide. I'm trying this with a bluetooth keyboard but I assume it's the same on devices with physical keyboards.
On a side note, that website allows for swiping between slides. However, it does not do this when loaded in a desktop browser. Does anyone know how they did this?
Thanks
Edit: I just noticed that I posted this in the development section. I probably should have been in general. Sorry!
What exactly you want to get? Referenced site (html5rocks) work well (switching slides and so on) in HTML5 compliant browsers. For example in Google Chrome or in Firefox. Actually in Firefox 3.6.17 (version that I've tested) it works not exactly as expected but the mostly. And in my Android builtin browser it works probably as proposed. Left/right dragging gesture (on touch screen) has the same effect as pressing right/left button in desktop browser - swapping to the next/previous slide.
Anyways, if you'd like to deal with browser events, you probably should look at WebView class.
Yes, perhaps this was a bad example. What I'm wondering is if it is possible for android to interpret the arrow keys on a physical keyboard the same was as a desktop. This this particular example, you can just swipe, but I would like to be able to use the arrow keys on my keyboard to do this, just like I would on a desktop.
Maybe a better example would be http://htmlfive.appspot.com/static/gifter.html
this is impossible to use in android
the arrow keys should move your character, but they just scroll the page
I see... Well, the hack below should do what you want, but for some reason it doesn't.
Code:
public class WebActivity extends Activity {
private static final String TAG = "WebActivity";
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new HelloWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setOnKeyListener(onKeyListener);
webView.addJavascriptInterface(new JavaScriptInterface(), "Android"); // this is for debug only
webView.loadUrl("http://htmlfive.appspot.com/static/gifter.html");
// webView.loadUrl("file:///android_asset/gifter.html");
}
// This is for debug - redirect Android.log() javascript calls to Log.d().
// 'Android' here - is an 'interfaceName argument' of addJavascriptInterface() call above
public class JavaScriptInterface {
public void log(String message) {
Log.d(TAG, message);
}
}
private void simulateKeyEvent(String key, int code, boolean keyDown) {
Log.d(TAG, "simulateKeyEvent('" + key + "', " + code + ", " + keyDown + ")");
webView.loadUrl("javascript:(function(){var e=document.createEvent('KeyboardEvent');e.initKeyboardEvent('" + (keyDown ? "keydown" : "keyup" ) + "',true,true,null,'" + key + "',0,0,0," + code + ",0);document.body.dispatchEvent(e);})()");
}
OnKeyListener onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, ".onKey(): keyCode = " + keyCode + ", action = " + event.getAction());
int code;
String key;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
code = 37; // JavaScript KeyboardEvent.keyCode value for left arrow
key = "Left";
break;
case KeyEvent.KEYCODE_DPAD_UP:
code = 38; // ...
key = "Up";
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
code = 39;
key = "Right";
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
code = 40;
key = "Down";
break;
default:
Log.d(TAG, "Unknown key");
return false;
}
boolean keyDown;
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
keyDown = true;
break;
case KeyEvent.ACTION_UP:
keyDown = false;
break;
default:
Log.d(TAG, "Unknown action");
return false;
}
simulateKeyEvent(key, code, keyDown);
return true;
}
};
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Looks like the problem is in the implementation of initKeyboardEvent() in WebKit (or V8?). It always produces KeyboardEvent object with 0 values of keyCode and keyChar. So this code doesn't working in my Chrome too: event is dispatched, appropriate handler is called, but keyCode is 0.
Probably there is some other way to simulate keyboard event for JavaScript running within WebView. Or maybe there is some way to redefine KeyEvent processing by WebView. I think you should look for something like this.
This is interesting subject. If you'll find a solution, please write here about it.
References:
http://developer.android.com/resources/tutorials/views/hello-webview.html - basics of WebView usage
http://developer.android.com/reference/android/webkit/WebView.html - WebView in more details
http://lexandera.com/2009/01/injecting-javascript-into-a-webview/ - about interacting with JavaScript from WebView
http://stackoverflow.com/questions/1897333/firing-a-keyboard-event-on-chrome (and many other topics) - about problems with simulating keyboard events on WebKit
this is really frustrating. No matter what I do, I just end up with keycode 0. google really needs to fix the problem with initKeyboardEvent. Oh well..
Thanks for you help
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.