How to add a button in android? - android

Can anybody tell how to add a button in android?

Check this Android Button tutorial; this simple example creates a Close Button.
All you need to do is:
1.Add Button widget to your Layout
<Button android:id="#+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/title_close" />
2.Attach a setOnClickListener method to the button instance:
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.layoutxml);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}

Dynamic:
Button btn= new Button(this);
btn.settext("Submit");
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
//your write code
}
});

According to official documentation of Buttons provided by Android.
You can first create Button in your .xml file.
Button.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
... />
And then cast your button with Button Class and set ClickListener.
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
For further detail you can visit this link

Related

Change the image drawable to a text by button click

I wanted to change the image drawable to a text on button click. I have a symbol tick(which is a button). On clicking the image, it should show a text "abc". I have tried but my text is coming over the image. I want only the text on button click,the image should not be seen. Can anyone help
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setText("abc");
}
});
My xml code is
<Button
android:id="#+id/button_a"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:background="#drawable/box" />
you can use
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setBackgroundResource(0);
button.setText("abc");
}
});

how to change the image on click of imagebutton?

I am make a image button .I need to change the image when I click on image button .Actually it change the background image but only for few seconds .why ?
here is my code
<ImageButton android:id="#+id/favorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/start"
android:background="#00ffffff"
/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/on" /> <!-- pressed -->
<item android:drawable="#drawable/off" /> <!-- default -->
</selector>
I need to show on image when I click on image button ..? can I write on java side ? can I write on click listener of image button ?
That's why you are using a selector for your button background and the image changes depending on the state of the button. If it is pressed the image will be "on" and in its normal state (no pressed and no focused) the image will be "off".
EDIT:
public class MainActivity extends AppCompatActivity {
ImageButton btn;
boolean isPressed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (ImageButton) findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.normal);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isPressed){
v.setBackgroundResource(R.drawable.normal);
}else{
v.setBackgroundResource(R.drawable.pressed);
}
isPressed = !isPressed; // reverse
}
});
}
<ImageButton
android:id="#+id/btn"
android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Actually it change the background image but only for few seconds .why ?
I think it's obvious,those items in drawable are for states that ImageButton is pressed by user or in normal mode.
can I write on java side ? can I write on click listener of image button ?
Yes, and yes again :D if you want to change it when user presses it, just do it like this :
XML layout:
<ImageButton android:id="#+id/favorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/off"
android:background="#00ffffff"
/>
Java side (eg. in you onCreate method)
ImageButton favorite;
boolean isFav = false;
public void onCreate(Bundle savedInstanceState){
///...
favorite = findViewById(R.id.favorite);
favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isFav = !isFav;
if (isFav){
favorite.setImageResource(R.drawable.on);
}
else{
favorite.setImageResource(R.drawable.off);
}
}
});
///...
}
change the source of the image by providing an absolute path or an internet url dynamically in the onClickListener of the image.
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Here before changing the image you can check if the appropriate //image is set to the view.
image.setImageResource(R.drawable.on);
}
});
and also can be like this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isPressed){
v.setBackgroundResource(R.drawable.normal);
}else{
v.setBackgroundResource(R.drawable.pressed);
}
isPressed = !isPressed; // reverse
}
});

One click listener for all views in a hierarchy?

Is there a way to set one click listener on all views in a hierarchy in one line? I believe we have to do something like the following:
<LinearLayout id="#+id/parent">
<TextView id="#+id/item1" />
<TextView id="#+id/item2" />
<TextView id="#+id/item3" />
</LinearLayout>
OnClickListener listener = ...;
View view = inflater.inflate(R.layout.the_above_layout);
view.setOnClickListener(listener);
view.findViewById(R.id.item1).setOnClickListener(listener);
view.findViewById(R.id.item2).setOnClickListener(listener);
view.findViewById(R.id.item3).setOnClickListener(listener);
...
But that's really a lot of code for a big view hierarchy. I could make a recursive function to do it, but is there something already available by android that does this for us?
Thanks
Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute in xml.
<LinearLayout id="#+id/parent">
<TextView id="#+id/item1" android:onClick="myFancyMethod" />
<TextView id="#+id/item2" android:onClick="myFancyMethod" />
<TextView id="#+id/item3" android:onClick="myFancyMethod" />
</LinearLayout>
Then in your Activity just define a method like this.
public void myFancyMethod(View v) {
// does something very interesting
}
That is probably the fastest way to do it.
that is nice example i think so bcz i m also finding shortest way to clici refer this site for click listner
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...
}
}
Well, this is the way add this line in your xml-
<TextView id="#+id/item1"
android:onClick="myMethod"
/>
And make sure you write this method in your class in which xml is used
public void myMethod(View v){
//content goes here
}
this will trigger your own methods

Writing onClick() event for an android custom dialog button

I have a button with id "ok" in my dialog.
Partial - Dialog Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_below="#+id/licenseTypeLayout">
<Button
android:id="#+id/ok"
style="#style/agreement_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
In activity:
Dialog dialog;
#OptionsItem(R.id.action_about)
boolean displayPopup() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.about_dialog);
dialog.setTitle(R.string.app_name);
dialog.show();
Button btn=(Button)findViewById(R.id.ok);
//btn remains empty
return true;
}
How would I write onClick() event for this button?
To initialize the Button, you need to use the Dialog object
Button btn = (Button) dialog.findViewById(R.id.ok);
Then set OnClickListener to the Button using setOnClickListener() as follows...
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//write your code here
}
});
I would guess you make the method:
public void closeDialog(View v){
// Do what you want to do here (event handler)
}
?

setOnClickListener causes a crash of an APP

Hello I have this very simple code I'm trying to run via Android Studio
public class MainActivity extends ActionBarActivity {
Button random;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = (Button) findViewById(R.id.button);
display = (TextView) findViewById(R.id.TextView);
random.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
I haven't really added very much, but whenever I use the setOnClickListener no matter what's inside of it crashes the app. I couldn't find a solution for this.
Thank you.
//edit: sorry. I added a wrong code, random is a button
It doesn't seem to be your case but I had the same problem and it was caused by having these lines
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
});
before
setContentView(R.layout.activity_main);
You are setting the setOnClickListener method on the random object which by your code is currently Null.
So you are getting a NullPointerException.
I think you intended it to be button instead.
What is "random"? I think correctly will be:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
In your code you are setting click listener on a button. Please make sure you get right id and right object to set listener. The button you want to set clcik listener on should have same id to the id you defined in you activity
In Activity
Button button =(Button)findViewById(R.id.button1);
In XML
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Firstly,check logcat by error level
Secondly,random is not initialized!!
You need
1.Create button in activity_main.xml
For example,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/myCoolButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
2.Init that button in code
Button myCoolButton = (Button) findViewById(R.id.myCoolButton);
3.Attach listener to button
myCoolButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}});
And it works! Hope that help you

Categories

Resources