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>
Related
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'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
I have tried to pass on an int without success from this activity:
public class SecondActivity extends Activity {
private int sign=3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(newView);
}
}
to this view:
public class TestView extends View {
private int sign;
public TestView(Context context, int signValue) {
super(context);
this.sign=signValue;
}
}
But the value isn't passed on, the variable "sign" remains null. Am I missing something? What?
(In my code the variable sign gets its value from a bundle, but I tested it and know it's getting the right value.)
EDIT:
Still don't know what was wrong with the previous code, but I solved it by creating a setSign method.
public class SecondActivity extends Activity {
private int sign=3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
newView.setSign(sign);
setContentView(newView);
}
}
to this view:
public class TestView extends View {
private int sign=0;
public TestView(Context context) {
super(context);
}
public void setSign(int signValue) {
sign=signValue;
}
}
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"/>
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..........