I have an Android application that create all the layout programmatically in the onCreate method, after that I need to do some calculation on the coordinates of some of the views I created, but when I try to get coordinates on screen, it always return 0,0.
I think that may be caused from the fact that the activity is still not rendered as in fact by debugging I still see a grey screen if i place a breakpoint on the point where I get coordinates, how can I calculate coordinates after starting activity?
For me it's ok even if it's out of onCreate, I just need it to be executed after that the activity rendering, and if possible I'd like to not use a OnGlobalLayoutListener (if there's no other way well... i'll just get used to the idea, but i'd like to know if there's some other way).
Ah, I even tried to put it on the onResume, but it doesn't work, probably when onResume is called the rendering is still not finished.
Extends View class and writing your own protected void onSizeChanged(int w, int h, int oldw, int oldh) { could help you.
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
final float yCenterX = w / 2;
final float yCenterY = h / 2;
...
}
Related
I have a custom LinearLayout class which I'm using to clip all of its child Views through Canvas#clipPath():
public class MaskedLinearLayout extends LinearLayout {
protected Path mMaskingClip = new Path();
(...)
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Update clipping path
mMaskingClip.reset();
(...)
mMaskingClip.close();
}
#Override
public void dispatchDraw(Canvas canvas) {
int save = canvas.save();
canvas.clipPath(mMaskingClip);
super.dispatchDraw(canvas);
canvas.restoreToCount(save);
}
}
The code above works great, but the background Drawable that I've set for the same MaskedLinearLayout is not clipped to the path that I've defined (only its children).
How should I accomplish this? Some things that I've tried include overriding method onDraw() (with similar code shown in method dispatchDraw() above) and setting its background Drawable boundaries manually (Drawable#setBounds()), both without success.
Note that I cannot use static Drawables in this layout, because the latter should be able to be resized/masked dynamically while being displayed on the screen.
Thank you in advance for any answer.
I have a horizontal scrollview (which has a single view group inside, obviously) which I'm adding some views into when my activity is created. I want a view to start and end at the middle so I need to give some paddings to right and left respectively to the first and last view. Since the sizes are not measured at onCreate, I calculated them on onSizeChanged but the sizes of views are not calculated yet so no prevail there.
public void add(ArrayList<String> list) {
for(String string : list) {
View view = inflater.inflate(R.layout.textview_with_indicator, null);
views.add(view);
((TextView)view.findViewById(R.id.text)).setText(string);
content.addView(view);
}
invalidate();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(paddingSet && null != views && views.size() > 0) {
int paddingLeft = getWidth() - (views.get(0).getWidth()/2);
int paddingRight = getWidth() - (views.get(views.size()-1).getWidth()/2);
setPadding(paddingLeft, getPaddingTop(), paddingRight, getPaddingBottom());
paddingSet = true;
}
}
I think I need to observe the size change of views instead of whole content but I have no idea how to implement it. I tried to implement onGlobalFocusChanged to scrollview but could not make it work. Any ideas would be appreciated.
In Android I'm trying to make a Custom View where the user can sign, like the android finger paint api demo, and it has an overlay (essentially a TextView) that goes away when touched. I need to be able to add this view programmatically.
I couldn't figure out how to make this in an xml layout, so I added the overlay view in onSizeChanged.
It works but sometimes when I rotate the device twice rapidly it doesn't display even though it should. The rest of the View works fine. My View is called PaintOverlayView.
The relevant code is
private TextView overlay= new TextView(getContext());
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
...
LayoutParams wrapped = new LayoutParams(w,h);
overlay.setId(50);
overlay.setText("Click to draw, double click to clear");
overlay.setTextSize(20);
overlay.setGravity(Gravity.CENTER);
overlay.setX(this.getX());
overlay.setY(this.getY());
overlay.setLayoutParams(wrapped);
overlay.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("Touch Event");
v.setVisibility(View.GONE);
overlayed=false;
//System.out.println("overlayed on tocuh overlay "+overlayed);
return false;
}
});
ViewGroup Par =(ViewGroup)this.getParent();
Par.addView(overlay);
System.out.println("overlayed on Size Changed "+overlayed);
System.out.println("visibility "+overlay.getVisibility());
/*if (!overlayed)
overlay.setVisibility(View.GONE);
else
overlay.setVisibility(View.VISIBLE);
*/
}
I rotate it and sometimes the overlay doesn't show up even though ovarlyed is true and the visibility is still set to 0 (visible).
Edit: In case it helps I'm trying to add the PaintOverlayView to a RelativeLayout
I am trying to make square child inside RelativeLayout(which I extend). On activity start it works ok, but when ad is loaded, my relative layout gets smaller but my code does not affect child.
Code from extended RelativeLayout:
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int minSize = Math.min(w, h);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageViewContainer.getLayoutParams();
layoutParams.width = minSize;
layoutParams.height = minSize;
imageViewContainer.setLayoutParams(layoutParams);
invalidate();
}
Try to change the values overriding onMeasure method, also in Your code I would either called measure() or requestLayout() by myself
I have a HorizontalScrollView which contains a variable number of VISIBLE or GONE ListViews.
When a ListView's OnItemClickListener is called, part of the handling code changes the visibility of other ListViews.
As part of the OnItemClickListener I am trying to HorizontalScrollView.scrollTo() to show newly visible ListViews at the right hand side of the ScrollView.
scrollView.getRight() does not report a changed size of the scrollView's content after ListViews are made VISIBLE or GONE. It does report the correct new content size after the OnItemClickListener has returned.
Is there some form of OnReflowed callback that I've missed?
How can I do a .getRight() as soon as the layout is reflowed, after the OnItemClickListener has exited?
Solution was to:
1) insert a subclassed the (linear) layout between the ScrollView and the ListViews
2) add an onSizeChanged method to the subclass:
public void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
((HorizontalScrollView) getParent()).scrollTo(w, 0);
}