Change <Button> color onClick? - java

<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/btnAdd"
android:background="#ff6347"
android:textColor="#f8f8f8"
android:layout_marginTop="36dp"
android:clickable="true"
android:layout_below="#+id/txtAddress"
android:layout_centerHorizontal="true"
android:focusable="true" />
This is my XML Button element and it's fine and dandy...but I want it to be known when it's pressed. How can I go about making it have different styling when pressed vs when not pressed?

You can use a selector and use a drawables for different states.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You can also use OnTouchListener and on ACTION_DOWN and ACTION_UP set the color to the buttons

You want to use a selector as the background for the button. Basically you can define the the different shape you want for each state of the button and the shape consists of color, corner radius, and other cool things.
XML file saved at res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"> <!-- pressed -->
<shape android:shape="rectangle" >
<solid android:color="#color/blue" />
</shape>
</item>
<item> <!-- default -->
<shape android:shape="rectangle" >
<solid android:color="#color/red" />
</shape>
</item>
</selector>
This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />

Related

How to add margin to scrollbar in edit text

I am trying to add some margin to the scrollbar inside of an editText but I am not sure how to do that, given the scrollbar is inside of the editText itself ("scrollbars=horizontal"). The way it comes automatically is too close to the text and I need it to be farther away, is there a way to do that?
I have a custom style for the scrollbar and it already looks the way it does in the picture, except for the distance, maybe there's a way to adjust the margins there? So far, only the right margin works, the top and bottom one make the bar disappear.
It needs to look like this:
XML:
<EditText
android:id="#+id/edit_text"
style="#style/Field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/size_2"
android:layout_marginTop="#dimen/size_1"
android:layout_marginBottom="#dimen/size_1"
android:autofillHints="#null"
android:clickable="true"
android:duplicateParentState="true"
android:focusable="true"
android:inputType="text"
android:scrollbars="horizontal"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbHorizontal="#drawable/scrollbar"
android:scrollbarTrackHorizontal="#drawable/scrollbar_track"
android:gravity="center_vertical"/>
Scrollbar thumb:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="16dp"> <!-- Margin -->
<shape>
<solid android:color="#color/specific_border_2" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
Scrollbar track:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="16dp"> <!-- Margin -->
<shape>
<solid android:color="#E2E0EB" />
<stroke
android:width="1dp"
android:color="#color/specific_background_3" />
<size android:width="15dp" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
You can set android:scrollbarStyle="outsideOverlay" and control the margin of the scrollbar using android:paddingBottom
Demo:
<EditText
android:id="#+id/name_text"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:fadeScrollbars="false"
android:paddingBottom="8dp"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbHorizontal="#drawable/scrollview_thumb"
android:scrollbars="horizontal" />
scrollview_thumb.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#C2C3C3" />
<corners android:radius="15dp" />
<size android:width="5dp" />
</shape>

Button color from drawable doesn't change

I have some issue with styling of my buttons. I use drawable to change shape and style my app buttons. When I add background in activity xml my buttons change shape but doesn't change background color but when I add background programmatically with "setBackgroundResource" method everything is ok.
There is my piece of code:
activity xml:
`<Button
android:id="#+id/button_traditional"
android:background="#drawable/button_default_main_app"
android:textColor="#color/mustard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="32dp"
android:onClick="newGame"
android:text="#string/standard_game"
app:layout_constraintEnd_toStartOf="#+id/button_extended"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_gameChose" />
`
res/drawable:
<solid
android:color="#color/black"
/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:bottomRightRadius="10dp"
/>
Thanks in advance for your help.
Edit:
Whole drawable code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid
android:color="#color/black"
/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:bottomRightRadius="10dp"
/>
</shape>
By default the button tint is the primary color. You need to set it to null for the drawable to take effect by setting.
app:backgroundTint="#null"
So your whole button would look like this:
<Button
android:id="#+id/button_traditional"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="32dp"
android:background="#drawable/button_default_main_app"
android:onClick="newGame"
android:text="Standard game"
android:textColor="#a00"
app:backgroundTint="#null" />
The result:

I need a design like that but I'm facing two problems

