Android studio button disable

Disable an ImageButton

This looks easy, but I’m not able to disable an ImageButton . It continues to receive click events, and its appearance don’t change like a standard Button would.

There are some similar questions on SO, but they don’t help me.

Even with a very simple layout like this :

The button is still enabled and I can click it.

What’s strange is that if I change the ImageButton to a simple Button , then it works as expected. The button becomes disabled and unclickable. I don’t understand. Does anyone have an idea?

7 Answers 7

Here’s the code I use to disable an ImageButton and make it look grayed out:

Simply call setImageButtonEnabled() ; the only downside is you need the image’s resource ID in here because it’s not possible to revert a transformed icon back into the original.

ImageButton has different inheritance chain meaning it does not extend Button :

ImageButton ImageView View

It continues to receive click events

Here is what happens when you set a click listener for the View :

So if you set a listener the android:clickable=»false» changes to android:clickable=»true» .

and its appearance don’t change like a standard Button would

You should supply a drawable state list to the view so it could set an appropriate image based on android:enabled . Do you have this? Or you have the only image for your button?

EDIT: You can find info on StateListDrawable here. android:state_enabled is what you need to use in the list in order to tell the OS what image to use for that state.

EDIT2: Since you really need to add a listener you can make a check inside of the listener if (!isEnabled()) < return; >else < /* process the event */ >.

Источник

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?

13 Answers 13

Did you try this?

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.

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 .

Yes it can be disabled in XML just using:

You just write a single line of code in your activity

When you want to enable the same button just write

In Java, once you have the reference of the button:

Читайте также:  Кнопки внизу экрана андроид поменять расположение

To enable/disable the button, you can use either:

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:

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.

If you or anyone having a similar issue, what works for me is setting the background drawable again. Works on any API Level.

In Kotlin, if you refer the Button View with id then, enable/disable button as like

layout.xml

activity.kt

With Kotlin you can do,

first in xml make the button as android:clickable=»false»

then in your code, inside oncreate() method set the button property as

then inside the button click change the code into

WRONG WAY IN LISTENER TO USE VARIABLE INSTEAD OF PARAMETER.

RIGHT WAY:

EDIT: In reply to @jeroen-bollen

is Interface definition for a callback to be invoked when a view is clicked.

with method definition

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:

Источник

Buttons

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:

  • With text, using the Button class:
  • With an icon, using the ImageButton class:
  • With text and an icon, using the Button class with the android:drawableLeft attribute:

Key classes are the following:

Responding to Click Events

When the user clicks a button, the Button object receives an on-click event.

To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

For example, here’s a layout with a button using android:onClick :

Within the Activity that hosts this layout, the following method handles the click event:

Читайте также:  Айфоновский рингтон для андроид

Kotlin

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

Using an OnClickListener

You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.

To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) . For example:

Kotlin

Styling Your Button

The appearance of your button (background image and font) may vary from one device to another, because devices by different manufacturers often have different default styles for input controls.

You can control exactly how your controls are styled using a theme that you apply to your entire application. For instance, to ensure that all devices running Android 4.0 and higher use the Holo theme in your app, declare android:theme=»@android:style/Theme.Holo» in your manifest’s element. Also read the blog post, Holo Everywhere for information about using the Holo theme while supporting older devices.

To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource. Alternatively, you can apply a style for the button, which works in a manner similar to HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, see Styles and Themes.

Borderless button

One design that can be useful is a «borderless» button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.

To create a borderless button, apply the borderlessButtonStyle style to the button. For example:

Custom background

If you want to truly redefine the appearance of your button, you can specify a custom background. Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button’s current state.

You can define the state list in an XML file that defines three different images or colors to use for the different button states.

To create a state list drawable for your button background:

    Create three bitmaps for the button background that represent the default, pressed, and focused button states.

To ensure that your images fit buttons of various sizes, create the bitmaps as Nine-patch bitmaps.

Источник

How to disable / enable dialog negative positive buttons?

Please look at the custom dialog below. I have an edittext field on the dialog and if the text field is empty I would like to disable the positiveButton . I can get a charListener for the text field but I am not sure how I am going to set the positivebutton to disable or enable from that listener? What is the reference for the positive and negative buttons?

7 Answers 7

Edit for complete solution.

Below are edited history, which can be refer as some more details

Here is a sample code, try this

For negative button

For buttons id : Reference alert_dialog.xml

Читайте также:  Android прошивка для meizu

Edited :

And the setOnShowListener since level 8 API (FroYo), does the same,

Edited

None of these answers really solve the problem.

I accomplish this using a custom layout with an EditText in it and a TextWatcher on that view.

Here is complete code to enable and disable positive button of dialog:

You can write a listener to the edit text box, and try to enable or disable buttons. This is a sample code for xamarin.

For delete record from database list view using view holder you used this code in your getview() method..

This dialogFragment will do the job for you. Note that the dialog will remain open after screen rotation preserving any text that the user has already typed in. If you don’t want that to happen you need to dismiss the fragment in your activity’s onStop. The newInstance method signature can be changed to whatever you need.

Add implements to your activity (any type of Activity is fine):

Create the diaglogFragment in your activity like this:

Handle the result in your activity like this:

You need to create the resource identifier so add this resource under somewhere under res/values

Источник

Disable back button in android

How to disable back button in android while logging out the application?

17 Answers 17

Override the onBackPressed method and do nothing if you meant to handle the back button on the device.

If looking for a higher api level 2.0 and above this will work great

If looking for android api level upto 1.6.

Write above code in your Activity to prevent back button pressed

You can do this simple way Don’t call super.onBackPressed()

Note:- Don’t do this unless and until you have strong reason to do it.

Simply override the onBackPressed() method.

Just override the onBackPressed() method and no need to call the super class of onBackPressed method or others..

Or pass your current activity into the onBackPressed() method.

Replace your require activity name to myActivity.

if you are using fragment then first of all call the callParentMethod() method

then call the empty method

If you want to make sure your android client application is logged out from some server before your Activity gets killed —> log out with a service on its own thread (that’s what you’re supposed to do anyway).

Disabling the back button won’t solve anything for you. You’ll still have the same problem when the user receives a phone call for instance. When a phone call is received, your activity has about as much chances of getting killed before it gets a reliable answer back from the network.

That’s why you should let a service wait on its own thread for the answer from the network, and then make it try again if it doesn’t succeed. The android service is not only much less likely to get killed before it gets an answer back, but should it really get killed before finishing the job, it can always get revived by AlarmManager to try again.

Источник

Оцените статью