I have a drawable circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:type="radial"
android:startColor="#FFFFFF"
android:endColor="#EEEEEE"
android:gradientRadius="90"
/>
<size android:width="50dp" android:height="50dp"/>
<stroke android:width="2dp" android:color="#999999"/>
</shape>
And I'm trying to change the stroke of this circle on code side, like so:
public void onStrokeNeedsToChange(int newStroke)
{
GradientDrawable gradientDrawable = (GradientDrawable)mButton.getBackground();
gradientDrawable.setStroke(newStroke, Color.RED);
}
However, when I change the size of the stroke it is being clipped as if the whole drawable button is inside of a square.
Does anybody know how to increase the size of the stroke without it clipping?
Related
i am creating a piano App in android studio. I am using the onTouch() method to check if the user has pressed a button and then play a sound. I have created two drawable resources for the piano keys, one is to be used when the piano keys are not pressed and another one to be used when a piano key is pressed. How do i set a condition inside my onTouch() method, to set the background to the respective drawable resources when the piano keys are pressed , and when they are not pressed? Thank you!
Here is the code
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
...
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.p1:
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
//set the background to key_pressed
soundPoolObject.play(c3, 1, 1, 0, 0, 1);
break;}
case MotionEvent.ACTION_UP:{
//set the background to key_normal
soundPoolObject.pause(c3);
break;}
}
}
return true;
}
}
Drawable resource key_pressed.xml
<?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="#00000000" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
Drawable resource key_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_shadow"/>
<item android:bottom="10dp" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<solid android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
Try this:
...
case R.id.p1:
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
//set the background to key_pressed
v.setBackgroundResource(R.drawable.key_pressed);
soundPoolObject.play(c3, 1, 1, 0, 0, 1);
break;}
case MotionEvent.ACTION_UP:{
//set the background to key_normal
v.setBackgroundResource(R.drawable.key_normal);
soundPoolObject.pause(c3);
break;}
}
...
I'm trying to draw drawable consisting of circle and rectangle. It's working fine while api is set to 23. I noticed that following attributes of 'item' have been added in newest api: height, gravity, weight. Is it possible to get the effect in older sdk?
Drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="5dp"
android:gravity="center_vertical"
android:left="5dp">
<shape android:shape="rectangle">
<size
android:width="50dp"
android:height="5dp" />
<solid android:color="#125572" />
</shape>
</item>
<item
android:width="20dp"
android:height="20dp"
android:gravity="center_vertical">
<shape android:shape="oval">
<solid android:color="#125572" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
Image using api 23:
Image using api 22:
I know this question is quite old but I solved this problem and maybe it is useful for someone even now.
For the solution I created two classes.
One class called MyLayerDrawable that extends LayerDrawable. The important thing to keep in mind about this class is that you can get the top and the left insets for each layer.
public class MyLayerDrawable extends LayerDrawable{
private List<Layer> layers;
//some code here
public static class Layer{
private Drawable drawable;
private int topInset;
private int leftInset;
public Layer(Drawable drawable,int topInset,int leftInset){
this.leftInset=leftInset;
this.drawable=drawable;
this.topInset=topInset;
}
public Drawable getDrawable(){
return drawable;
}
public int getTopInset(){
return topInset;
}
public int getLeftInset(){return leftInset;}
}
The second class I created is called MyImageViewPatch that owns an ImageView.
The only public method of this class is setImageMyLayerDrawable(MyLayerDrawable layerDrawable). This method delegates setting the image drawable to the ImageView that it owns:
public void setImageMyLayerDrawable(MyLayerDrawable layerDrawable){
imageView.setImageDrawable(layerDrawable);
}
But before calling the setImageDrawable method an observer object has been added to the ImageView in the MyImageViewPatch constructor. This observer is called before drawing the Drawable in the ImageView:
imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
MyLayerDrawable layerDrawable=(MyLayerDrawable)imageView.getDrawable();
int numberOfLayers=layerDrawable.getNumberOfLayers();
Drawable drawable;
Rect bounds;
for(int i=0;i<numberOfLayers;i++){//for each layer
drawable=layerDrawable.getDrawable(i);
bounds=drawable.getBounds();
bounds.top=layerDrawable.getLayer(i).getTopInset();
bounds.left=layerDrawable.getLayer(i).getLeftInset();
if(drawable.getIntrinsicHeight()<bounds.height()){
bounds.bottom=drawable.getIntrinsicHeight()+layerDrawable.getLayer(i).getTopInset();
}
if(drawable.getIntrinsicWidth()<bounds.width()){
bounds.right=drawable.getIntrinsicWidth()+layerDrawable.getLayer(i).getLeftInset();
}
}
return true;
}
});
As you can see before drawing the MyLayerDrawable the bounds for each Layer are adjusted based on the left and top insets; this is the key part to get the drawables drawn in the correct size instead of stretched.
Here is the shapes xml file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="5dp"
android:gravity="center_vertical"
android:left="5dp"
android:top="8dp">
<shape android:shape="rectangle">
<size
android:width="50dp"
android:height="5dp" />
<solid android:color="#125572" />
</shape>
</item>
<item
android:width="20dp"
android:height="20dp">
<shape android:shape="oval">
<solid android:color="#125572" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
To centre the rectangle vertically I used android:top="8dp" instead of android:gravity="center_vertical".
Here is the code for the Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv=(ImageView)findViewById(R.id.imageView);
MyLayerDrawable myLayerDrawable=MyLayerDrawable.Builder.build(getApplicationContext(),R.drawable.layer_d_example);
MyImageViewPatch myImageViewPatch=new MyImageViewPatch(iv);
myImageViewPatch.setImageMyLayerDrawable(myLayerDrawable);
}
}
I have the complete code in github. You may check it out: https://github.com/marcosbses/my_layout_drawable.git
I want to change color of underline in Edittext and i follow some tutorials to do this.
I create this xml file:
<?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="#00FFFFFF" />
<padding android:bottom="2dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
But i only do the underline not all rectangle, similiar as default line but white inside of blue and solid background transpartet.
How can i do this?
Thanks
You can change it programatically too:
public static void changeEditTextUnderlineColor(EditText editText) {
int color = Color.parse("#[putColorHere]")
Drawable drawable = editText.getBackground();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackground(drawable);
} else {
editText.setCompoundDrawables(null, null, drawable, null);
}
}
you can change Edit color Underline in EditText specifying it in styles.xml. In your app theme styles.xml add the following.
<item name="android:textColorSecondary">#color/primary_text_color</item>
I am trying to fill linear layout programmatically like this
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(dpToPx(6), 0, 0, 0);
kac.setLayoutParams(params);
LinearLay.addView(kac);
and I want to add (after every TextView (like kac)) a horizontal line like this one I have in my xml
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#B3B3B3"
android:padding="10dp"
/>
View v = new View(this);
v.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
5
));
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
LinearLay.addView(v);
I recommend creating a drawable containing the line and setting it as a background to your textview. You would avoid unnecessary views.
Sample drawable could be:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#LINE_COLOR"/>
<padding
android:bottom="1dp"
/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#BACKGROUND_COLOR"/>
</shape>
</item>
</layer-list>
And you can set it by
textView.setBackgroundResource(R.drawable.resource)
edit* example:
#Override
protected void onDraw(Canvas canvas)
{
//this would draw the textview normally
super.onDraw(canvas);
//this would add the line that you wanted to add at the bottom of your view
//but you would want to create your rect and paint outside of the draw call
//after onmeasure/onlayout is called so you have proper dimensions
canvas.drawRect(new Rect(0, this.getHeight()-1, this.getWidth(), this.getHeight()), mPaint);
}
if i understand that you are just looking to draw a line after every textview, your best bet is probably to just extend TextView with a custom class, overriding the onDraw method, adding a paint call to draw the line yourself
you just need to set style (background) to your TextViews, making Views to in this case is a waste
For example, I have this code:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dip"
android:color="#000000"/>
</shape>
How can I change the value of android:color?
Thanks
You can create it in runtime, then change it whenever you want:
RoundRectShape rect = new RoundRectShape(
new float[] {30,30, 30,30, 30,30, 30,30},
null,
null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint.setColor(0x99FFFFFF);
view.setBackgroundDrawable(bg);