I'm a beginner in Android programming.
How do you execute a task when a button is clicked? I have heard you can use the onclick method but I'm not quite sure how to use it.
i think this will be useful:
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Put some code here for response to button click
}
});
Write your code in the Activity's class as a function:
public void myFunction(View v){
//all codes here for the click
}
Then in the design, select the Button, and in the property list look for the property "onclick". In the dropdown you will find your method listed there, choose it.
This will link you button click to the function being triggered.
Related
I have just started to learn Android Application Development from thenewboston.com tutorials.
I had a confusion with setting the onClickListener event handler. When setting it for a button in the main activity, they used the Button class.
redButton.setOnClickListener(
new Button.onClickListener(){
public void onClick(View v){
// Do Something
}
}
);
But when setting it for a fragment they used the view class.
redButton.setOnClickListener(
new View.onClickListener(){
public void onClick(View v){
// Do Something
}
}
);
What is the difference between the two ? And when to use them ?
Please Help !!
In Button also, you can use View also as you are using in fragment.
Actually both works identically.It is just the way you want to go for better solution than other.
For more details you could refer:
setOnclickListener vs OnClickListener vs View.OnClickListener
Difference between specifying the view.onclicklistener and just having onclicklistener
Button.onClickListener() could only be used for Button. But View.onClickListener() could be used for any view. So what is the right way of doing it?
Well, what you have currently done is called 'setting an anonymous listener`. That's bad. More info here: https://softwareengineering.stackexchange.com/a/110113
The best way to do is let your Activity or Fragment implement the View.onClickListener() and the override the onClick() method.
Button is derived class of View, so onClickListener of Button is overridden from onClickListener for View.
And there is no difference between two implementations.
In the documentation for button example uses view.onClickListener
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
so that seems the recomanded way.
I'm new to android development. I've a doubt.
I know that you can add a button and initialize it like
Button b1=(Button) findViewById(R.id.button1);
and I can also give a unction name in the XML file.
android:onClick="click_event"
My doubt is, which is the best and efficient way?
like it says that its better to use #string resource instead of a hard-coded one.
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1
So in your xml you would have somewhere
<Button
android:id="#+id/button1"
<!-- other properties -->
/>
You can add a Button programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="#+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
#Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener in your Activity declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
#Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.
I'm new to android development. I've a doubt.
I know that you can add a button and initialize it like
Button b1=(Button) findViewById(R.id.button1);
and I can also give a unction name in the XML file.
android:onClick="click_event"
My doubt is, which is the best and efficient way?
like it says that its better to use #string resource instead of a hard-coded one.
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1
So in your xml you would have somewhere
<Button
android:id="#+id/button1"
<!-- other properties -->
/>
You can add a Button programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="#+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
#Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener in your Activity declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
#Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.
I have an alarm symbol on my interface. I want it like if anyone clicks on this then he/she can get another interface.
so how to write a click event of Imageview. plz give me answer. Thank you
You can add an OnClickListener to ImageView using setOnClickListener method on your ImageView and write your code in the listener's onClick method.
imageView.setOnClickListener(clickListener);
OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
if (v.equals(imageView)) {
// Write your awesome code here
}
}
};
Faster.
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your code.
}
});
with lambda expression
img_drawer_open_icon.setOnClickListener(v -> Home.drawerLayout.openDrawer(GravityCompat.START));
Also just do it in some easy steps visually:
1- Click your Image
2- From Attribute, Select OnClick and Type a Name Such as onCkick_btnST
3- In code of Your xml, find and select the above verb
4- using yellow bulb menu, select "Create OnClick Event Handler"
5- Fill your Code in created area.
The benefit of this methode is that U`LL see that event title in structure tab of Android Studio!
I have three buttons in one activity.when each of the button is clicked ,different layout should be shown with in the same activity.for example if first button is clicked,edit boxes and button should be shown.if second buttojn is clicked listview should be shown etc..
Define different layout files for each layout.
Then after each click event have the intent call this particular activity recalled.
Have setContentView() called conditionally ie determining the particular clickevent and vice versa.
This you can do if you want complete activity to be layuot in diffrent manner. Otherwise if you want some widgets to be displayed on button click then it is pretty easy to show them on click event.
You might wanna consider a "TabWidget" for this. It actually does what you need.
A sample tutorial here.
Why don't you just include the all the layout elements in your single layout, then use the setVisibility attribute to turn them on and off, depending on which button is pressed.
Something like this pseudo code:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
}
});