I am trying to create some Circles inside my layout, here is my drawable.circle code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:endColor="#80FF00FF"
android:startColor="#FFFF0000" />
</shape>
but since I am trying to create 10 of this view and also do some functions on them I can't create them inside my layout file, I want to use array of views - here is what I got so far:
private View imageViewArray[];
private Random rand;
private int layoutwidth;
private int layoutheight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rand = new Random();
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
layoutwidth = size.x;
layoutheight = size.y;
imageViewArray = new ImageView[10];
for (int i = 0; i < 10; i++) {
imageViewArray[i] = new View(this);
imageViewArray[i].setTag(i);
randomx = rand.nextInt((layoutwidth - layoutwidth / 3) + layoutwidth / 3);
randomy = rand.nextInt(layoutheight - layoutheight / 3);
imageViewArray[i].setX(randomx);
imageViewArray[i].setY(randomy);
rlt.addView(imageViewArray[i]);
}
How do I assign Circle.xml to all of these views? imageViewArray[i].set?
You can add your drawable xml file as background resource to the ImageView:
imageViewArray[i].setBackgroundResource(R.drawable.circle);
There's no reason not to draw them in the xml, except if the random part is necessary.
Anyway, in order to add views to your main view, create a layout with the circle only. Then inflate it.
final View inflatedView = LayoutInflater.from(this).inflate(R.layout.your_circle_id, null);
/*Manipulate the inflated view (for example, change location)*/
LinearLayout container = (LinearLayout)findViewById(R.id.your_main_layout);
container.addView(inflatedView);
I hope that I got you correctly. Change the LinearLayout if it's off a different type.
You should use java graphics classes to do it.
Related
How can I set a drawable for button text color programmatically as in XML ?
I am creating a servel buttons and I want to change their text color when pressed.
The method btn.setTextColor() has an integer as entry which represents a color value so when I put a drawable in there it is considered as color.
private void showDialog(List list) {
View mView = getLayoutInflater().inflate(R.layout.dialogfor_each_activity, null);
LinearLayout ll = mView.findViewById(R.id.linearLayout);
ll.setWeightSum((float)list.size());
dialog.setContentView(R.layout.dialogfor_each_activity);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels * 0.90);
int height = (int) (displaymetrics.heightPixels * 0.50);
dialog.getWindow().setLayout(width,height);
dialog.setCancelable(true);
for (int i = 0; i < list.size(); i++) {
Button btn = new Button(this);
btn.setText(list.get(i).toString());
btn.setTag(list.get(i).toString());
btn.setId(i);
btn.setBackgroundResource(R.drawable.strockbtn_white_to_roundbtn_white);
btn.setTextColor(R.drawable.text_blue_to_white);
btn.setTextSize(15);
btn.setTypeface(null, Typeface.BOLD);
btn.setPadding(0,0,0,0);
params.setMargins(150, 6, 150, 6);
btn.setLayoutParams(params);
btn.setOnClickListener(this);
ll.addView(btn);
buttons.add(btn);
}
dialog.setContentView(mView);
dialog.show();
}
I want the buttonTextColor to be set to R.drawable.text_blue_to_white like in XML.
EDIT : This is not a duplicate question I already done what I want in a another way.
I need the equivalent java code for (if it is possible):
android:textColor="#drawable/text_blue_to_white"
You need to have a color resource in res/color/blue_to_white:
<selector>
<item
android:state_pressed="true"
android:color="#ffffff"/>
<item android:color="#0000ff"/>
</selector>
Then do:
btn.setTextColor(getResources().getColorStateList(R.color.blue_to_white));
EDIT: Upon revisiting https://developer.android.com/reference/android/content/res/Resources.html#getColorStateList(int), getColorStateList(int) is deprecated in API 23. If your target is >= API 23, simply:
btn.setTextColor(getResources().getColorStateList(R.color.blue_to_white, getTheme()));
ContextCompat.getColor(context, R.color.yourcolor);
or
button.setTextColor(getResources().getColorStateList(R.color.yourcolorColor));
and create a color ressource for each state of your desired Color
so I have two different Drawables which I need to merge and get a single Drawable at runtime. I want the first Drawable to be at the top and the other one at the bottom. I came across LayerDrawable and it looks like it's exactly what I need, but I'm having trouble trying to arrange the Drawables.
So I have an ImageButton which is 48x48 dp and this is where the final Drawable goes. The first Drawable is a plus button (20x20 dp) and the second one is a small dot (4x4 dp) below the plus button.
The plus button Drawable is loaded using font glyphs. I'm creating the dot button Drawable using this xml snippet:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/white_40"/>
<size
android:width="4dp"
android:height="4dp"/>
</shape>
My first approach was to just add both Drawables to the LayerDrawable, but when I do that, the width/height attributes of the dot specified in the xml are ignored, and it stretches to cover the plus icon.
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
The above results in this:
The second approach I tried was by using setLayerInset to try and position the two Drawables.
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInset(0, 0, 0, 0, 0);
finalDrawable.setLayerInset(1, dp(22), dp(44), dp(22), 0);
The above code snippet ended up placing the dot in the correct position, but it also started affecting the position and size of the plus button and it ended up looking like this:
But what I really want is to have the plus button in the centre of the ImageButton and the plus icon just below it. Does anyone have an idea where I'm going wrong and how can I position the two drawables correctly?
PS: My app supports API 15+, so I can't use a bunch of methods from LayerDrawable's API like setLayerGravity, `setPaddingMode, etc.
Edit
This code will work on API levels below 23:
ImageButton button = (ImageButton) findViewById(R.id.button);
Drawable plusIcon = ContextCompat.getDrawable(this, R.drawable.plus);
Drawable dotIcon = ContextCompat.getDrawable(this, R.drawable.oval);
int horizontalInset = (plusIcon.getIntrinsicWidth() - dotIcon.getIntrinsicWidth()) / 2;
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInset(0, 0, 0, 0, dotIcon.getIntrinsicHeight());
finalDrawable.setLayerInset(1, horizontalInset, plusIcon.getIntrinsicHeight(), horizontalInset, 0);
button.setImageDrawable(finalDrawable);
Original
The following code works for me:
ImageButton button = (ImageButton) findViewById(R.id.button);
Drawable plusIcon = ContextCompat.getDrawable(this, R.drawable.plus);
Drawable dotIcon = ContextCompat.getDrawable(this, R.drawable.oval);
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInsetBottom(0, dotIcon.getIntrinsicHeight());
finalDrawable.setLayerGravity(1, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
button.setImageDrawable(finalDrawable);
This produces the following ui:
This is how i solved this:
final View view = findViewById(R.id.view_in_layout);
Drawable bottom = ContextCompat.getDrawable(context, R.drawable.ic_bottom);
Drawable top = ContextCompat.getDrawable(context, R.drawable.ic_top);
//bottom = index 0, top = index 1
final LayerDrawable layer = new LayerDrawable(new Drawable[]{bottom, top});
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
//get the real height after it is calculated
int height = view.getHeight();
//set the height half of the available space for example purposes,
//the drawables will have half of the available height
int halfHeight = height / 2;
//the bottom drawable need to start when the top drawable ends
layer.setLayerInset(0, 0, halfHeight, 0, 0);
//the top drawable need to end before the bottom drawable starts
layer.setLayerInset(1, 0, 0, 0, halfHeight);
view.setBackground(layer);
view.removeOnLayoutChangeListener(this);
}
});
You can choose different dimensions for your drawables or moving them with the indexes. I used View.OnLayoutChangeListener(...) for example to wait for the current available height of the view
I want to color e.g. 20 percent of layout background and after some time color 40 percent of it and so on.
How can I achieve this in android ?
You can start off with a ClipDrawable. This will clip another drawable — for instance, a ShapeDrawable — based on the drawable level.
Then in your timer callback:
int level; // from 0 to 10000 = 100%
view.getBackground.setLevel(level);
Drawable#setLevel
EDIT: Here's an example:
Define the shape drawable in /res/drawable. Call it bkgd_shape.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/background"/>
</shape>
Define the background drawable in /res/drawable. Let's call it bkgd_level.xml:
<?xml version="1.0" encoding="utf-8"?>
<clip
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/bkgd_shape"
android:clipOrientation="horizontal"
android:gravity="left|clip_horizontal|fill_vertical"/>
You might be able to put a color in directly for the drawable source, but I haven't tried it.
Set it as the background of your layout:
android:background="#drawable/bkgd_level"
Call setLevel on the drawable:
int level; // from 0 to 10000 = 100%
view.getBackground.setLevel(level);
You can put any type of view in the background and update it's with and set the color you'd like it to have dynamically.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Expanding across the width then, just go by percentage.
int percentChange = .2; //Update this accordingly, put in its own function possibly
int backgroundWidth = width * percentChange;
For example, using an imageView in the background:
ImageView background = (ImageView) findViewById(R.id.expandingBackground);
background.requestLayout();
background.getLayoutParams().width = backgroundWidth;
Hope that helps, good luck.
I would know if there is the possibility to create a new Android Activity with a different layout comparing to linear layout, table layout, relative layout ect...
My goal is to create an activity where the android load some object (for example notes) and place this notes into the activity in order to give the illusion at the final user to have a lot of post-it on the screen of the tablet.
The result that i would to achieve is very similar to the layout of computer, where you have a lot of icons and you can insert, remove and move this icon that represent my notes.
I thougth to use a grid like this http://developer.android.com/guide/tutorials/views/hello-gridview.html, but with this kind of layout can i move where i want my icons??
Something like this:
public class MyActivity extends Activity {
private RelativeLayout _mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_mainLayout = (RelativeLayout) findViewById(R.id.canvas);
addChild(50, 50, 150, 150, Color.RED);
addChild(150, 250, 50, 50, Color.GREEN);
addChild(200, 200, 100, 100, Color.BLUE);
}
public void addChild(int centerX, int centerY, int width, int height, int color) {
int X = centerX - width / 2;
int Y = centerY - height / 2;
RelativeLayout child = new RelativeLayout(this);
child.setBackgroundColor(color);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
layoutParams.leftMargin = X;
layoutParams.topMargin = Y;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
child.setLayoutParams(layoutParams);
_mainLayout.addView(child);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/canvas"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg0large"
>
</RelativeLayout>
You would need to draw your views manually to the screen, probably using something like a Canvas. This will allow you to draw yours views where you like.
I have a class Ball which extents View.Inside there i give some characteristics and implements onTouchEvent() so i can handle the movement.Also i use onDraw so i can draw the bitmap of the ball. in my activity class i crate a new Layout and i add the view to it so it can be displayed. Everything works fine except when, i try to add more balls to my layout they don't appear!Always the first added in layout ball is displayed!
Here's the onCreate code from activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);
ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
}
What could possibly be the problem?I have try it also with only one setContentView() at the end.I am thinking that i can't use a Layout so i can draw bitmaps which are in a custom View!Am i right?
Should i change my code and create a View and inside there make an array with all the balls i want to be displayed and then set this View to be displayed from my main class of activity?(like this setContentView(customview)).
You are calling setContentView several times, while it is expected to call only once per activity initialization.
UPDATE:
Could you use this layout xml instead of programmatic way you use? This is just to be 100% sure the container to which you will add the ColorBalls is Ok.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Just in case, here is the code to include it in the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_name_of_layout);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
..
container.addView(ball1);
container.addView(ball2);
..
}