How can I do key event. I do it, but on button event. I have to change it to key.
I have my keyboard from How can you make a custom keyboard in Android?
My buttons events:
public class MainActivity extends AppCompatActivity {
private static final int pic_id = 123;
EditText editText;
Button button1;
Button button2;
Button button3;
Button button5;
Button button6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button1 = (Button) findViewById(R.id.n1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("I am custom Keyboard!");
}
});
final MediaPlayer sawSound = MediaPlayer.create(this, R.raw.playagame);
button2 = (Button) this.findViewById(R.id.playMusic);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sawSound.start();
}
});
}
}
I don't know whether I got you right: do you mean whether reacting to key events of a physical keyboard? If so, you should be able to overwrite dispatchKeyEvent of your keyboard activity.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_2:
case KeyEvent.KEYCODE_NUMPAD_2:
handleInput("2");
return true;
}
}
return super.dispatchKeyEvent(event);
}
Your code seems to be working fine.....it plays the music
check your xml file and cross-check if you are reffering the correct id to each button
and check your device volume sound incase its muted
and also post your aactivity.xml file if still didnt fix
Related
for my android app, I use a button to go to the next Activity.
the problem is when I touch the button on the screen one instance of the activity is created but when
I use performClick() method to click the button programmatically, it creates two instances of the activity. ( performClick() is called from a callback method).
I used the CLEAR_TOP FLAG but it seems to break the back button.
Any idea how to solve this problem ?
this is what my code looks like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
}
private void A_callback_method(){
if (some_condition_to_launch_activity){
btn.performClick();
}
}
Just a simple trick. create function
private void function_name(){
startActivity(new Intent(Activity1.this, Activity2.class));
}
Then on the button onClickListener
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
function_name();
}
});
and also inside A_callback_method
private void A_callback_method(){
if (some_condition_to_launch_activity){
function_name();
}
}
hope this will solve your problems.
I succesfully did a work around to solve this problem by adding a boolean variable intialise it to false in the onResume() method and then did the following:
private boolean clicked_btn;
private void A_callback_method(){
if ((some_condition_to_launch_activity)&&!clicked_btn){
clicked_btn=true;
btn.performClick();
}
}
How to combine two functions in one button?
How to make when button clicked - the button text change and sound will play
Now I can only play the sound or just change the button text
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button one = (Button) this.findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String ButtonText=one.getText().toString();
if(ButtonText.equals("Stop")){
one.setText("Play");
}else {
one.setText("Stop");
}
}
});
It seems that you're already changing the button text in your onClick event handler. If you're using some method call to play the desired sound, you can just put that in your onClick handler method as well. Eg:
public void onClick(View v) {
String ButtonText=one.getText().toString();
// method call that plays the sound
if(ButtonText.equals("Stop")){
one.setText("Play");
} else {
one.setText("Stop");
}
}
I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style
I'm trying to learn android development with Android Studio but I can't seem to figure out why my clicks are not registering. Is there something I'm missing here?
I don't like the way Stack Overflow FORCES me to format things they way THEY want. Here is pastebin! :_
http://pastebin.com/hdzZpsud
call this changeText() method inside onCreate() method of your activity
your activity should look like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
changeText();
}
public void changeText(){
final Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView)findViewById(R.id.largetext);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("Button has been pressed");
text.setText("The large text has been changed");
}
});
}
}
In fact, you didn't set the listener. So you should add changeText() after setContentView(R.layout.activity_my);.
I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}