I realize this has been asked multiple times, but my image is changing size when it is pressed (which is should be the same size, but a different image). I've previously tried setting the size of the button in java, but it didn't work either. Here is my code.
XML Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/testbuttonpressed" /> <!-- pressed -->
<item android:drawable="#drawable/testbutton" /> <!-- default -->
</selector>
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backtest1">
<Button
android:id="#+id/start_button"
android:layout_width="225px"
android:layout_height="55px"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="#string/start"
android:background="#drawable/buttonsxml"
/>
</LinearLayout>
It turns out my pictures were a pixel off...
Related
I need to highlight widgets as per the following design, In below design all three widgets are highlighted in red when the cursor reaches the position
I have written the following code to achieve above:
left_strip_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/focus_color"/>
<item android:state_focused="true" android:drawable="#color/focus_color" />
<item android:drawable="#color/menu_list_bg" />
</selector>
leftmenu_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/focus_color"/> <!-- pressed -->
<item android:state_focused="true" android:color="#color/focus_color"/> <!-- focused -->
<item android:color="#color/white"/> <!-- default -->
</selector>
leftmenu_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.leanback.widget.NonOverlappingLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<View
android:layout_width="4dp"
android:layout_height="17dp"
android:layout_marginEnd="22dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#drawable/left_strip_color_selector"></View>
<ImageView
android:id="#+id/left_menu_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="15dp"/>
<com.zeenews.tv.base.BaseTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/left_menu_text"
android:lines="1"
android:textSize="14sp"
android:textColor="#color/leftmenu_text_selector"
android:textAllCaps="true"/>
</androidx.leanback.widget.NonOverlappingLinearLayout>
Note: For now, I have implemented the above code for red strip and menu text only
With the above code, I am getting below effects when I move the cursor from one menu option to another (only strip is getting colored and text is not getting colored.)
My Question: Am I doing something wrong? Can two widgets receive focus at the same time? Can I achieve these effects using any other standard coding practice?
Here is my button image file from my drawable folder called ooo.png:
And that's how it looks like on the device:
As you can see the width height proportion changed. It's a bit wider than the original.
I need it to be looking the same though.
I don't understand what causes this stretching. Here is my xml-file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="#style/styleBtns"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
My styleBtns-part of the styles.xml:
<style name="styleBtns" >
<item name="android:background">#drawable/btns_abc</item>
</style>
And in my btns_abc I set the background to my image resource. Currently state pressed and the normal state are the same, I want to change that as soon as it works properly:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/ooo" android:state_pressed="true"></item>
<item android:drawable="#drawable/ooo"></item>
</selector>
Thanks !
I don't know the exact reason but for a solution if u don't have any text for button ,use ImageView will show the same drawable u have added.
This is what is happening:
This is the source image:
This is part of the xml layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_headers"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/etNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:completionThreshold="2"
android:dropDownHeight="wrap_content"
android:dropDownWidth="wrap_content"
android:hint="#string/et_newitemhint"
android:inputType="text|textCapSentences"
android:layout_centerVertical="true"
style="#style/DialogEditText"
android:layout_marginRight="10dp"
android:singleLine="true" />
<Button
android:id="#+id/btnAddNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/btn_add"
android:layout_centerVertical="true" />
</RelativeLayout>
This is the Selector xml for the button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/btn_add_pressed"/>
<item android:state_pressed="true"
android:drawable="#drawable/btn_add_pressed"/>
<item android:drawable="#drawable/btn_add_normal"/>
</selector>
Why is this button horizontally resizing in 4.x.x? The source graphic did use to be a 9-patch, but it did exactly the same thing, so I changed it to a normal png, with no result.
The odd thing is is that this is the only graphic which exhibits this behaviour. I have it in an actionbar somewhere: same thing; I have it in a dialog too: same thing.
My 2.2.2 and 2.3.6 test devices show the button as it should be, but any of my test devices of 4.x.x show the elongated button.
First, make these:
res/drawable/button_add_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/btn_add_normal"
android:tileMode="disabled"
android:gravity="top" >
</bitmap>
res/drawable/button_add_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/btn_add_pressed"
android:tileMode="disabled"
android:gravity="top" >
</bitmap>
Then use this as selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/button_add_pressed"/>
<item android:state_pressed="true"
android:drawable="#drawable/button_add_pressed"/>
<item android:drawable="#drawable/button_add_normal"/>
</selector>
I didn't manage to reproduce this on my devices. However, I still have a solution: you can use exact sizes like this:
android:layout_width="50dp"
android:layout_height="50dp"
This way the image should look the same on every device.
i want to make image button, so when it pressed i will replase the not-pressed image to pressed image of the button, and then go to other activity. i have two different images in drawable.
i have two xml files:
first one- load the main activity: background and image button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<ImageButton
android:id="#+id/b1_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/s1"
android:scaleType="centerCrop"
android:background="#color/trans"
android:src="#drawable/b1_l" />
</RelativeLayout>
second one:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/image_not_pressed" />
<item android:state_pressed="true"
android:drawable="#drawable/image_pressed"" />
</selector>
what is the code that i need to write in addition to my xml?
or can i pass the xml and write it only as code...?
thanks!
you need to usually have drawable for pressed and default(unpressed) state. You can have the xml file like this(below) for that. You need to put this xml file in the drawable folder and then use as src for ImageButton or background source in case of Button.
Lets name it is mybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/your image name" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/your image name" />
<item android:drawable="#drawable/your image name" />
</selector>
You layout file for main activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<ImageButton
android:id="#+id/b1_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/s1"
android:scaleType="centerCrop"
android:background="#color/trans"
android:src="#drawable/mybutton" />
</RelativeLayout>
You migth want do check out android developer guide on this
http://developer.android.com/guide/topics/ui/controls/button.html
I have three images in my drawable folder, and an XML Image Button code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">\
<item android:state_pressed="true"
android:drawable="#drawable/happycarrot" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/largerunicornbutton2" /> <!-- focused -->
<item android:drawable="#drawable/largerunicornbutton" /> <!-- default -->
</selector>
In my layout folder, I have my main XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:src="#drawable/button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
></ImageView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
When I run it in the emualtor I only get the first image. It doesn't do anything else. Is there anything I can do to solve this?
Using a Linearlayout instead of a relative one fixes the issue.