error overriding OnMeasure method - android

hi I'm working in my android app which plays videos and now I want to control my video to play in full screen I have a code but it contains errors it says that void is not valid type for variable onmeasure I dont know how to correct that
public class video extends Activity {
String SrcPath = "rtsp://v8.cache3.c.youtube.com/CjYLENy73wIaLQltaP8vg4qMsBMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOL6qv2DoMPrUAw=/0/0/0/video.3gp";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video1);
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
VideoView myVideoView = (VideoView)findViewById(R.id.vview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
}
}
thanks

The compilation problem is that there are bracket issues. Move
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
So it is not in onCreate(). Right now, methods can't directly enclose other methods.
Also, onMeasure() is not a method in Activity, but instead in View. You should review the documentation so you figure out exactly what you want to do.

It seems like you might want to have these two functions as methods in your class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video1);
// Set up the video when you create the activity
VideoView myVideoView = (VideoView)findViewById(R.id.vview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
}
// On measure should be a different method also in the class video
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

Related

Dynamic height viewpager for images (onMeasure() not working)

I have a custom ViewPager inside of an RecyclerView. I made wrap_content height of ViewPager and put onMeasure() function inside of CustomViewPager. But when I scroll down and up the RecyclerView, sometimes ViewPager is disappear. I look many SO answers but onMeasure() function not working every time for me. Method:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); //Deneme
int height = 0;
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
if (height != 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
ViewPager XML:
<com.monster.vpagerapp.Utils.ViewPagerFixedPost
android:id="#+id/postsViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Also I use this in constructor of RecylerView:
setHasStableIds(true);
And use this in MainFragment
recyclerView.getRecycledViewPool().setMaxRecycledViews(1, 0); //for unnecessary refresh

Extending a view which overrides onMeasure()

I have a view view1 which extends FrameLayout and overrides onMeasure() like this, so that it is rendered as a square:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
Now I want to create another view view2 which extends view1, but now I want view2 to have a height equal to the parent, and hence to cover the full screen. As of now, I get it as a square too.
How do I achieve this?
In view 1:
private boolean measureAsSquare = true;
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (measureAsSquare) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setMeasureAsSquare(boolean b) {
measureAsSquare = b;
}
In View2 constructor that subclasses view 1:
public View2(Context c) {
setMeasureAsSquare(false);
}
Then you can use normal layout weight and height for view 2 to assign its preferred size.
Since the onMeasure for Square was already overridden where it disregards the height passed to it, as per your code:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
One way to do what you want -- view2 which extends view1, but now I want view2 to have a height equal to the parent, and hence to cover the full screen -- is to also override onMeasure of view2 calling on setDimension like this...
public class Square2 extends Square {
public Square2(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}
Here is a sample after I tried it out. (For code in Square View, I just copied your code in onMeasure).
Using Square:
For Squre2:
Hope this helps.

Passing data from View To Activity

I need view width and height to use in some calculations in activity.
In a SO Q&A i saw
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
It works but my questions:
1)how can i pass it to activity?
2) Or should i put my CustomView as a inner class into the Activity file like this
public class MainActivity extends ActionBarActivity {
int parentHeight=0;
int parentWidth=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle aBundle=new Bundle();
MyView myView = new MyView(this);
Binary c=calculateCoordinates();
aBundle.putSerializable("coordinate", c);
myView.setData(aBundle);
myView.setBackgroundColor(Color.WHITE);
setContentView(myView);
}
public Binary calculateCoordinates(){
Random generator = new Random();
Binary c=new Binary();
c.setX(generator.nextInt(parentWidth-1));
c.setY(generator.nextInt(parentHeight-1));
return c;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_UP:
Intent intent = getIntent();
finish();
startActivity(intent);
break;
}
return true;
}
public class MyView extends View {
Bundle bundle = new Bundle();
Paint paint = new Paint();
public MyView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
Binary c = (Binary)bundle.getSerializable("coordinate");
canvas.drawRect(c.getX(), c.getY(), c.getX()+1, c.getY()+1, paint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
parentWidth = MeasureSpec.getSize(widthMeasureSpec);
parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setData(Bundle bundle){
this.bundle=bundle;
}
}
}
But if so i can not know when measurement have been got exactly, and if it has been got before i call the coordinate calculation.
Activity Class
add this to your OnCreate code.
final ViewTreeObserver obs = myView.getViewTreeObserver();
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw () {
int height = myView.getHeight();
int width = myView.getWidth();
}
when the View will be drawn this function will be called with the correct width and height.
The OnMeasure function within your view will get called once the View has been placed on your actual page.
So let's say you want to get the Width and the Height when you click a button or something and want to pass those values over to another activity.
Put your view in your XML file:
<your.namespace.app.MyView
xmlns:app="http://schemas.android.com/apk/res/novoda.tastecard"
android:id="#+id/myView"
android:layout_width="match_parent"
...../>
Then reference your View in your activity:
mMyView = findViewById(R.id.myView);
Then you can then just do:
int width = myView.getWidth();
int height = myView.getHeight();
That should return you the width and the height of the view.
Then do the following to call the new Activity with your values:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("width", width);
intent.putExtra("height", height);
startActivity(intent);

Android onMeasure change View's parameters twice

I've created a new view (extends ImageView) and override onMeasure method.
Xml code:
<by.bsuir.klondike.mainCardsView
android:id="#+id/imageview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitEnd"
android:baselineAlignBottom="true"
android:src="#drawable/cardsmain"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
At first I overrode a method like this
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
width = parentWidth/2;
height = 210*width/498;
this.setMeasuredDimension(width, height);
this.setLayoutParams(new *ParentLayoutType*.LayoutParams(width,height));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
According to Android developer onMeasure calls twice. On it's first call I receive parentWidth == 480 and parentHeight == 320. But on second call I receive parentWidth == 240 and parentHeight == 101.
It turns out, that the size is changing twice.
I've solve this problem using bool flag and change dimension only on first call.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (flag){
flag=false;
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
width = parentWidth/2;
height = 210*width/498;
}
this.setMeasuredDimension(width, height);
}
But I think that it's a wrong way.
How can I do it right?

onMeasure not getting called in my custom viewgroup android

Im having two custom viewgroups, superViewGroup and subViewGroup. The subviewgroup contains views. Im adding my superviewgroup to a linearLayout and the subViewGroups to my superviewgroup.
The superviewgroup onMeasure() is getting called but not in the subviewgroup. but in both cases onLayout() method is getting called.
The code as follows
public class SuperViewGroup extends ViewGroup{
public SuperViewGroup(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
public class SubViewGroup extends ViewGroup{
public SubViewGroup(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUB VIEWGROUP");
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
Comments are appreciated. thanks in advance.
Because you have to actually pass the measure to the children views:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
//Make or work out measurements for children here (MeasureSpec.make...)
measureChild (child, widthMeasureSpec, heightMeasureSpec);
}
}
}
Otherwise you never actually measure your children. It is up to you to decide how to do this. Just because your SuperViewGroup is in a linear layout, your SuperViewGroup takes on responsibility to measure its children.

Categories

Resources