Image set as background of toggle button is stretched android - android

I have a Toggle Button as:
<ToggleButton
android:id="#+id/tv_pmpSwitch"
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:background="#drawable/toggle_view"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
/>
And my toggle_view drawable is :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/ic_list_action"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/ic_grid_action"
android:state_checked="false"/>
</selector>
I don't understand why the image in background is stretched ?? I've tried various size images.

You can simply set in the xml layout file:
android:minWidth="0dp"
android:minHeight="0dp"
The image will not stretch anymore

<ToggleButton
android:id="#+id/tv_pmpSwitch"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_weight="0.1"
android:background="#drawable/toggle_view"
android:textOff=""
android:textOn="" />
Try this on your code and adjust only layout height parameter!
EDIT
The way to get a non stretched image is using a bitmap and not a drawable.
use following as your xml as your background.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<bitmap android:src="#drawable/ic_list_action"
android:gravity="center_vertical|left" />
</item>
<item>
<bitmap android:src="#drawable/ic_grid_action"
android:gravity="center_vertical|left" />
</item>
</layer-list>

After trying out a few things I found what was wrong:
The weightSum was the culprit and the assigned weight was stretching the image.
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pmpSwitch"
android:background="#drawable/ic_toggle_list"
/>
</LinearLayout>
Putting the whole code inside a LL parent did the trick

Related

Android drawable oval with image drawn differently on design view and device

I have an XML drawable that draws a red circle and centres a microphone image in the middle of it. I then have it on top of a relatively layout background. However, the design view layout looks exactly like I want it to, the device layout does not. Where is the discrepancy?
Drawable XML
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:width="125dp"
android:height="125dp"
android:gravity="center"
>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="#FD91B5" />
<!--
<stroke
android:width="10dp"
android:color="#" />
-->
</shape>
</item>
<item
android:gravity="center"
android:width="26dp"
android:height="44dp"
android:drawable="#drawable/kiosk_microphone"
/>
</layer-list>
View XML (Look for the ImageButton)
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/kiosk_bg_default_view">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Close"
android:id="#+id/kiosk_close_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentTop="true" />
<ImageButton
style="#style/Base.Widget.AppCompat.ActionButton"
app:layout_widthPercent="34%"
app:layout_heightPercent="34%"
android:scaleType="fitCenter"
android:src="#drawable/kiosk_record_circle"
android:id="#+id/kiosk_record_button"
app:layout_marginTopPercent="33%"
app:layout_marginLeftPercent="23%"
/>
<LinearLayout
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="5%"
android:orientation="vertical"
android:background="#android:color/darker_gray"
app:layout_widthPercent="25%"
app:layout_heightPercent="20%"
>
<me.grantland.widget.AutofitTextView
android:text="3:45 PM"
android:textSize="45sp"
android:textColor="#B9CFDF"
android:id="#+id/time_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:layout_weight="1"
/>
<me.grantland.widget.AutofitTextView
android:text="Monday, December 15th, 2016"
android:textSize="10sp"
android:textColor="#B9CFDF"
android:id="#+id/date_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:layout_weight="3"
/>
</LinearLayout>
<LinearLayout
app:layout_widthPercent="25%"
app:layout_heightPercent="20%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="60%"
android:orientation="vertical"
android:background="#android:color/darker_gray"
>
<me.grantland.widget.AutofitTextView
android:text="25 deg"
android:textSize="45sp"
android:textColor="#android:color/white"
android:id="#+id/weather_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:maxLines="1"
android:gravity="center"
/>
<me.grantland.widget.AutofitTextView
android:text="Island Park, NY"
android:textSize="10sp"
android:textColor="#android:color/white"
android:id="#+id/location_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:maxLines="1"
/>
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
I want this
But I get this
circularbutton.xml in your drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#FD91B5"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#FD91B9"/>
</shape>
</item>
Edit the color for Pressed State as per your wish.
Add circularbutton.xml to your ImageView
<ImageButton
style="#style/Base.Widget.AppCompat.ActionButton"
app:layout_widthPercent="34%"
app:layout_heightPercent="34%"
android:scaleType="fitXY"
android:src="your_image"
android:background="#drawable/circularbutton"
android:id="#+id/kiosk_record_button"
app:layout_marginTopPercent="33%"
app:layout_marginLeftPercent="23%"
/>
If scaleType doesn't work, you can apply padding to make the icon smaller.
Doing something like :
android:width="125dp"
android:height="125dp"
And:
android:width="26dp"
android:height="44dp"
And on the button:
android:layout_height="60dp"
Is destined to backfire on you, because it will not look the same on different devices. Especially if you try to combine a few different items together each of whom has some random values like this. This is due to a significant discrepancy in screen sizes and screen densities among Android devices. You can make it work on one screen properly, and yet it can look completely different on other screens.
For this kind of thing, since you basically need an icon, you can use a drawable or an SVG file.
EDIT
As an alternative solution, I thought of using this SVG as the src of your imageButton and the button should have the custom circular shape like in this answer.

Ripple effect not working in relative layout with image

