Problems with setEnabled in imagebutton - android

I have ImageButton, putting setEnabled(false) that is so, but only in the emulator / tablet
But the button does not change anything when I put setEnabled(false) on some devices
Which can be? I would have all the appliances are as in the first image (I've tried using the android: clickable, but without success)
this is my xml:
<LinearLayout
android:id="#+id/MenuBotoes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#fc6719">
<ImageButton
android:id="#+id/btnMarcacao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/draw"/>
<ImageButton
android:id="#+id/btnZoom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/zoom"/>
<ImageButton
android:id="#+id/btnBorracha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/eraser"/>
</LinearLayout>

Setting the button as enabled or disabled will not necessarily change what it looks like, especially in the case of an imagebutton. You may need to make custom selectors/drawables/backgrounds for your images if you want to have different appearances based on state. You can do this by either changing the src or the background of your button to a selector drawable.
See selectors here: https://developer.android.com/guide/topics/resources/drawable-resource.html
And then see here: https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

Related

Buttons with ButtonBarButtonStyle do not highlight on touch

There are plenty of explanation about how to get buttons to display like standard dialogue buttons - which I follow. I have this in my xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerVertical"
style="?android:attr/buttonBarStyle"
android:showDividers="middle"
android:measureWithLargestChild="true">
<Button
android:id="#+id/button_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:background="#drawable/dialog_button"
android:text="#string/button_cancel" />
<Button
android:id="#+id/button_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
style="?android:attr/buttonBarButtonStyle"
android:background="#drawable/dialog_button"
android:text="#string/button_ok" />
</LinearLayout>
This works perfectly well and produces the visual design I need. However when I touch the buttons, they don't highlight. This is rather annoying, as everything else is exactly as it should be. It seems that the buttonBarButtonStyle doesn't include the state drawables for different states of the button. Is there an easy way to get the highlight back?
Of course, I can just define my own state drawable defining different colours, but I want to use theme colours instead.
You need to apply the background:
android:background="?android:attr/selectableItemBackground"
to achieve the default "button bar" style and behavior.

Android: EditText displayed with black background

I have a row with two EditTexts and two ImageButtons. In the preview of android studio everything looks fine and the EditTexts have the default style. But if I display it on my tablet the background is black.
How it should look like:
How it looks like on the tablet:
Other EditText-Boxes are displayed correctly.
Does anyone know why the these look different and how I can fix it?
Here the XML-Layout of this line:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<EditText
android:id="#+id/first_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<EditText
android:id="#+id/second_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.4"
/>
<ImageButton
android:id="#+id/first_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/move"
android:background="#drawable/first_button_background"
android:onClick="onFirstButtonClicked"
/>
<ImageButton
android:id="#+id/second_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/trash"
android:background="#drawable/second_button_background"
android:onClick="onSecondButtonClicked"
/>
</LinearLayout>
EDIT:
I tried changing the background color to white but then the EditTexts lose their whole style so also the underlining will not be displayed anymore.
this's because the editext default background change based on device theme go for an edittext with custom background and in a simple way include android:background="#ffffff" for your editexts
The problem might be occurring due to the imageButtons but You can change the background color of the edittext like this:
android:background="#FFFFFF"
May be your tab does not support the selected theme, try to change background color of your edittext using:
android:background="#FFFFFF"
if problem does not solve, then you can change background of edittext programmatically like this:
youeEditText.setBackgroundColor(Color.parseColor("#ffffff"));

Clickable Button (or any View) inside EditText in android

I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigo for this answer. But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-
In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?
Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.
You don't need any drawable and listening to the click event of the android buttons is not a question anymore.
Its absolutely correct
Use this , It worked for me -
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/id_search_EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingRight="40dp"
android:hint="Enter your feelings and connect" />
<ImageButton android:id="#+id/id_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/id_search_EditText"
android:layout_alignBottom="#+id/id_search_EditText"
android:layout_alignRight="#+id/id_search_EditText"
android:background="#drawable/ic_launcher"/>
</RelativeLayout>
I think you try to search set click event for compound drawable. You can find some solutions here
handling-click-events-on-a-drawable-within-an-edittext
there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.
The only possible solution is to use a RelativeLayout that allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.
Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.html maybe it can helps you :)
Use this this code may work.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/REFReLayTellFriend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/editext_rounded"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="start"
android:hint="Search"
android:singleLine="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtSearch"
android:background="#drawable/ic_action_content_filter_list"/>
</RelativeLayout>
</LinearLayout>

