Android custom button sensitivity - android-layout

I have created a custom button like this :
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false">
<shape>
<gradient
android:endColor="#color/title_dark_red"
android:startColor="#color/title_red"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/red_stroke" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" android:state_pressed="true">
<shape>
<gradient
android:endColor="#color/title_pressed_red"
android:startColor="#color/title_pressed_red"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/red_stroke" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="false" android:state_pressed="true">
<shape>
<gradient
android:endColor="#color/title_pressed_red"
android:startColor="#color/title_pressed_red"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/red_stroke" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
The problem is sensitivity of the button is very bad, not like the regular buttons. The buttons are not too small in the screen, so it's easy to touch.
What can be the problem ?
P.S I'm using Button.OnClickListener() to set click handler.
Button is in relative layout structure :
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
>
<TextView
android:id="#+id/titlePlace"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/expand_bg"
android:gravity="center_vertical"
android:text=""
android:textColor="#color/titletextcolor"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBaseline="#+id/showButton"
android:layout_alignBottom="#+id/showButton"
android:layout_alignParentLeft="true"
android:background="#drawable/custom_button_back"
android:layout_marginLeft="5dip"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Places"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>

It's a hardware related question. Software just takes on click event sent by hardware and checks whether it's in view's bounds.
Other potential issues:
inaccurate touch screen
view was animated and moved to different position
view is too small or finger too big ;)

Related

How to do more than one square and to align them in rows and columns

I have the following square shape and have managed to align it by using the marginBottom and marginLeft etc but how would I create more than one of the same shape? I tried something like this but only one is showing at the moment....
This is the code for my box shape:
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/colorPrimary" />
<corners android:radius="20dp"/>
</shape>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/colorPrimary" />
<corners android:radius="20dp"/>
</shape>
</item>
This is my main activity code:
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/box"
android:text="Write here"
android:gravity="center"
android:layout_marginBottom="200dp"
android:layout_marginRight="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/box"
android:text="Write here"
android:gravity="center"
android:layout_marginBottom="200dp"
android:layout_marginRight="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="172dp"
android:layout_height="83dp"
android:layout_marginRight="200dp"
android:layout_marginBottom="200dp"
android:background="#drawable/box"
android:gravity="center"
android:text="Write here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.824" />
You can get multiple boxes like what you have by duplicating it just assign them and put them into position.

The spinner is hidden by the background of the liner layout

I want to display the spinner in a rectangle with soild fill color around it. My code is given below
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="113dp"
android:layout_margin="10dp"
android:layout_marginStart="9dp"
android:layout_marginLeft="9dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:background="#drawable/rectangle_shape"
android:orientation="vertical"
android:padding="10dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="#+id/spinmain"
android:layout_width="352dp"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="60dp"
android:layout_marginBottom="416dp"
android:dropDownVerticalOffset="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/view7" />
</LinearLayout>
and rectangle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/re/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
But the problem is that the spinner is hidden by the rectangle linear layout
How can I bring the spinner in front of it.Please suggest
Try to change android:layout_height="113dp" from 113 to match_parent

How to set background color of Android ListView entries/items?

I have a ListView with entries that have a lighter background color than the application's layout, and I don't know why.This is a snapshot with red arrows indicating the difference in color: Main_Activity
I have no idea where to start on this.
This is the manifest.xml file that takes up the theme, AppTheme:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gtfp.workingmemory"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<!-- Start an Alarm When the Device Boots if past due -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".appController"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".editToDoItem"
android:theme="#android:style/Theme.Holo.Dialog" >
</activity>
<receiver android:name=".ToDoAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".AlarmActivity"
android:theme="#android:style/Theme.Holo" >
</activity>
<activity
android:name=".SettingsActivity"
android:theme="#android:style/Theme.Holo" >
</activity>
</application>
</manifest>
That theme in fact is Theme.Holo in the resources file:
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<!-- <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> -->
<style name="AppBaseTheme" parent="android:Theme.Holo">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
This is the main layout for the application you'll see the ListView here:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="${relativePackage}.${activityClass}">
<ListView
android:id="#+id/lvToDos"
android:layout_width="206dp"
android:layout_height="380dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:smoothScrollbar="false"
android:layout_above="#+id/itemEntryView"
android:layout_alignParentTop="true"
>
</ListView>
<LinearLayout
android:id="#+id/itemEntryView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/btnNewToDo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="editToDoItem"
android:text="#string/new_item"
android:textSize="#dimen/smallTextSize"/>
</LinearLayout>
</RelativeLayout>
Each row in the ListView is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center"
android:id="#+id/container"
android:layout_height="?android:attr/listPreferredItemHeight"
>
<ImageView
android:id="#+id/priorityImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="center"
android:focusable="false"
android:clickable="true"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:src="#drawable/low"
/>
<TextView
android:id="#+id/itemText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAlignment="center"
android:textStyle="bold"
/>
<TextView
android:id="#+id/itemDueDate"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_marginRight="5sp"
android:scaleType="center"
android:textSize="12sp"
android:focusable="false"
android:textStyle="bold"
android:gravity="center"
/>
</LinearLayout>
It seems that you want to change the background color of your list view if this is the case then you can easily do this in your custom listview base adapter you can do it in your base adapter's getview Method.
here is the sample code
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = new TextView(ListHighlightTestActivity.this);
convertView.setPadding(10, 10, 10, 10);
//here I am changing the color of text in textview
((TextView)convertView).setTextColor(Color.WHITE);
}
convertView.setBackgroundColor((position == curSelected) ?
Color.argb(0x80, 0x20, 0xa0, 0x40) : Color.argb(0, 0, 0, 0));
((TextView)convertView).setText((String)getItem(position));
return convertView;
}
or alternatively if you know the idea of using selector then you can easily achieve it through selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#color/BackgroundColor" />
<item android:drawable="#color/transparent" />
</selector>
I am sure your problem would get solved in these two above mentioned solutions. Use any of them, which ever is easy for you.
Set app color in list view
<ListView
android:id="#+id/lvToDos"
android:layout_width="206dp"
android:layout_height="380dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:smoothScrollbar="false"
android:background="#color/app_color"
android:layout_above="#+id/itemEntryView"
android:layout_alignParentTop="true"
>
</ListView>
I found the problem.
In the getView, I'm producing 'rounded corners' for every item in the ListView by setting their resource background to a customized drawable:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
:
:
:
:
convertView.setBackgroundResource(R.drawable.rounded_corner);
}
Well, in the drawable, I found there was a solid colour being drawn in there. I commented it out, and now I get what I want:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- <solid android:color="#433E41"/> -->
<corners android:radius="5dp" />
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
<stroke android:width="3dp" android:color="#E6E6E6"/>
</shape>`enter code here`
I just happened upon this while investigating another matter (I hate porgramming). Appreciate your efforts regardless.

