How do I create on-screen buttons for android apps. I am using the SDK's Lunar Lander game as a base, and I want to make it so that you don't need a keyboard.
To create a button you add something like this in your layout XML file:
<Button
android:id="#+id/button_1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:text="Button 1"/>
To get a hook to the button in your code where you can add some action when the button is pressed add an OnClickListener:
findViewById(R.id.button_1).setOnClickListener(new MyButtonListener());
Declare the OnClickListener as a private class (or inline)
private class MyButtonListener implements OnClickListener {
#Override
public void onClick(View v) {
// Your code doing something cool goes here...
System.out.println("Click!");
}
}
Please look in the documentation that Mr Dittmar posted links to for more details, but this should hopefully get you started. :)
Take a look at the documentation on how to create a layout. After that, the documentation on how to create and handle buttons might come handy.
Related
I think that my question is easy but I have no idea how to solve it.
I made a java class (test_class) and xml file for this class. In xml file I made a button and its work correctly in test_class, but I have to use it in MainActivity.
I made this button public and made object of this class, but the button is null. I also tried to make button from main activity
Button btn = (Button) findViewById(R.id.logButton)
but its also null.
How can I solve this problem?
thanks in advance!
In MainActivity.java make a public void method that takes a View parameter.
e.g.
public void buttonPressed(View view) {
// do stuff
}
In activity_main.xml - in your button node add
<Button
...
android:onClick="buttonPressed"
...
/>
And now they're wired.
I want to run a function each time a button is pressed in my app. I know I can make a new button which extends Android's button and override onClick() but I have already made my entire app and want this new functionality as an afterthought. Can I somehow add this functionality without changing the class of all buttons in my app?
Lets say I want to add something like
runthisfunction();
before every onClick() code executes. There are many buttons in various activities and so they have their own onClick functions.
Is there a way that this function is run before each and every onClick() without the need to make a new class and changing all the existing buttons.
Add to your xml resource for whatever button/view you want to have an onClick listener:
android:onClick="fncClick"
For example:
<Button
android:id="#+id/btnClicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fncClick" />
Then in the activity that utilizes that xml resource, include:
public void fncClick(View view) {
//TODO: Code here
}
This ONLY attaches an onClick listener for this specific button - not all buttons
i stuck with a unusual problem
i have a restaurant application which includes menu icons fixed at the bottom of every layout,
my problem is i dont want to create onsetclicklistner() method of every icon on each of my activity class....
please give some suggestion so that i can make a common class where i can put all my footer icon click event and activities in it and use it on my every activity class with different setcontentview...
hope you all get my question...
looking forward for your reply
You don't need to setup onClickListeners programmatically. You can also put them in the layout XML like this:
<Button android:id="#+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_button_text"
android:onClick="myOnClick"
/>
Then you can declare a base activity class that contains this method in it, like this:
public class BaseActivity extends Activity {
public void myOnClick(View v) {
// Do whatever you want to do when the button is clicked
}
}
Then you write all of your activity classes so that they derive from BaseActivity instead of Activity.
I am a learning via a book so please forgive this newbie question.
I have a bunch of imageButtons in my xml, here is how one of them looks:
<ImageButton android:src="#drawable/level1" android:layout_width="wrap_content"
android:id="#+id/imageButton1" android:layout_height="wrap_content"
android:onClick="button_clicked1"></ImageButton>
and processing code:
public void button_clicked1(View v) {
text1.setText("clicked");
}
rather than have each button have its separate onClick code, is there anyway I can pass which button was clicked? for example button_clicked(1) and then button_clicked(2) instead of button_clicked1 like it is now (in the above example xml code)
or i have no choice but have to do it separately?
Kind of - what I like to do is make my View or Activity implement View.OnClickListener.
public class MyView extends ImageButton implements OnClickListener
Then during onCreate, I do something like:
((ImageButton)findViewById(R.id.imageButton1)).setOnClickListener(this);
then, in my onclick:
public void onClick(View view){
switch(view.getId()){
case R.id.imageButton1:
// do something.
break;
case R.id.imageButton2:
// do somethign else.
break;
}
Of course, you can definitely get creative and toss the switch statement if any of your buttons should trigger the same event behavior. Also, I'm not in a place where I can easily view my droid references so there may be an OnClickListener specific to ImageButton - if so, implement that on your containing View or Activity to consolidate the handlers...
Hope that makes sense - happy coding!
B
Probably a really basic answer to this but google is throwing up nonsense.
Ok, so i have a bunch of nested linear layouts, each one contains a textview and an imageview. What i want is my textview to be linked so that when a user clicks on the text, it will take the user to a new page that is in the same project. Not a website or anything.
Appreciate any help!!
You can add an onTouchListner to you TextViews and when user clicks on it you simply launch a new Activity. Google for adding touch listeners and then for launching activities and you will find the components you need.
Activities are like pages in Android apps.
Yeah, Juhani's right. You can use an onTouchListener. It's actually really simple. Just create a new .java file that loads the Layout you want in the onCreate. In the code you just use this line for the onClickListener:
startActivity(new Intent(this, newjavafile.class));
and add the new Activity to your manifest. I had something close to this exact problem. The nice thing about doing it this way is the back button on the phone/device works to get you back to the main screen.
First write attribute in TextView Like,
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Click Here" />
And in Java File
TextView text= (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,SecondActivity.class));
}
});