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
Related
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++;
}
I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});
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.