TextView not appearing on canvas - android

I want to add a textView on a relative layout dynamically, and i want to show it on a canvas. I have tried out the following code. Please help me to find out what's wrong!. Thanks in advance.
public class MainActivity2 extends Activity
{
Paint p= new Paint();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View myView= new Panel(this);
setContentView(myView);
p.setColor(Color.BLUE);
}
class Panel extends View
{
RelativeLayout rl= new RelativeLayout(getApplicationContext());
TextView tv = new TextView(getApplicationContext());
public Panel(Context context) {
super(context);
tv.setText("Helllllloo");
rl.addView(tv);
}
#Override
public void onDraw(Canvas canvas)
{
rl.draw(canvas);
canvas.drawText("helllo canvas!1!!!!!!!", 0, 100, p);
}
}
}

I solved it by adding seperate views to relative layout and set contentview as that relative layout!

Related

Changing the contents of the same Java Activity by pressing the button

When I run the following code, first I see a red rectangle.
But I want to see a green rectangle first, and then when I press the button, next see a red rectangle in the same activity.
What is the solution?
MainActivity.java
public class MainActivity extends Activity {
Button button;
Draw draw;
Draw2 draw2;
RelativeLayout linearLayout;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.t);
button = (Button) findViewById(R.id.button);
draw = new Draw(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
draw.setLayoutParams(layoutParams);
linearLayout.addView(draw);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Draw2.class);
startActivity(intent);
}
});
draw2 = new Draw2(this);
draw2.setLayoutParams(layoutParams);
linearLayout.addView(draw2);
}
}
Draw.java
public class Draw extends View {
Paint paint;
public Draw(Context context) {
super(context);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
#Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}}
Draw2.java
public class Draw2 extends View {
Paint paint;
Draw2(Context context){
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas){
paint.setColor(Color.RED);
canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}
}
Draw2 is just a view not an activity,so Change MainActivity like this
public class MainActivity extends Activity {
Button button;
Draw draw;
Draw2 draw2;
RelativeLayout linearLayout;
ViewGroup.LayoutParams layoutParams;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.container);
button = (Button) findViewById(R.id.button1);
draw = new Draw(this);
layoutParams = new ViewGroup.LayoutParams(200, 200);
draw.setLayoutParams(layoutParams);
linearLayout.addView(draw);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
draw2 = new Draw2(getApplicationContext());
draw2.setLayoutParams(layoutParams);
linearLayout.addView(draw2);
}
});
}
}

view in xml layout

I've created my own view and I can't figure out how to add it from the main.xml layout file
setContentView(vi); its work but ı want setContentView(R.layout.activity_main);
public class Gui extends View {
public Gui(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
Paint myPaint = new Paint();
Paint myPaintFill= new Paint();
myPaintFill.setColor(Color.GREEN);
myPaint.setColor(Color.BLACK);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(1);
RectF r = new RectF(0,0,50,50);
canvas.drawRoundRect(r, 0, 0, myPaint);
}
}
}
You have to add this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Because your custom View is an inner class in your Activity the java compiler will output the name ActivityName$GraphicsView for that class. You can't use that name directly as the View name in the xml layout because of the $ character but you can do it like this:
<view
class="com.package.here.ActivityName$GraphicsView"
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
where ActivityName is the name of the activity where your GraphicsView class is declared.

Drawing several layers onto ImageView

I'm trying to follow instructions from Android developers site but I must be doing something wrong.
I tried to create custom ImageView and draw 2 bitmaps onto it.
Activity
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout left = (LinearLayout) findViewById(R.id.container);
ModuleImageView iv = new ModuleImageView(this);
left.addView(iv);
iv.invalidate();
}
}
ImageView
public class ModuleImageView extends ImageView{
public ModuleImageView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.main_engine);
Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.energy);
canvas.drawBitmap(b1, 0, 0, null);
canvas.drawBitmap(b2, 5, 5, null);
super.onDraw(canvas);
}
}
There is nothing showing up on the screen and it's probably because the onDraw method is never executed.
Use LayerDrawable instead. Construct it and pass to a normal ImageView

add view to scrollview in android

I have a relative layout inside a scrollview, then i created a new view with this code:
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(5, 5, 140, 140, paint);
}
}
Then i try to use layout.addView(DrawView) to add the new view to the relative layout, so it can be scrollable with the rest of the content, but is doesn't work, nothing shows up..
Am i missing something ?
Edit:
DrawView formas;
GifMovieView gif;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout layout = new RelativeLayout(this);
final ScrollView scrollView = new ScrollView(this);
//gif = new GifMovieView(this, t_img);
formas = new DrawView(this);
layout.addView(formas);
scrollView.addView(layout);
this.setContentView(scrollView);
inicializar();
load(layout);
}
You will probably want to override onMeasure() in your DrawView. Otherwise your view will have size of 0x0 pixels.
You can start with something as simple as
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(200, 200);
}
just to prove it works, but you will need to do some more meaningful, based on what are contents of your view.
Start from this article to see help on overriding onDraw() and onMeasure()

Cannot render an xml layout with LayoutInfalter. What I'm doing wrong?

I need to show a layout from xml file and draw some vector based image on top of that. If code below is executed then everything is shown but the layout. Why? What I'm doing wrong?
public class About extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.about);
setContentView(new GraphicsView(this));
}
public class GraphicsView extends View
{
public GraphicsView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
Context context;
context=getContext();
View aview;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
aview = inflater.inflate(R.layout.about, null);
aview.draw(canvas);
Path circle = new Path();
Paint cPaint = new Paint();
cPaint.setColor(Color.LTGRAY);
circle.addCircle(150, 150, 100, Direction.CW);
canvas.drawPath(circle, cPaint);
cPaint.setColor(Color.RED);
canvas.drawTextOnPath("Some stupid text", circle, 0, 20, cPaint);
}
}
}
After inflating the View you need to measure it and lay it out to give it a size so it can be properly drawn. You should call aview.measure() and aview.layout() before calling aview.draw().
Note that you should NEVER inflate views from onDraw(). It is very expensive and wasteful. Similarly, you should NEVER create paints or paths inside onDraw().
There is an another answer from another forum:
FrameLayout:
public class About extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
GraphicsView drawing = new GraphicsView(this);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
container.addView(drawing);
}
public class GraphicsView extends View
{
public GraphicsView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
Path circle = new Path();
Paint cPaint = new Paint();
cPaint.setColor(Color.LTGRAY);
circle.addCircle(150, 150, 100, Direction.CW);
canvas.drawPath(circle, cPaint);
cPaint.setColor(Color.RED);
canvas.drawTextOnPath("Some stupid text", circle, 0, 20, cPaint);
}
}
}
And then here comes XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:id="#+id/container">
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</FrameLayout>

Categories

Resources