Change theme of button on click - java

I set a theme for my button. This is the code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shape="rectangle">
<solid android:color="#color/theme_blue_color"/>
<corners
android:radius="100dp">
</corners>
<stroke android:width="1dp"
android:color="#05f">
</stroke>
</shape>
Then I want that image change on click. So I create another file (I named it reverse and I put it in drawable folder). Then in java I set:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn(getResources().getColor(R.color.theme_blue_color));
btn(R.drawable.reverse);
}
});
I works correctely! But I want that happened as happened when you normally click a button (change color onpress and then it stay with originally color).
How can I do this with theme? Thanks for answer

you need a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shape="rectangle">
<solid android:color="#android:color/red"/>
<corners
android:radius="100dp" />
<stroke android:width="1dp" android:color="#05f" />
</shape>
</item>
<item>
<shape android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shape="rectangle">
<solid android:color="#color/theme_blue_color"/>
<corners
android:radius="100dp" />
<stroke android:width="1dp" android:color="#05f" />
</shape>
</item>
</selector>
In this example you have two different shapes. The first one has the pressed state, with a solid red color. Use it as background for your button

Related

Right-Bottom-Corner black colored on a layer list

I have this code for the style of my list item.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:width="40dp"
android:height="40dp" />
<solid android:color="#444444" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
</item>
<item android:top="30dp">
<shape android:shape="rectangle">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#2f2f2f" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="00dp"
android:topRightRadius="0dp"/>
</shape>
</item>
</layer-list>
How can I dye the right bottom corner black ?
When I add a new layer-list-item it only appears at the bottom of the last.
Here is an example what I want to do:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item
android:bottom="-200dp"
android:right="-200dp"
android:top="250dp">
<rotate
android:fromDegrees="-6"
android:pivotX="0%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="#edf0f9" />
</shape>
</rotate>
</item>
</layer-list>
Try this one and chnage from degree ,pivot x and y
and one thing add this as child view and put both view inside framelayout

Adding shadow to the view

I have a view and I need to add shadow to it below and right. Here is my xml.
With this xml, the borders at bottom and right look like a line rather than a shadow.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/darkGray"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/darkGray"/>
</shape>
</item>
<item android:bottom="5px" android:right="5px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#color/lightGray"/>
</shape>
</item>
</layer-list>
How can add shadow with the above code?
For shadow you can use elivation attributes in your view component. Check below show for show shadow in button :-
<Button
android:elevation="5dp"/>

how to implement layout shadow in xml layer list?

i need to make a menu shadow like in the photo. how can i implement it?
this is the one I use
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="5px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
I just made this drawable for this case
shadow.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#00FFFFFF"
android:endColor="#0c6bba"
android:type="linear" />
</shape>
</item>
</layer-list>
And set it to the navigation drawer like this
navigationDrawer.setDrawerShadow(R.drawable.shadow, GravityCompat.START);

I'm developing a keyboard for android. How to change the text color of the keys?

How to change the text color of the keys?
Here is my keyboard layout.
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:keyBackground="#drawable/key_background"
android:background="#color/keyboard_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="#layout/preview" />
key_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom 2dp Shadow -->
<item android:top="4dp"
android:left="2dp"
android:right="2dp">
<shape android:shape="rectangle" >
<solid android:color="#color/key_shadow" />
<corners android:radius="4dp"/>
</shape>
</item>
<!-- White Top color -->
<item android:bottom="4px"
android:top="4dp"
android:left="2dp"
android:right="2dp">
<shape android:shape="rectangle" >
<solid android:color="#color/white" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
Please look into this you will get an idea for your problem.
http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html
android:keyTextColor="your color code"
U can use android:keyTextColor attribute: like
android:keyTextColor="#android:color/black"

Changing background color on touchevent on relative layout

i am using following code.
<RelativeLayout
android:layout_width="310dp"
android:layout_height="70dp"
android:layout_below="#+id/UIReportView"
android:layout_marginLeft="4dp"
android:layout_marginTop="2dp"
android:background="#drawable/small_corners" >
small_corners
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/UINoCorners"
android:shape="rectangle">
<corners android:radius="10dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="5dp"
android:bottom="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
Now , it shows a white bar. I want that, whenever i click on this relative layout its color should change, set by me.
report = (RelativeLayout) findViewById(R.id.UIMainReportViewTimely);
report .setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// My Code
}
});
What should i do here? report.onTouchEvent?
And how can i change the color of background.
Best Regards
Create another drawable with the new background, and in your setOnclickListener put
report.setBackgroundDrawable(R.drawable.newbackground_small_corners);
or
report.setBackgroundColor(Color.parseColor("#00000000"));
you can use 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="1dp" android:color="#999999" />
<padding android:left="10dp" android:top="3dp" android:right="10dp"
android:bottom="3dp" />
<corners android:radius="7dp" android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
<gradient android:startColor="#449def" android:endColor="#2f6699"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<corners android:radius="10dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="5dp"
android:bottom="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
</item>
</selector>
</shape>
I'd rather suggest to use the onTouch() method and check whether the touched coordinates are within the bounds of your relative layout. If yes, then just set background using setBackgroundColor().

Categories