How to setContentView to a new class that extends view android - android

I have two classes, a class called Menu and a class called circleGame. When I run my application the Menu class is started and creates a rectangle on the screen. When the user taps the screen I would like my program to switch to my circleGame class (the circleGame class has the exact same code as my Menu class except it draws a circle rather than a rectangle). I wanted to do this using the Activity.setContentView method, but when I try using that method I get an error saying, "Cannot make a static reference to the non-static setContentView(View) from the type Activity." My code is shown below:
public class Menu extends View{
Paint blue = new Paint(), black = new Paint();
Display display;
//Width of the user's screen.
int screenWidth;
//Height of the user's screen.
int screenHeight;
public Menu(Context context) {
super(context);
blue.setColor(Color.BLUE);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
black.setColor(Color.BLACK);
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//Setting the background color to white.
canvas.drawColor(Color.WHITE);
canvas.drawRect(0, screenHeight/2, screenWidth, screenHeight/1.5f, blue);
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if(event.getAction() == event.ACTION_UP){
circleGame test = new circleGame(getContext());
Activity.setContentView(test);
}
return true;
}
}
My error occurs on the line in the onTouchEvent Listener where I wrote: Activity.setContentView(test); I know I can just put the two codes together to make one large file, but then my code would get two confusing if I just have this one large file later on when I start having a lot of code.

As error states, setContentView is not static method, but rather instance method of Activity. You can only setContentView on some particular activity instance.
If you creating your Menu instance from code of activity, you can pass Activity instance in constructor, and use it later. However it would be better to redesign your code to use Observer Pattern.

Related

dynamic xml layout rotation

I have two buttons on my main activity and when pressed I want to call the exact same activity exept that on one the layout is rotated 180 degrees. Is there any way to do this (like rotate based on an extra passed from main activity) do I really need to create two different activities with practically the same code in their respective java file?
EDIT: when I say exact same activity I don't mean the same as main. It's just the same for those two buttons...
I think i found a solution for you. Use a custom reverse view as a wraper to reverse the whole layout.
Try to run the sample code below.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layoutId = R.layout.main;
View content = getLayoutInflater().inflate(layoutId, null);
ReverseView wraper = new ReverseView(this);
wraper.addView(content);
setContentView(wraper);
}
public class ReverseView extends FrameLayout {
public ReverseView(Context context) {
super(context);
}
#Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
int px = getWidth() / 2;
int py = getHeight() / 2;
canvas.rotate(180, px, py);
super.dispatchDraw(canvas);
canvas.restore();
}
}
}
You can change the orientation in your activity by adding the following code.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
And you can also create a new layout xml in layout-land/, and when your activity enter the landscape mode, it will try to load the different layout.

Drawing a line with canvas from an Activity

I have read the following links:
How to draw a line in android
http://marakana.com/s/android_2d_graphics_example,1036/index.html
http://developer.android.com/reference/android/graphics/Canvas.html
Activity.setContentView, View.setContentView?
How to create an activity without 'setContentView(R.layout.main)'
http://www.jayway.com/2009/03/26/layout-resources-in-android/
I have read the previous articles that talk about about my problem but I can't solve my problem.
My problem is that I want to draw straight lines to simulate the connection between images, but basically is to paint straight lines.
All the examples I've seen have the main class that inherits from the activity and have another inner class that inherits from the View. And in the main class set the user interface that contains the View class, so we have a empty user interface.
Something like this:
public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView view1 =new MyView(this);
setContentView(view1);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
}
//And more code
}
I want to set my user interface (xml file) in the class that inherits from the activity (as usual):
public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.union);
}
}
And I want from the class that inherits from activity to draw straight lines, I don't want to have any class that inherits from View.
I tried the following but I don't see any line:
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawLine(0, 0, 300, 700, paint);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
In the previous method I want to draw a line from the corner 0,0 to the corner 300,700. It's a simple test. But I don't draw anything and I don't know why.
You can extend a view and override the onDraw and manage all your lines there.
Then, you can add this view (that can be full screen) in another xml (that can be used in an Activity) like this:
<my.package.myView
android:layout_width=#"fill_parent"
android:layout_height=#"fill_parent"/>
Be sure that the layout you use in your main activity is a RelativeLayout so you can fill the entire screen AND be on the top of your views. To place your view on top of your views be sure that you have your view in the end of your activity xml.

Getting the width/height of a layout in Android

