How to change button background in java? - java

I have xml file in drawable folder for rounded button - rounded.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ccc" />
<corners android:radius="5dp" />
<stroke android:width="1dp" android:color="#000" />
</shape>
Usually I set it by using android:background = "#drawable/rounded" inside button xml, but I would like to create few xml files with different colors so that I could change app theme.
Do you know how should I set background for button, but in java?

This should work:
Button button = (Button) findViewById(R.id.myButton);
button.setBackgroundResource(R.drawable.rounded);

Related

Android handle virtual Direction pad on screen to focus view

I'm developing an app can visible Direction pad on the screen, but I don't know how to handle or override action like UP, DOWN, LEFT, RIGHT to focus the next view (like some buttons in image below).
Set this tow attributes for your view items :
android:focusableInTouchMode="true"
android:background="#drawable/bg_view"
where bg_view.xml is :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#android:color/white" />
<solid android:color="#1F1F3E" />
</shape>
</item>
<item>
<shape>
<solid android:color="#1F1F3E" />
</shape>
</item>
I found a solution to what I need here Android simulate key press
. Use Instrumentation class to sendKeyEvent.

How to create a custom circular seekbar with custom thumb?

Im trying to create a custom circular seekbar like this one:
I have the shape and the thumb xml done but the thumb isn't appearing when i put it in the layout xml.
Seekbar background xml (lap_counter.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:thicknessRatio="16"
android:useLevel="false">
<corners android:radius="500dp" />
<stroke
android:width="45dp"
android:color="#color/blue_secondary" />
</shape>
</item>
</layer-list>
Custom thumb xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white">
<item>
<shape android:shape="oval">
<solid android:color="#color/white"/>
<stroke android:color="#color/orange" android:width="3dp"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
</item>
</layer-list>
But when apply the custom xml this are the results:
<androidx.appcompat.widget.AppCompatSeekBar
android:id="#+id/sb_lap_counter"
android:layout_width="700dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:progress="50"
android:background="#drawable/lap_counter"
android:thumb="#drawable/lap_counter_thumb"
tools:progress="50" />
I tried putting it in as a custom style in themes.xml but it doesn't work either.
What am i doing wrong?
You are using a seekbar.This only works for horizontal seeks.That orange thing is the thumbnail which you need.

How to achieve Android Button Animation with text-blurred and shadow

I want to achieve this button animation in Android. The sample below is from iOS. I want the shadow to disappear when the user clicks the button and reappear once the user releases the button.
Any help will be highly appreciated.
You need to add color like the following. For example, the color name is button_text_color.xml. Here is the .xml file which needs to be put in the color folder. If the color folder does not exist, create one in your res directory.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#50FFFFFF" android:state_pressed="true" />
<item android:color="#FFFFFF" android:state_pressed="false" />
</selector>
Check that I have added 50% transparency to the white color in the pressed state. Now just add this attribute in the Button where it is declared.
Now you need a background drawable, to be put in your drawable folder. For example, let us take the name of the drawable is button_state_list_animator.xml. This should have the following content.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<layer-list>
<item>
<shape>
<solid android:color="#android:color/darker_gray" />
<corners android:radius="19dp" />
</shape>
</item>
<item android:bottom="5px">
<shape>
<padding android:bottom="5dp" />
<gradient android:angle="270" android:endColor="#163969" android:startColor="#1c4985" />
<corners android:radius="19dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="5dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
<item android:state_pressed="true">
<layer-list>
<item>
<shape>
<solid android:color="#102746" />
<corners android:radius="19dp" />
</shape>
</item>
<item android:bottom="5px">
<shape>
<padding android:bottom="5dp" />
<gradient android:angle="270" android:endColor="#163969" android:startColor="#1c4985" />
<corners android:radius="19dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="5dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Now your button construction is simple.
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="32dp"
android:background="#drawable/button_state_list_animator"
android:text="Save Changes"
android:textColor="#color/button_text_color" />
Here is the screenshot from my code.
You can have the push-down animation using the library as well, as mentioned in an answer here.
Hope that helps!
Use the animation effect (as the code below) on the custom button when clicking on the button.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:toXScale="0.8"
android:fromYScale="1"
android:toYScale="0.8"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
<alpha
android:fromAlpha="1"
android:toAlpha="0.8"
android:duration="500">
</alpha>
</set>
set Animation on Button Click using.
btn_custom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R
.anim.animation_btn);
btn_custom.startAnimation(animation);
}
});
try changing alpha values as your need and see the effect.
You can use this library : pushdown-anim-click
How to install:
compile( 'com.github.thekhaeng:pushdown-anim-click:1.1.1' ){
exclude group: 'com.android.support'
}
How to use:
Button button = findViewById( R.id.button );
PushDownAnim.setPushDownAnimTo( button, ... )
.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick( View view ){
Toast.makeText( MainActivity.this, "PUSH DOWN !!", Toast.LENGTH_SHORT ).show();
}
});
For Shadow:
As I saw library files, There are only three java files. He just used class to animate button. You can give shadow in your button by this way. Even though I have created issue You can keep watch on this.
For more functionality, you can visit github link.
Thank you.
Add a layout with the background as the shadow below the button. In the button.setOnTouchListener hide the layout when button is pressed and make it visible when released.
However it will only work if the shadow is below.

How to set the thickness of an EditText line?

I noticed that the two EditTexts I drew have different line thickness (the Password EditTexts line is thicker). Is there a property I can set so that they have the same thickness?
Thanks
You can handle it with a custom drawable
Add this drawable as a background of your edit text and you can can control it's width by changing width of stroke
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle" >
<stroke
android:width="10dp" />
<solid android:color="#00FFFFFF" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
</layer-list>

Change android button colour on click without changing border colour?

I am new to Android and i was setting up my xml file. Is there a way to change the colour of the button with a feel of "click" when the button is clicked and then return to it's original colour WITHOUT changing the colour of the border?
I am using eclipse.
Thank you
First you need to make some xml files in the Drawable folder:
btn_clicked.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ff0000" />
<stroke
android:width="1dp"
android:color="#fff" />
</shape>
btn_normal.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#00ff00" />
<stroke
android:width="1dp"
android:color="#fff" />
</shape>
name_btn.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_clicked" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal"/>
</selector>
and then use it like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:layout_width="140dp" android:layout_height="40dp"
android:background="#drawable/cancel_btn" />
</RelativeLayout>
AndroidGeek,
You can specify the effects inside an .XML file under the drawable folder and then linking the file to the Button under the layout folder.
Example:
Inside your activity_main.xml Your Button tag will look like as :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Styled Button"
android:id="#+id/styledButton"
android:background="#drawable/button"/>
Create an XML file [Example: button.xml] under the res/drawable folder to specify the changes that will take place when the button is pressed.
Below is the contents of the button.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Changes that the button has to show when it is pressed -->
<item android:state_pressed="true" >
<shape>
<solid
android:color="#ef4444" />
<!-- Keep the stroke values same if you want the same border effect, irrespective you press / un-press the button
In case ih you want to change the border thickness, while the button is being clicked,
then increase the "android:width" value -->
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<!-- Default appearance when the button is displayed -->
<item>
<shape>
<gradient
android:startColor="#ef4444"
android:endColor="#992f2f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Hope this helps.
This should work irrespective of the IDE. Eclipse and Android Studio.

Categories

Resources