Ripple effect is not working with the below code! Using the below relative layout as item in recycler view. Building with Xamarin platform.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:paddingBottom="1dp"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<ImageView
android:id="#+id/bugImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/grey_small"
android:clickable="false"/>
<TextView
android:id="#+id/textBugName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/bugImage"
android:layout_alignTop="#id/bugImage"
android:layout_alignBottom="#id/bugImage"
android:gravity="left"
android:text="Klebsiella pneumoniae"
android:textColor="#ffffffff"
android:textSize="30dp"
android:layout_marginRight="145dp"
android:padding="10dp"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="25"
android:clickable="false"/>
<TextView
android:id="#+id/textBugCount"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_alignRight="#id/bugImage"
android:layout_alignTop="#id/bugImage"
android:layout_alignBottom="#id/bugImage"
android:gravity="right"
android:text="234"
android:textColor="#ffffffff"
android:textSize="30dp"
android:padding="10dp"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="25"
android:clickable="false"/>
</RelativeLayout>
I have already tried this android:foreground="?android:attr/selectableItemBackground" and it didn't work!
How to fix this? I tested it on api 22.
Not sure what you're trying to accomplish with this (i.e. setting foreground):
android:foreground="?android:attr/selectableItemBackground"
Try this:
android:background="?android:attr/selectableItemBackground"
though I use a custom drawable to have control over coloring, as shown here:
Set the background on your RelativeLayout android:background="#drawable/mybackground"
in res/drawable, mybackground.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/disabledBackground" />
<item android:state_activated="true" android:drawable="#color/colorAccent" />
<item android:drawable="#color/listBackground" />
</selector>
in res/drawable-v21, mybackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="#color/disabledBackground" xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_pressed="true" android:drawable="#color/disabledBackground" />
<item android:state_activated="true" android:drawable="#color/colorAccent" />
<item android:drawable="#color/listBackground" />
</selector>
</item>
</ripple>
Here you've specified the ripple effect, supported on platforms v21+. You can adjust the colors and states to your needs, disabledBackground, colorAccent, listBackground are just examples of named color attributes
With a RelativeLayout as a root view you should set the native ripple with background="" instead of foreground=""
Or you may try to implement a cardview which will properly respond to foreground=""
android:foreground="?android:attr/selectableItemBackground"
works only for frame layout.
use
android:background="?android:attr/selectableItemBackground"
or it is better to use:
android:background="?attr/selectableItemBackground"
if you are using compat library
Just add android:onClick="add on click listener here" and android:background="?android:attr/selectableItemBackground" to your layout where you need a ripple effect. It worked for me.

Reduce Check box size in android?

i have tried out the suggestions given but unable to reduce the size...on reduction of size in graphical layout,the border lines of the checkbox disappear...iam new to android...any help will be appreciated
thanks
here is code...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3CAE5"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp" >
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000" />
<TextView
android:id="#+id/txt_fName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
height and width change removes the border lines...check box appears in a list view that is populated dynamically...
the below code works for changing the checkbox image but now iam unable to check the box ie the tick mark does not appear on checking the box???
The text determines the size of the check box. try this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
....... />
use custom image for check box
and use image of your size
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/checked" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/unchecked" />
<item android:state_checked="false"
android:drawable="#drawable/unchecked" />
<item android:state_checked="true"
android:drawable="#drawable/checked" />
</selector>

Android RelativeLayout Drawable Selector doesn't work

I have troubles making my RelativeLayout change background drawable on click. I made it clickable and focusable but this make nothing change. The background remains static to enables drawable. Thanks in advance.
<RelativeLayout
android:id="#+id/bttn_type_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="#drawable/day_bttn_bg_red"
android:clickable="true"
android:focusable="true"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<ImageView
android:id="#+id/bttn_type_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:contentDescription="#string/app_name"
android:src="#drawable/relationship" />
<TextView
android:id="#+id/bttn_type_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bttn_type_iv"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:text="#string/type_bttn_txt"
android:textSize="#dimen/data_fill_bttn_txt_size"
android:textStyle="bold" />
</RelativeLayout>
Here is the selector xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bttn_red_p_bg" android:state_enabled="false" android:state_pressed="true"></item>
<item android:drawable="#drawable/bttn_red_a_bg" android:state_enabled="true"></item>
</selector>

change color of relative layout, and also its child elements

I have this relative layout:
<RelativeLayout
android:id="#+id/relativeLayout5"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_below="#+id/relativeLayout4"
android:background="#drawable/pigs_banner_selector"
android:clickable="true"
>
<ImageView
android:id="#+id/imageView9"
android:layout_width="220dp"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="-10dp"
android:src="#drawable/banner_games_pighunt_hill" />
<TextView
android:id="#+id/textView3"
android:fontFamily="sans-serif-condensed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gluecksschweinjagd"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white"
android:textSize="31sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:textStyle="bold" />
</RelativeLayout>
and the selector of the RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#color/white"/>
<item android:state_pressed="true" android:drawable="#color/highlight_pigs_banner" />
<item android:drawable="#color/pigs_banner"/>
</selector>
The behavior is this: The relativeLayout changes it´s color as intended. But I want the Child elements (the ImageView) to also get the color of the selector (#color/highlight_pigs_banner).
EDIT:
The color highlight_pigs_banner basically puts alpha value on the color. How can I achieve this also on the imageview (child element) by still clicking only the RelativeLayout
I tried adding android:clickable="true" to the child elements, which didnt work, and android:addStatesFromChildren="true" to the relativeLayout which gave me an stackoverflow exception. Anyone knows how to achieve this? Thanks.
You should use Selector for ImageView and TextView as well.
<TextView android:id="#+id/textView3"
android:fontFamily="sans-serif-condensed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#drawable/text_selector" />
text_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false"
android:state_pressed="false"
android:color="#color/normal"/>
<item android:state_pressed="true"
android:color="#drawable/highlight"/>
<item android:state_selected="true"
android:state_pressed="false"
android:color="#drawable/highlight"/>
</selector>
colors.xml
<drawable name="highlight">#ff590d</drawable>

Categories

Resources