I'm wondering how to measure the dimensions of a view. In my case it is aan Absolute Layout. I've read the answers concerning those questions but I still don't get it.
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
drawOval();
}
public void drawOval(){ //, int screenWidth, int screenHeight){
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
int screenWidth = layoutbase.getWidth();
int screenHeight = layoutbase.getHeight();
Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);
Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);
...some code ...
((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));
mybutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showText(mybutton);
}
});
}
public void showText(View button){
int x = findViewById(LAYOUT).getWidth();
int y = findViewById(LAYOUT).getHeight();
Toast message = Toast.makeText(this, "x: " + x , Toast.LENGTH_SHORT);
message.show();
}
The getWidth() command works great in showText() but it does not in drawOval(). I know it looks a bit different there but I also used the int x = findViewById(LAYOUT).getWidth(); version in drawOval(), and x/y are always 0. I don't really understand why there seems to be no width/height at that earlier point. Even if I actually draw a Button on the Absolute Layout, getWidth() returns 0. Oviously I want to measure the sizes in drawOval().
I think will help you.
LinearLayout headerLayout = (LinearLayout)findviewbyid(R.id.headerLayout);
ViewTreeObserver observer = headerLayout .getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
int headerLayoutHeight= headerLayout.getHeight();
int headerLayoutWidth = headerLayout.getWidth();
headerLayout .getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});
}
getWidth() is giving you 0 because onCreate is called before layout actually happens. Due to views being able to have dynamic positions and sizes based on attributes or other elements (fill_parent for example) there's not a fixed size for any given view or layout. At runtime there is a point in time (actually it can happen repeatedly depending on many factors) where everything is actually measured and laid out. If you really need the height and width, you'll have to get them later as you've discovered.
This specially deal with Dimensions so
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
This may help you in managing dimensions.
Note: This returns the display dimensions in pixels - as expected. But the getWidth() and getHeight() methods are deprecated. Instead you can use:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
as also Martin Koubek suggested.
If your goal is to simply draw an oval on the screen, then consider creating your own custom View rather than messing around with AbsoluteLayout. Your custom View must override onDraw(android.graphics.Canvas), which will be called when the view should render its content.
Here is some extremely simple sample code that might help get you started:
public class MainActivity extends Activity {
private final Paint mPaint = new Paint();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
// create a nested custom view class that can draw an oval. if the
// "SampleView" is not specific to the Activity, put the class in
// a new file called "SampleView.java" and make the class public
// and non-static so that other Activities can use it.
private static class SampleView extends View {
public SampleView(Context context) {
super(context);
setFocusable(true);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
// smoothen edges
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4.5f);
// set alpha value (opacity)
mPaint.setAlpha(0x80);
// draw oval on canvas
canvas.drawOval(new RectF(50, 50, 20, 40), mPaint);
}
}
}
This give you screen resolution:
WindowManager wm = (WindowManager)context.getSystemService(context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point outSize = new Point();
display.getSize(outSize);
kabuko's answer is correct, but could be a little more clear, so let me clarify.
getWidth() and getHeight() are (correctly) giving you 0 because they have not been drawn in the layout when you call them. try calling the two methods on the button after addView() (after the view has been drawn and is present in the layout) and see if that gives you the expected result.
See this post for more information.

invalidate only works in custom view

so i created a view called "drawable view"
class DrawableView extends View{
Context mContext;
int touches=0,k,Xoffs,clicks=0;
double x_1 = 0,x_2=0;
private float mLastTouchX, mLastTouchY;
public DrawableView(Context context) {
super(context);
mContext = context;
}
....
#Override
protected void onDraw(Canvas canvas){
Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas.drawColor(Color.BLUE);
myPaint.setColor(Color.WHITE);
canvas.drawCircle(200, 100, 20, myPaint);
}
..... more code....
}
and it can only be invalidated within the ondraw command! ie: calling "invalidate();" at the end of the ondraw command causes it to loop.
I have tried many times to call g_draw.invalidate(); or g_draw.postInvalidate(); (g_draw is the name of the created Drawable View)from other classes and even the main activity class and it doesnt work. why and how can i fix it?
thanks
If you want continious onDraw invoking try doing it in another thread. Create a thread, and from its run method try doing postInvalidate.
It always worked for me.
Another thing is that when you draw a circle once, next time wont make any difference - it will look the same.
You may want to call invalidate() somewhere in your DrawableView class. For example, if you want your view to redraw itself after any touch event, you would do something like this:
public boolean onTouchEvent( MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
invalidate();
}
}
This is how I draw the movable pieces in my puzzle game.

How-to create a clickable Button with a ShapeDrawable?

I'm trying to draw a shape on a Canvas and get information about the clicks that the user performs on that Canvas.
I created a class which extends the Button class.
class CustomDrawableButton extends Button {
private final ShapeDrawable mDrawable;
int x = 50;
int y = 50;
int width = 30;
int height = 30;
public CustomDrawableButton (Context context) {
super(context);
mDrawable = new ShapeDrawable (new OvalShape ());
mDrawable.getPaint().setColor(Color.GREEN);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
Then, in a class which also extends View, I added the instantiation and a listener:
CustomDrawableButton mCustomDrawableButton = new CustomDrawableButton(getBaseContext());
layout.addView(mCustomDrawableButton);
mCustomDrawableButton.draw(canvas);
mCustomDrawableButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
System.out.println("Yey clicked");
Toast.makeText(view.getContext(), "Yey", Toast.LENGTH_LONG).show();
}});
When this is executed, it is not able to detect the clicks on the image / button. I have read that ShapeDrawable objects can't be "clickable", but I was expecting this to work since I'm extending the Button (which is a View and is "clickable").
Is this possible? If not this way, can you direct me to some way to get information about the screen coordinates that were pressed on a Canvas or a View ?
Thanks in advance.
After some searching here's a tutorial that shows how to do it using by getting the touched screen position.
http://marakana.com/tutorials/android/2d-graphics-example.html
Still don't know how to do it by automatically binding the touched event to a button. If anyone knows how to do it, please say so.
Thanks.

Categories

Resources