I need a template for my list view with the following item design
I've tried some code I found but I did not get the result I need.
<item
android:bottom="-32dp"
android:left="-3dp"
android:right="-2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<corners android:radius="32dp"/>
<stroke
android:width="2dp"
android:color="#fff" />
<solid android:color="#android:color/transparent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="0dp" />
</shape>
</item>
this is my linearlayout background.
Though we can use png for this or or image to get this .
but if you really want to do this this drawable then here the code .
I tried and result was pretty same .
I have created a drawable using two layer list , that contains two different rectangle .
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="10dip">
<shape
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="12dp" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<size
android:width="60dp"
android:height="60dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:top="20dip">
<shape android:shape="rectangle">
<solid android:color="#800080" />
</shape>
</item>
You can use this in your view as background . I have used this in mine , so i am adding code of my item view .
<?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:background="#800080">
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_18"
android:background="#drawable/sample">
<TextView
android:id="#+id/crime_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Crime Title"
android:textColor="#color/colorAccent"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/crime_solved"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/crime_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="8dp"
android:text="Crime Date"
android:textColor="#color/colorAccent"
app:layout_constraintEnd_toEndOf="#id/crime_title"
app:layout_constraintStart_toStartOf="#id/crime_title"
app:layout_constraintTop_toBottomOf="#+id/crime_title" />
<ImageView
android:id="#+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="#+id/crime_date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/crime_title"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_solved" />
</android.support.constraint.ConstraintLayout>
Hope this will be helpful .
Instead of using xml drawable, you can Create a 9-patch image for that shape, that might help.
Create that shape in your favourite image-editor and copy that to drawable directory of your project.
In Android Studio, right-click the PNG image you'd like to create a NinePatch image from, then click Create 9-patch file.
Type a file name for your NinePatch image, and click OK. Your image will be created with the .9.png file extension.
Double-click your new NinePatch file to open it in Android Studio. Your workspace will now open.
The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.
Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.
When done, click File > Save to save your changes.

How do I enable/disable a drawable for a button object in android?

I have a border that I made for a button that should be enabled when the button is clicked on.
<Button
android:id="#+id/imageView_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="#drawable/border" />
My question is how can I enable/disable this drawable? Does java provide a solution for this?
I tried looking at this post, but it didn't seem to answer that question
Android - border for button
All you need to do is you have to put the selector drawable in the button like this
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
And the drawable like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<stroke android:width="10dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<stroke android:width="0dp"/>
</shape>
</item>

set background color generically to elements

I have two Text views in my xml file.
<TextView
android:layout_width="#dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Trainings"
android:gravity="center"
android:background="#drawable/rounded_corner"
android:onClick="changeColor"
/>
<TextView
android:id="#+id/learning_programs"
android:layout_width="#dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Learning Programs"
android:background="#drawable/rounded_corner"
android:gravity="center"
android:onClick="changeColor"/>
By default the background color of both of these is grey as set by the drawable rounded_corner.xml file, and when any of these is clicked I want it to change to white.
In my Java file I have
public void changeColor(){
this.setBackground(getResources().getColor((R.color.dark_grey_color)));
}
I know that I can use setOnclickListener and run a switch-case between the ids of the buttons based on View.
But the requirement is to it this way.
So, how can I implement it this way?
Thanks.
Just change your function. Replace Color.BLUE with whatever color or drawable you want. Because when you set onClick from xml it passes its view to the activity which you can get using view,getId to identify which click performed. In your case you are passing textview so cast that to textview.
public void changeColor(View view) {
//((TextView) view).setBackgroundColor(Color.BLUE);
GradientDrawable bgShape = (GradientDrawable)((TextView) view).getBackground();
bgShape.setColor(Color.BLUE);
}
If you want the view to maintain its background once it is clicked then you can keep the TextView and simply change the background color on the changeColor method. If you want it to toggle the background color according to click then it's a checkbox behaviour. If you want this last behaviour then implement it as follows:
Checkbox:
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Trainings"
android:button="#null"
android:gravity="center"
android:background="#drawable/view_state"/>
<CheckBox
android:id="#+id/learning_programs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Learning Programs"
android:button="#null"
android:background="#drawable/view_state"
android:gravity="center"/>
Then add a drawable to define those states (drawable/view_state):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#000" /> <!-- clicked color -->
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#FFF" /> <!-- default color -->
</shape>
</item>
</selector>
Just add the below code snipet onclick of your textview
your_textview.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));

Categories