I'd like to know how to create buttons, textViews etc. in an Activity, which doesn't use an xml file as ContentView.
What I mean:
Main Class:
protected void onCreate(Bundle savedInstanceState)
{
//...
puzzleView = new PuzzleView(this);
setContentView(puzzleView);
}
The PuzzleView class:
public class PuzzleView extends View
{
private final Game game;
public PuzzleView(Context context)
{
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
#Override
protected void onDraw(Canvas canvas)
{
// drawing some things here
}
}
You probably want to set your content view to a class that extends ViewGroup, then add views to that class. Layouts are probably what you're after, in particular. Here are some tutorials:
http://blogspot.arcintechnologies.com/android/generate-android-layout-programmatically/
http://mainerrors.blogspot.ca/2011/02/programmatically-creating-layout-part-1.html
Related
I have a Custom view activity and I want to show in it an advert. Just a banner view on the bottom of the screen. I am using Appodeal's API for ads.
Here is what I tried so far:
public class main extends AppCompatActivity {
BannerView bannerView;
View customView;
public static void loadAd(Context context, Activity activity) {
Appodeal.setTesting(true);
Appodeal.disableNetwork(context, "cheetah");
Appodeal.disableLocationPermissionCheck();
Appodeal.setBannerViewId(R.id.appodealBannerView);
Appodeal.initialize(activity, context.getString(R.string.app_key), Appodeal.BANNER_VIEW);
Appodeal.show(activity, Appodeal.BANNER_VIEW);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new CustomView(this, null);
setContentView(customView);
bannerView = new BannerView(this, null);
bannerView.setId(R.id.appodealBannerView);
}
#Override
protected void onResume() {
super.onResume();
loadAd(this, this);
}
class CustomView extends View {
CustomView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
bannerView.draw(canvas);
invalidate();
}
}
}
Any help will be appreciated!
U can use xml layout as below for example:
<RelativeLayout
...>
<PathToCustomView.CustomView
...
/>
<PathToBannerView.BannerView
android:below="+id/myCustomviewId"
...
/>
</RelativeLayout>
I am working on an android application with a DrawingView.
I would like to have a button which clears the drawing view, however I cannot figure out how to make a button that calls a method in somewhere that's not the MainActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
The above text is the only code in the main activity, so I do not have access to a reference of the drawingView.
public DrawingView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
AsyncTask myTask = new AsyncTask() {
#Override
protected Object doInBackground(Object... objects) {
try {
DrawingView dView = (DrawingView) objects[0];
dView.connection = new Connection((DrawingView) objects[0], (Context) objects[1],new Socket("128.199.236.107", 3333));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
Object[] myObjects = new Object[2];
myObjects[0] = this;
myObjects[1] = context;
myTask.execute(myObjects);
getRes();
setupDrawing();
This is the constructor of the class DrawingView.java
There is a method in this class called "refresh", and I am trying to call it using a button. Is there any way I can do this?
The refresh method looks like this
public void refresh(){
Canvas.drawColor(0, Mode.CLEAR);
}
The MAIN ACTIVITY refresh method (called on the button press), looks like this
public void refresh(View v){
final DrawingView DV = (DrawingView) findViewById(R.id.drawing);
DV.refresh();
}
Logcat? I think
Make your shared method static by adding the "static" keyword:
public static void myFunction()
Then use:
Another Activity.myFunction();
One simpe way to cleanup your canvas is to insert this code on the OnDraw method:
Paint p=new Paint();
p.setARGB(255,100,120,140); // your background color
Rect r =new Rect();
canvas.getClipBounds(r);
canvas.drawRect(r,p);`
but it's better to perform the allocations of r and p outside of onDraw.
I have a class SomeView that extends View and that is displayed in a class Controls that extends linear layout.
The linear layout is instantiated onCreate of an activity.
I would like to call a method in the activity every time I click on this view SomeView.
I have tried to set an onClickListener in the activity like this
public class MainActivity extends AppCompatActivity implements
SomeView.OnClickListener {
private Controls menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menu = new Controls(this);
menu.getSomeView().setOnClickListener(this);
setContentView(menu);
}
#Override
public void onClick(View view) {
System.out.println("Hello");
}
}
The controls class looks like this
public class Controls extends LinearLayout {
private SomeView aview;
public Controls(Context context) {
super(context);
this.setOrientation(LinearLayout.HORIZONTAL);
aview = new SomeView(context);
this.addView(aview, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
public SomeView getSomeView() {
return aview;
}
}
and the SomeView class looks like this (it just draws an oval)
public class SomeView extends View {
public SomeView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF aRect = new RectF();
aRect.left = getPaddingLeft();
aRect.top = getPaddingTop();
aRect.right = getWidth() - getPaddingRight();
aRect.bottom = getHeight() - getPaddingBottom();
Paint aPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
aPaint.setColor(Color.GREEN);
canvas.drawOval(aRect, aPaint);
}
}
But I am missing something because clicks are not calling the onClick method.
What else do I need to set up?
it seems like you did only mistake in your MainActivity class, where you forgot to call the super method. Try doing this, hope it will work, since it works from here in my mobile.
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menu = new Controls(this);
menu.getSomeView().setOnClickListener(this);
setContentView(menu);
}
And in your callback method, instead using System.out.println(), use Log.d() as below:
#Override
public void onClick(View view) {
Log.d(TAG, "Hello");
}
and it is working from here, look at the image below as well.
I am trying to call the Log.i method to test the refresh rate for the View:
public class GameView extends View {
public GameView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Log.i("OnDraw","ping");
invalidate();
}
Why doesn't this work?
public class GameUI extends Activity implements OnClickListener, OnTouchListener {
/** Called when the activity is first created. */
GameView gameview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameview = new GameView(this);
setContentView(gameview);
}
Try this
public GameView(Context context)
{
super(context);
this.setWillNotDraw(false);
}
This may help you..........
Hi i want create simple apps.i use the one customclass which name like a Drawcanvas which purpose to draw runtime canvas.so i use the ontouchListener and OnClicklistener onthis. but those event can`t working.my code is below.
this the class where i use the Custom class name like DrawCanvas
public class CanvasExample extends Activity
{
/** Called when the activity is first created. */
RelativeLayout relMainOperationLayout;
RelativeLayout relTabHeader;
RelativeLayout relMidalLayout;
RelativeLayout relBelowLayout;
Context myContext;
DrawCanvas drawCanvas;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myContext=CanvasExample.this;
LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layoutId = myContext.getResources().getIdentifier("main","layout",getPackageName());
relMainOperationLayout = (RelativeLayout) layoutInflater.inflate(layoutId,null);
relTabHeader=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relHeadLayout);
relMidalLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relmidalLayout);
relBelowLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relBelowLayout);
drawCanvas=new DrawCanvas(CanvasExample.this,myContext);
drawCanvas.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,400);
drawParams.addRule(RelativeLayout.BELOW, relTabHeader.getId());
//relMidalLayout.addView(drawCanvas,drawParams);
relMainOperationLayout.addView(drawCanvas,drawParams);
setContentView(relMainOperationLayout);
}
And This is my CustomClass code which extend View. Name DrawCanvas
public class DrawCanvas extends View implements View.OnTouchListener,View.OnClickListener
{
Context drawContext;
Activity drawActivity;
public DrawCanvas(Activity activity,Context context)
{
super(activity);
this.drawActivity=activity;
this.drawContext=context;
}
#Override
public void onClick(View v)
{
System.err.println("Click Here");
Toast.makeText(drawContext, "Click ", 1000).show();
}
#Override
public boolean onTouch(View v, MotionEvent event)
{
System.err.println("Touch Here");
return true;
}
}
i am new in canvas.
You have to use setOnTouchListener and setOnClickListener (though I don't think click events will help you) to register the click and touch events for your View.
public DrawCanvas(Activity activity,Context context) {
super(activity);
this.drawActivity=activity;
this.drawContext=context;
setOnTouchListener(this);
setOnClickListener(this);
}