Round corner button: Where am I wrong

I want to create round corner button. This is what I tried:
I have defined the button's background drawable button_bg.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/grey">
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/dark_grey"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_enabled="true"
android:drawable="#color/blue"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
</selector>
Then, I apply it to my button in layout file:
<Button
android:text="#string/press_me"
android:layout_margin="12dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/button_bg"
/>
But my button is still showing sharp corner instead of the round one, why??
(Please don't just throw me a link about how to make round corner, my above code followed those tutorials, I just want to know where am I wrong. Thanks)
remove android:drawable from the item node have it like this.
<item android:state_pressed="true"
android:state_enabled="true">
EDIT : (How can I specify the color from colors.xml for item then?)
Create a stateful color drawable for your button text color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
and then set this as your text color for your button .
android:textColor="#drawable/button_text_color"
Remove all android:drawable from item as below then it will work.
When you add the attribute android:drawable in item then this drawable overlapped your created shape. That's why you can't see your rounded shape.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true" android:state_pressed="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
</selector>

How can I change colors in my StateListDrawable?

I have the following selector for a button (with 2 states, regular and pressed):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#3498DB" />
<stroke
android:width="1dp"
android:color="#2980B9" />
<corners
android:radius="0dp" />
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#2980B9" />
<stroke
android:width="1dp"
android:color="#2980B9" />
<corners
android:radius="0dp" />
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp" />
</shape>
</item>
</selector>
My button has the following which specifies the background as the above selector:
<Button
android:id="#+id/button_LaunchShiftsGame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ShiftsMode"
android:background="#drawable/selector_Button"
style="#style/Button_Text" />
I need to access and change the colors for both states from code when the Activity loads.
How can I do this?
StateListDrawable gradientDrawable = (StateListDrawable) inflatedView.getBackground();
DrawableContainerState drawableContainerState = (DrawableContainerState) gradientDrawable.getConstantState();
Drawable[] children = drawableContainerState.getChildren();
LayerDrawable selectedItem = (LayerDrawable) children[0];
LayerDrawable unselectedItem = (LayerDrawable) children[1];
GradientDrawable selectedDrawable = (GradientDrawable) selectedItem.getDrawable(0);
GradientDrawable unselectedDrawable = (GradientDrawable) unselectedItem.getDrawable(0);
selectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
unselectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
I used this to change the stroke, it can be helpfull.
This is the Kotlin version of the #Borja's answer with drawable resource
val drawableRes: Drawable? = ResourcesCompat.getDrawable(mContext.resources, R.drawable.drawable_res_id, null)
val drawableContainerState: DrawableContainer.DrawableContainerState = drawableRes?.constantState as DrawableContainer.DrawableContainerState
val children: Array<Drawable> = drawableContainerState.children
val selectedItem: GradientDrawable = children[0] as GradientDrawable
val unselectedItem: GradientDrawable = children[1] as GradientDrawable
selectedItem.setStroke(2, colorState)
unselectedItem.setStroke(2, colorState)
and yes! Many thanks to #Borja. I found this working well as in 2022.
I have below selector round_circle_blue.xml shape xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size android:width="#dimen/_50sdp" android:height="#dimen/_50sdp" />
<solid android:color="#color/color_blue" />
<stroke android:width="#dimen/_2sdp" android:color="#color/white" />
</shape>
</item>
</selector>
And I have set it as background in text view as below
<TextView
android:id="#+id/tvActivityicon"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_40sdp"
android:textColor="#color/white"
android:gravity="center"
android:text="A"
android:textSize="#dimen/_14sdp"
android:background="#drawable/round_circle_blue" />
and in kotlin file i have use below code to change the color.
val gradientDrawable = tvActivityicon.background as StateListDrawable
val drawableContainerState = gradientDrawable.constantState as DrawableContainer.DrawableContainerState
val children = drawableContainerState!!.children
val selectedItem = children[0] as GradientDrawable
selectedItem.setColor(Color.parseColor("#FD00DF"))

Resources