I am new to android. I want to set OnclickListner for different buttons which are located in different xml layouts.
You can also use a definition like that directly in the XML-file:
<Button android:onClick="myClickHandler" />
After that you can create the method "myClickHandler" in your Code like that:
class MyActivity extends Activity {
public void myClickHandler(View target) {
// Do stuff
}
}
Something like this:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Then all you need to do is reference the different buttons by their different ids set in the XML
Related
I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!
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'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 want to implement a click listener for a button on my main view. My code is something like below
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
But shows me error as follows
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
I have no idea what to do. Please help.
You are not using the correct interface to instantiate the mFinishLinstener variable...
It is possible you have an import specifying DialogInterface and that is confusing the view.
Try specifying View.OnClickListener explicitly.
private View.OnClickListener mFinishListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
As per my opinion Best way to implement On click event for the Button.
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
public void selfDestruct(View view) {
// Kabloey
}
Note: The above code is given in Android SDK - Button.
try this code :::
final Button button = (Button) findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Simply try this one as:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something when the button is clicked
}
};
you can also use like below code..
Button button = (Button)findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Write Your code here
}
});
You can also declare the onclick in the xml.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />
And in your code you would define the function as:
public void buttonClick(View view)
{
// handle click
}