Android: onClick causing me to go back an Activity - android

I am trying to build a dynamic UI, but when I add the onClick method to the button whenever I push the button I go back to my previous activity. Any ideas on how to fix it?
my button's code: (the addMenu method is never run in the activities class)
<Button
android:text="New Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newButton"
android:layout_weight="1"
android:onClick="addMenu"/>
here is my addmenu code although no matter what goes in here(even if nothing at all) it still won't work
public void addMenu()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}

whenever I push the button I go back to my previous activity.
Sounds like your app is crashing and restarting... read the logcat, and you'd see something along the lines that your method signature is wrong.
android:onClick="addMenu" needs a method of public void addMenu(View v).
Or just use Java to set the button listener and remove android:onClick.
findViewById(R.id.newButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMenu();
}
}

try this
/**
* #param v android:id="#+id/newButton"
*/
public void addMenu(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}

Related

setting onclicklistener for several buttons [duplicate]

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.

Android: Make several buttons use the same onClickListener object [duplicate]

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.

Which is the best way to add a button?

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.

Error in changing button visibility onClick

I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.

How to disable an Android button?

I have created a layout that contains two buttons, Next and Previous. In between the buttons I'm generating some dynamic views. So when I first launch the application I want to disable the "Previous" button since there wont be any previous views. I also want to disable the "Next" button when there are not more views to display. Is there anyway to disable the buttons?
Did you try this?
myButton.setEnabled(false);
Update: Thanks to Gwen. Almost forgot that android:clickable can be set in your XML layout to determine whether a button can be clickable or not.
Yes it can be disabled in XML
just using:
<Button
android:enabled="false"
/>
You can't enable it or disable it in your XML (since your layout is set at runtime), but you can set if it's clickable at the launch of the activity with android:clickable.
You just write a single line of code in your activity
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);
When you want to enable the same button just write
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(true);
In Java, once you have the reference of the button:
Button button = (Button) findviewById(R.id.button);
To enable/disable the button, you can use either:
button.setEnabled(false);
button.setEnabled(true);
Or:
button.setClickable(false);
button.setClickable(true);
Since you want to disable the button from the beginning, you can use button.setEnabled(false); in the onCreate method.
Otherwise, from XML, you can directly use:
android:clickable = "false"
So:
<Button
android:id="#+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:clickable = "false" />
In my case,
myButton.setEnabled(false);
myButton.setEnabled(true);
is working fine and it is enabling and disabling the button as it should. But once the button state becomes disabled, it never goes back to the enabled state again, although it's clickable. I tried invalidating and refreshing the drawable state, but no luck.
myButton.invalidate();
myButton.refreshDrawableState();
If you or anyone having a similar issue, what works for me is setting the background drawable again. Works on any API Level.
myButton.setEnabled(true);
myButton.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.myButtonDrawable));
In Kotlin, if you refer the Button View with id then, enable/disable button as like
layout.xml
<Button
android:id="#+id/btn_start"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="#string/start"
android:layout_alignParentBottom="true"/>
activity.kt
btn_start.isEnabled = true //to enable button
btn_start.isEnabled = false //to disable button
With Kotlin you can do,
// to disable clicks
myButton.isClickable = false
// to disable button
myButton.isEnabled = false
// to enable clicks
myButton.isClickable = true
// to enable button
myButton.isEnabled = true
WRONG WAY IN LISTENER TO USE VARIABLE INSTEAD OF PARAMETER!!!
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnSend.setClickable(false);
}
});
RIGHT WAY:
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/** check given view by assertion or cast as u wish */
if(v instance of Button) {
/** cast */
Button button = (Button) v;
/** we can perform some check up */
if(button.getId() == EXPECTED_ID) {
/** disable view */
button.setEnabled(false)
button.setClickable(false);
}
} else {
/** you can for example find desired view by root view */
Button bt = (Button) v.getRootView().findViewById(R.id.btId);
/*check for button */
if(bt!=null) {
/** disable button view */
...
} else {
/** according to #jeroen-bollen remark
* we made assumption that we expected a view
* of type button here in other any case
*/
throw new IllegalArgumentException("Wrong argument: " +
"View passed to method is not a Button type!");
}
}
}
});
EDIT:
In reply to #jeroen-bollen
View.OnClickListener
is Interface definition for a callback to be invoked when a view is clicked.
with method definition
void onClick(View v);
when the view is clicked the View class object makes callback to method onClick() sending as parameter itself, so null view parameter should not occur if it does it's an Assertion Error it could happen for example when View object class was destroyed in meanwhile (for example collected by GC) or method was tampered due to hack
little about instanceof & null
JLS / 15.20.2. Type Comparison Operator instanceof
At run time, the result of the instanceof operator is true
if the value of the RelationalExpression is not null
and the reference could be cast to the ReferenceType
without raising a ClassCastException.
Otherwise the result is false.
three words from the Author
IF U ASK WHY ?
MOSTLY TO AVOID NullPointerException
Little more code will save your time on later bug tracking in your code & reduces the occurrence of abnomalies.
consider following example:
View.OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
btnSend.setClickable(false);
}
});
btnSend.setOnClickListener(listener)
btnCancel.setOnClickListener(listener)
If you need to disable button add this line of code.
Button button = findViewById(R.id.button)
button.setEnabled(false);
And enable button , just add this line
button.setEnabled(true);
Happy coding :D
first in xml make the button as android:clickable="false"
<Button
android:id="#+id/btn_send"
android:clickable="false"/>
then in your code, inside oncreate() method set the button property as
btn.setClickable(true);
then inside the button click change the code into
btn.setClickable(false);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setClickable(true);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnSend.setClickable(false);
}
});
}
You can disable a button from your xml but that won't be dynamic. Best way to disable button dynamically is.
myButton.setEnabled(false);
Just use setEnabled method in Java.
myButton.setEnabled(false);
And in Kotlin
myButton.enabled = false

Categories

Resources