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?
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>
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've seen lots of examples using TextureView in a main Activity but I'm trying to put it into a Fragment.
I've created the simplest example possible and its onCreateView is being called, onActivityCreated as well but onSurfaceTextureAvailable isn't being called after passing back the TextureView.
What am I missing ?
Thanks
G
public class FullscreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
}
}
public class TextureViewFragment extends Fragment
implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTextureView = new TextureView(getActivity());
mTextureView.setSurfaceTextureListener(this);
mTextureView.setOpaque(false);
return mTextureView;
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// TODO Auto-generated method stub
}
}
activity_fullscreen.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".FullscreenActivity" >
<fragment class="com.example.test.TextureViewFragment"
android:id="#+id/graphTextureView"
android:layout_width="0px" android:layout_height="match_parent" />
</FrameLayout>
This is because the TextureView must have a nonzero size and be visible. Based on the comment, the layout_width was set to 0px. Change that to a nonzero value.
(Old question but posting answer for posterity)
You should add 'android:hardwareAccelerated=true' to your AndroidManifest.xml.
TextureView needs hardware acceleration.
Your TextureView should be in a layout xml file. Instead of using new to instantiate it you should get it using something like this:
View view = inflater.inflate(R.layout.layout_video, container, false);
mTextureView = (TextureView) view.findViewById(R.id.video_texture_view);
Then in your layout_video.xml file you need a TextureView with id video_texture_view
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.
I'm a newbie to OpenGL on Android and last week I described a problem failing to get the renderer to start for a GLSurfaceView declared in the layout. It starts fine if I declare the renderer in the Activity class and setContentView to it. Here's a simplifed version with all the source code. What am I doing wrong?
Layout . . .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/dummy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="a Button" />
<FrameLayout
android:id="#+id/framelay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.test.rendertest.RTSurface
android:id="#+id/RTSurfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
Activity class. UNcomment the //A's, and comment-out the //B's and the renderer runs. But as shown below the renderer does not run even though its constructor gets called.
public class RenderTest extends Activity {
RTSurface myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// myView = new RTSurface(this); // A
// setContentView(myView); //A
setContentView(R.layout.main); // B
myView = (com.test.rendertest.RTSurface)findViewById(R.id.RTSurfaceView); //B
}
#Override
protected void onPause() {
super.onPause();
myView.onPause();
}
#Override
protected void onResume() {
super.onResume();
myView.onResume();
}
}
GLSurfaceView . . .
class RTSurface extends GLSurfaceView {
private final RTRenderer renderer;
public RTSurface(Context context) {
super(context);
Log.i("rendertest", "RTSurface constructor - Default Form");
renderer = new RTRenderer();
setRenderer(renderer);
}
public RTSurface(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("rendertest", "RTSurface constructor - Layout Form");
renderer = new RTRenderer();
setRenderer(renderer);
}
}
. . . and the Renderer (just stubs)
class RTRenderer implements GLSurfaceView.Renderer {
public RTRenderer () {
// a constructor just to have somewhere to set
// breakpoints and logcat messages
Log.i("rendertest", "RTRenderer Constructor");
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.i("rendertest", "onSurfaceCreated in RTRenderer");
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.i("rendertest", "onSurfaceChanged in RTRenderer");
}
public void onDrawFrame(GL10 gl) {
Log.i("rendertest", "onDrawFrame in RTRenderer");
}
}
Thanks in advance!!
You didn't specify your LinearLayout orientation, so it is set to horizontal by default. This means your GLSurfaceView is outside of the screen (because you set your button width to fill_parent).
Just add the following attribute to your LinearLayout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">