How to remove button background color

I searched a lot but I didn't find how to remove the background color from the button which is appearing on the right and left side of button. Can anybody help?
My screen looks like
No matter what I try I am not able to remove the black portion.
Code:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_base"
android:text="#string/base"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
<Button
android:id="#+id/btn_care"
android:text="#string/care"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_daily_prize"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/daily_prize" />
<Button
android:id="#+id/btn_winner"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/winner" />
</LinearLayout>
</LinearLayout>
#Drawable/selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="#drawable/pressed"> </item>
<item android:state_focused="true"
android:drawable="#drawable/focused"/>
<item android:drawable="#drawable/change"></item>
</selector>
this is the image used with name change.9.png
according to your scenario i can surely say that either you are not using a 9-patch image (an image with extension like .9.png ) or the 1 pixel borders of 9-patch at left and top are not drawn in correct manner. thats why the edges and the side border shade get expanded with the button with long width. either you should show what 9-patch button background you have used or try some correct 9-patch and check results for that.
Why don't you use some other control element like TextView instead of buttons? I just saw that TextView has onClickListner and so you can use it as sort-of button, though I have not done it; button is meant to aid you defining your layout, but as this seems to only be a problem for you, just do not use it).
By the way I seriously recommend you to use android styles, as you copy-paste a lot of attributes. If you use Eclipse for development, open your layout xml, select the item you want to extract the style of, press ctrl + 1 and then select extract style. That way you should avoid copy-pasting all these style attributes.
Try removing the android:textColor attribute. These can be misleading and sometimes alter the colour of more than just the text. If the text is supposed to be black then you don't need it.

Android: Dialog background on emulator and my phone are different color

This is the dialog on the emulator:
And this is on my phone:
I tried setting background to my layout but then the title space and bottom space would still be this white color. How do I "repair" it?
If it matters, the second picture is from MIUI ROM.
//EDIT:
Here is my layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Spinner
android:id="#+id/spinner_minutes"
android:layout_width="170dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:entries="#array/timer_values"/>
<TextView
android:id="#+id/text_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone"/>
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dip">
<Button
android:id="#+id/button_set"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Set"
android:layout_marginLeft="10dip"
android:layout_marginBottom="5dip" />
<Button
android:id="#+id/button_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginRight="10dip"
android:layout_marginBottom="5dip" />
</LinearLayout>
<LinearLayout
android:id="#+id/button_holder2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_timer"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dip"
android:visibility="gone">
<Button
android:id="#+id/button_close"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Close"/>
<Button
android:id="#+id/button_cancel2"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</RelativeLayout>
I am creating the dialog this way:
about_dlg=new Dialog(SoundRelaxerActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.about_dialog);
about_dlg.show();
This call:
about_dlg=new Dialog(SoundRelaxerActivity.this);
will use the system default theme. Every hardware manufacture develops their own custom system default themes. For example, the default Button might look different on Motorola Blur than it does on HTC Sense. If you want to build a dialog, with a custom theme, use this call instead:
about_dlg = new Dialog(context, android.R.style.Theme);
BUT android.R.style.Theme might not be what you want. Look at http://developer.android.com/reference/android/R.style.html
and check out any of the values defined as Theme_
AND A HEADS UP:
This might not work out of the box. You are going to have to modify your layout xml file to have the appropriate styles relevant to the theme you choose. For example, Android.R.style.Theme defaults to a solid black background.
And lastly,
this might not be worth the effort. While Android Fragmentation is a pain in the butt, sometimes we have to embrace it. The user is used to the default settings of the device-- for example if every dialogue on their phone is white but yours is black, they might be jarred by this. That is a weak example, but hopefully you get the point.
Make sure you are setting the background of the top overall layout (in this case the relative layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#c0c0c0">
The emulator is just using a different default color than your phone. Change #c0c0c0 to whatever color you want
I know on Android 4 you can ask for the default Holo theme for your application which eliminates this possibility as Holo HAS to be present on an Android 4 system. Prior to that, all you've got is the default theme which could be anything at all.
The question is, why is your text white on white on MIUI? Are you setting that explicitly? If you aren't, then I would consider that you're seeing a bug in the theme rather than your app because if the theme author has made these dialogs white then the default text colour should surely be dark or else every app would suffer the same problem.

Categories

Resources