how to dynamically get center point of view group layout? - java

This's my code
var graphHeight = parentLayout.height
var graphPointView = LayoutInflater.from(context).inflate(R.layout.view_graph_point, null, false)
graphPointView.y =(graphHeight / 2f)
graphView.addView(graphPointView)
and...
The grey line is on the center position in the view group layout, why the red point is't on the center of view group layout? I want to make that on the center position. (like grey line)
What's the problem with my code?
view_graph_point.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/graph_point_view"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerHorizontal="true"
android:background="#drawable/graph_round_point"
xmlns:android="http://schemas.android.com/apk/res/android" />
graph_round_point.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#A04000"/>
<stroke android:width="1dp" android:color="#fff" />
<size android:width="12dp" android:height="12dp"/>

What's the problem with my code
It's most likely "problem" with your layout, not the code. I guess your container is not stretched to max width. Just ensure its width is match_parent not wrap_content and that should be it.

Related

How to add a border to a particular side of a view

This is how I go about adding a border to a EditText. How can I go about adding a border only on one side of a EditText, and define the color and width of the border?
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
Vielen dank im voraus.
To get border on one side, you can create your own drawable like this:
<?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="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
</layer-list>
And set this drawable as background to your EditText.
If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line.
To draw horizontal line use this:
<View
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="2dp" />

How to draw a circle on different screens(devices) with same diameter in Android

I'm trying to draw a circle which will have the same diameter of 2.5 inches (or any other value) in every android device, regardless of the screen size. The diameter size must be same in all the devices and below is what I have tried.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/light"
tools:context=".MainActivity">
<ImageView
android:layout_width="3in"
android:layout_height="3in"
android:src="#drawable/oval_shape"
android:id="#+id/imageView"
android:scaleType="matrix"/>
</RelativeLayout>
oval_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="2.5in"
android:height="2.5in"/>
</shape>
The issue with this code is when I try it with 4.5 in screen (real phone), the diameter is 2.5inches as expected but when tested in 4 inch screen the diameter is less than 2.5 inches, it has a slight difference. I tried using millimeters (mm) as well but it didn't work well either.
So how can I achieve the requirement of same size circle in every phone?
//You are using fix static size in oval_shape.xml for all screens. thats why it is varying for different screens.
you can use this in dimens.
// use this code in oval_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="#dimen/inch_size"
android:height="#dimen/inch_size"/>
</shape>
// and You can put dimens.xml in
1) values-xlarge
2) values-small
3) values-normal
4) values-large
And give different sizes in dimens.xml within corresponding folders according to densities.
// like 2.5in in normal ,
1.5x of normal value in large (means 3.75in),
2x of normal value in xlarge(means 5in),
.75x of normal in small (means 1.875in aprox)
//hope it may be helpfull for you.

Android progress bar fill

I want a loading bar in a circle shape. I want to fill it with an integer i tried this
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateDrawable="#xml/progress" >
</ProgressBar>
progress.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<size
android:height="48dip"
android:width="48dip" />
<gradient
android:centerY="0.50"
android:endColor="#000000"
android:startColor="#ffffff"
android:gradientRadius="20"
android:type="radial"
android:useLevel="false" />
</shape>
and in my activity
protected void onPostExecute(Void result) {
bar.setProgress(50);
bar.setMax(100);
bar.setVisibility(View.VISIBLE);
}
The Circle stays black. If i set the gradient type to sweep, the circle fills with startcolor black and endcolor white.. and it constantly repeats.
But I want the circle to be filled once.. so it starts black and fills white; only 1 time..
Is this possible?
I want to reach this result:
http://ultimateprogrammingtutorials.blogspot.be/2013/02/how-to-draw-circular-progress-bar-in.html
here is one good example of CircularProgressBar, As per your requirement Hope this helps you
An example of how to do this:
https://github.com/Todd-Davies/ProgressWheel
There are many similar questions on stackoverflow:
How can I make a progress circle
Circular progressbar
Custom Drawable for ProgressBar/ProgressDialog
How To Make Circle Custom Progress Bar in Android

Accessing properties of drawable shapes through code

Assuming I have an ImageView defined as:
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_image_view_background"
/>
With my_image_view_background being:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<gradient
android:angle="45"
android:startColor="#555555"
android:endColor="#111111"
android:type="radial"
android:centerX="0.1"
android:centerY="0.1"
android:gradientRadius="70"
/>
<stroke
android:width="5dp"
android:color="#ff3300"
/>
</shape>
Is it possible to access the my_image_view_background drawable and change the colour of its stroke in code? Also, is it possible to do that based on the 'state' (pressed/selected/default)?
yes its possible
using GradientDrawable you can do it thru code.
link:
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xFFadced4, 0xFF71c2eb });
gd.setCornerRadius(0f);
gd.setStroke(1, 0xFF7c279b); //here you can define stroke width
gd.setCornerRadius(3f);
refer this site:

How to position a custom shape inside a view?

I've defined a custom shape in my res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:color="#FF404040"
android:width="1dp"
android:dashGap="3dp"
android:dashWidth="3dp"
/>
<size
android:height="1dp"
/>
</shape>
I'm using it as a background for one of my views. The shape is positioned vertically centered inside the view, but I want it to appear at the bottom of the view instead, is there a way to do this?
I am not sure there is a way to do position shape inside a view. However as a workaround I would consider something like this.
<RelativeLayout >
<OldView with shape as background now without any background
android:layout_alignParentTop="true" android:layout_alignParentBottom="true"/>
<View with this shape as background
android:layout_alignParentBottom="true"/>
</RelativeLayout>
This will supposedly give you what you want to achieve.
Try this , this may be useful,
http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Set your custom view as background of layout or view.
or if not useful then provide your layout xml file code here so i can help you more easily.
The following receives the margin layout params of the shape so that you can change the views' margins, positioning it anyway you want:
MarginLayoutParams params = (MarginLayoutParams)shape.getLayoutParams();
// Sets the margins of the shape, making it move to a specific location on the screen
params.setMargins(int left, int top, int right, int bottom);
I had a similar problem, but I wanted to have a background for my views, a rectangle, but only left and bottom sides of the rectangle to be shown, and no top or right ones. In order to achieve this, I have used this xml:
<?xml version="1.0" encoding="utf-8"?>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape
android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#FFFF00" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="1dp"
android:top="1dp">
<shape
android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#000000" />
</shape>
</item>
Note: There are two rectangles, one drawn on top of the other, the second one drawn on top of the first, but a bit shifted, so that 1dp of the first one to be shown on screen on the left and bottom. Also, you must note that the color of the second one must be chosen to be the one who hides. In my case it was the black color.
The result is (You may observe yellow lines only on the left and bottom):
I know gravity works on other kinds of drawables
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
Here is my decision to embed blocks
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#f00" />
<corners android:radius="50dp" />
<padding android:bottom="20dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#090" />
<corners android:radius="50dp" />
<padding
android:top="450dp"
/>strong text
</shape>
</item>
</layer-list>

Categories

Resources