How to draw circle in android by pressing a button? - java

I want to show small dots on screen while pressing a button, something like pin code...
switch (view.getId()){
case R.id.buttonNum1:
editor.putString("PinNumbers",getString(R.string.numberOne));
int x = 10;
int y = 30;
int r = 100;
Paint mPaint = new Paint();
mPaint.setColor(Color.BLACK);
Canvas mCanvas = new Canvas();
mCanvas.drawCircle(x,y,r,mPaint);
break;

You can achive by creating ripple effect
create one drawable name like pin_button_ripple in drawable-v21 folder and add bellow code
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorPrimary" />
</shape>
</item>
</ripple>
Now in Layout file set it as a background of button and define click listener in activity
<Button
android:id="#+id/btnPinButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#drawable/pin_button_ripple"
android:padding="20dp"
android:text="10"/>
You will see the ripple effect in circle shape like we enter Pin
number or password via keyboard and showing that effect

Related

Set imageview foreground gradient color programmatically

I have xml gradient drawable resource file here:
file: fg_graidient
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:centerX="0%"
android:startColor="#00020321"
android:centerColor="#F2000000"
android:endColor="#B2020321"
android:angle="45"/>
</shape>
I can the imageview foreground like below:
<ImageView
android:id="#+id/backimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:foreground="#drawable/fg_graidient"
/>
But I want to set this drawable foreground for imageview programmatically. Is it possible?
Create programmatically gradient shapes
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
#Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, width, height,
new int[]{Color.GREEN, Color.GREEN, Color.WHITE, Color.WHITE},
new float[]{0,0.5f,.55f,1}, Shader.TileMode.REPEAT);
return lg;
}
};
PaintDrawable p=new PaintDrawable();
p.setShape(new RectShape());
p.setShaderFactory(sf);
use
Button btn= (Button)findViewById(R.id.btn);
btn.setBackgroundDrawable((Drawable)p);
Try this snipet:
ImageView img = (ImageView)findViewById(R.id.backimg);
img.setImageResource(R.drawable.fg_graidient);

Creating a Shape from a Drawable without pixel transparency in Android

I'm trying to create a Shape or a Rect from a Drawable but without the transparency of the Drawable.
As you can see here, the following button has transparency on its borders. My issue is that I'm creating a panel Editor and I want to avoid superposition of widget when I move a widget.
To achieve this, I have used the method : Rect.intersects(Rect)
However, the Rect used is the shape of the entire drawable(with the transparency) and it's create too much empty space.
EDIT: more informations
Here is my Editor, I succed to avoid superposition but the superposition is base on the Shape of the drawable (picture). And the picture's tranparency create some empty space on my panel.
and my check collision code, where tempRect is the current Moving widget
public boolean checkCollision(Rect tempRect,ArrayList<IHMObject> listWidget) {
float dimen = activity.getResources().getDimension(R.dimen.abc_action_bar_default_height)+ 38;
for(IHMObject widget :listWidget ){
int[]location = new int[2];
View v =(View)widget.getView();
v.getLocationInWindow(location);
//vérifier la postion du widget en fonctions des autres widgets de l'IHM
Rect staticRect = new Rect(location[0],location[1]-(int)dimen,location[0]+v.getWidth(),location[1]+v.getHeight()-(int)dimen);
if (this.id!=widget.id){
if (staticRect.intersect(tempRect)) {
//il y a une collision entre les deux rectangles
return true;
}
}
}
return false;
}
Create a round_shape.xml file in drawable folder like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners android:radius="10dp"/>
</shape>
Set this Draweble like below
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:src="#drawable/round_shape" // here is your round_shape
android:background="#000000"
android:text="Buttons" />

How to set Random GradientDrawable Color programmatically - Android

