This is the java part of the code
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
class surf extends View {
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
And the resource file for the same without other views
<view
class="tictac.Tictac.MainActivity$surf"
android:id="#+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
It closes as soon as it opens with a error, I have been following the tutorial in the android website
Try this code. Here I am adding own view as main view.
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new surf(this));
}
class surf extends View {
public surf(Context c)
{
super(c);
}
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
Or you can add your view in XML
<com.my.package.MyClass
android:id="#+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
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 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 have some methods for drawing simple objects using canvas. I need use use it together with xml file storing layout. Here is my code:
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new yourCustomView(getApplicationContext());
}
private class yourCustomView extends View {
public yourCustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
}
}
}
and there is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<package.name.here.yourCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Why it does not working properly?
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..........
Here is my program.
public class MathsExplorer extends Activity {
/** Called when the activity is first created. */
private GraphicsView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
}
}
public class GraphicsView extends View {
public GraphicsView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
String background = "/Maths Explorer/bin/res/drawable/MC900436279.jpg";
Bitmap b = BitmapFactory.decodeFile(background);
canvas.drawBitmap(b, 10, 10, null);
}
}
It quits right away with the error "Unfortunatly, Maths Explorer has stopped."
Can anyone help me? I'm not sure, I'm just trying to display an image.
I think that path to file is wrong, you can't do that.
Try this.