I have a problem with ProgressBar. When i press the button "Start" Random function choose one color from array, but only the first color is set as "background", when i click one more time new color is choose but not set as background. When i exit from app and start it again new color is choose but now only one. I would like to have different background color all the time when i press the button.
activity_main.xml
<ProgressBar
android:id="#+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:indeterminate="false"
android:max="100"
android:progress="100"
android:progressDrawable="#drawable/circular"
android:secondaryProgress="100" />
circular.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/SecondaryProgress">
<shape
android:innerRadiusRatio="5.2"
android:shape="ring"
android:useLevel="true"
android:thicknessRatio="9.5">
</shape>
</item>
MainActivity.java
public void Start(){
Random random = new Random();
String[] hex_colors = {"F8ED31", "000000", "F7931D", "ED1C24", "7A1777", "ED135A", "71BF43"};
int random_hex = random.nextInt(hex_colors.length);
String color="#"+hex_colors[random_hex];
layers = (LayerDrawable) ContextCompat.getDrawable(this,R.drawable.circular);
shape = ( GradientDrawable) layers.findDrawableByLayerId(R.id.SecondaryProgress);
shape.setColor(Color.parseColor(color));
}

How to make horizontal line in Android programmatically

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

android button with double border and gradient

I want to create a custom button.
This button should have a gradient and a two pixel border, but the inner and outer edge should be in a different color (example: inner is red and outer is yellow).
My question: how do I program the double border (like in the image)?!
Image:
I tried with an XML file with two strokes, but it doesn't work.
I could do this with a 9png file, but I want to do it with pure coding.
btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<padding android:left="3.5px"
android:top="3.5px"
android:right="3.5px"
android:bottom="3.5px"/>
<solid android:color="#d4e23a"/>
</shape>
</item>
<item >
<shape android:shape="rectangle">
<padding android:left="4.5px"
android:top="4.5px"
android:right="4.5px"
android:bottom="4.5px"/>
<solid android:color="#d4413a"/>
</shape>
</item>
<item >
<shape android:shape="rectangle">
<gradient android:startColor="#37c325"
android:endColor="#2573c3"
android:angle="-90"/>
</shape>
</item>
</layer-list>
set the above xml as button background.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:background="#drawable/btn_bg"
android:gravity="center"
android:padding="10dp"
android:textStyle="bold"
>
</Button>
Result:
If you want to go by plain Java code then you need to create a class which extends the button, write all your logic in
public void onDraw(Canvas iCanvas).
I have pasted small code snippet from one of my project. Give it a try. thought I have not created the gradient, I have used plain colors.
public class MyButton extends Button {
private Paint m_paint1 = new Paint();
private Paint m_paint2 = new Paint();
private int m_color1 = 0XFF92C84D; // LIKE AN OLIVE GREEN..
private int m_color2 = 0XFFFF0000; // LIKE AN OLIVE GREEN..
private RectF innerRect1, innerRect2;
public MyButton(Context context) {
super(context);
setBackgroundColor(Color.BLACK);
}
public void onDraw(Canvas iCanvas) {
// draw the button background
m_paint1.setColor(m_color1);
m_paint2.setColor(m_color2);
innerRect1 = new RectF(5, 5, getWidth() - 5, getHeight() - 5);
innerRect2 = new RectF(10, 10, getWidth() - 10, getHeight() - 10);
iCanvas.drawRoundRect(innerRect1, 0, 0, m_paint1);
iCanvas.drawRoundRect(innerRect2, 0, 0, m_paint2);
}
public static RelativeLayout.LayoutParams GetRelativeParam(int iLeft,
int iTop, int iWidth, int iHeight) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
iHeight, iWidth);
params.leftMargin = iLeft;
params.topMargin = iTop;
return params;
}
}
and
RelativeLayout relLay = new RelativeLayout(this);
MyButton m_button = new MyButton(this);
setContentView(relLay);
relLay.addView(m_button, MyButton.GetRelativeParam(0, 0, 100, 500));
Put the button inside a layout that you will create just for it. So set to the layout the outest background color that you want.

Categories