Add TextView to layout inside scrollView - android

I have a LinearLayout within a ScrollView in the xml file. I need to paint and write within the ScrollView so I've created a view in the layout and have been able to paint and write inside using: canvas.draw() and canvas.drawText(), my problem comes when writing text with canvas are not clearly the letters, so I need to add a TextView to the layout of the ScrollView from the class, without knowing very well how to do it.
Paste the code:
public class Calendario extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
MyView v = new MyView(this);
layout.addView(v,250,2000);
}
private class MyView extends View{
public MyView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
onDrawMethod();
//Here draw and write text//
}
}
}
A picture of what I need: I need only add textviews inside
Thankss

As I understand you, you want to add those texts programmaticaly, by saying
so I need to add a TextView to the layout of the ScrollView from the class
If so, might be this post will help you.

Related

CustomView not showing inside ScrollView

New Android programmer here.
I'm trying to display an .png image as a bitmap on Android. The only way I have been able to display the converted image at all is with a custom class that extends View. However, my image is too large to be displayed entirely on the screen and I would like to be able to scroll with it. But when I define a ScrollView and put the Canvas with the Bitmap into it, I get a blank screen. I had no luck setting this up with the layout file, so it is all done in the Activity class.
This is where the Activity is created:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll = new ScrollView(this);
scroll.addView(new CustomView(this));
setContentView(scroll);
}
And this is my CustomView class:
private class CustomView extends View{
public CustomView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas){
Bitmap bitmapMap = BitmapFactory.decodeResource(getResources(),R.drawable.resourceimage);
canvas.drawBitmap(bitmapMap,0,0,null);
this.setWillNotDraw(false);
}
}
And if I replace this line of code in my Activity: setContentView(scroll)
with this line: setContentView(new CustomView(this)), I can see the image, albeit not the entire image. So, is there a way to set this up in the layout files instead? Or is there something I'm missing that I need to declare in the ScrollView class?
EDIT: I would prefer not to use ImageView, because I would like to change the image in specific locations, and using bitmap seemed to be the easiest way to accomplish that, via x and y coordinates.
Your custom view needs to override the onMeasure method and properly set the measured width and height so that the parent views (in this case the ScrollView) can know how much space to allocate for the child.

Is important to use the setContentView(R.layout.name.xml) in android

Is important to use the setContentView(R.layout.name.xml) in android?
In the following code, I want to draw a line in relativeLayout Object without using the setContentView(R.layout.name.xml). It can compile Successfully. But it stops execution time.
Draw.java
class Draw extends View {
Paint paint;
public Draw(Context context) {
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawLine(10,10,100,100,paint);
super.onDraw(canvas);
}}
MainActivity.java
class MainActivity extends Activity {
Draw draw;
#Override
public void onCreate(Bundle s){
super.onCreate(s);
RelativeLayout relativeLayout = new RelativeLayout(this);
draw = new Draw(this);
relativeLayout.addView(draw,200,200);
}
}
setContentView() in Android must be called in the onCreate() method before getting any references to any views in the Activity.
Without setContentView() your activity won't be inflated with or assigned a layout.
As a result your app won't start because the start activity has no layout or UI to display.
So, always include a setContentView(), passing the layout as the parameter, in the onCreate() method of your activity.
Hope that helps.
What method setContentView does is not just setting resource of layout, but something more important. If it's a new activity, it will generate the window decor, apply the theme to your activity and so on. So you see, you cannot just dismiss this and replacing it with your code.
try add this code at the end of onCreate method
setContentView(relativeLayout);

Can I do addView in onDraw of MyLayout?

I am new for android. I want to add image button in onDraw. I wonder if I can do like this. I don't have compile error. But simulator says, "Unfortunately MyApp has stopped."
My second question is how can I add button at x, y location in screen?
There is no location parameters in addView.
protected class MyLayout extends LinearLayout {
ImageButton button;
public MyLayout(Context context) {
super(context);
setWillNotDraw(false);
button = new ImageButton(context);
button.setImageBitmap(buttonBitmap); // buttonBitmap is loaded in onCreate
}
public void onDraw(Canvas canvas) {
addView(button);
}
}
I would not be adding a button to a custom linearlayout in the onDraw() override function. onDraw is called periodically (for example when the element is resized). So you will be adding the same button EACH time onDraw() is called. And you will get an error if you add the same item more than once.
What exactly are you trying to do? If you want to just add a button to a linear layout, you can do so without extending a layout.

How do I position ZoomButtonsController in custom ImageView?

Layouting in Android is getting me rather perplexed.
I'm slowly implementing a custom ImageView where I'd like to make use of the ZoomButtonsController.
However, I would like to decide where the zoom buttons go in the layout and I can't figure out how to move them from the default bottom center position.
I have been experimenting with layouting simple views such as buttons in the main activity and this seems to be working as I would guess and expect.
In the case of the ZoomButtonsController I would however like to reposition them. I'm using a RelativeLayout as the mail layout and add the ZoomButtonsController within the custom ImageView.
The Activity code
public class ImageViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomImageView imageView = new CustomImageView(this);
relativeLayout.addView(imageView);
}
}
The CustomImageView code
public class CustomImageView extends ImageView {
private ZoomButtonsController mZoomButtons;
public CustomImageView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
mZoomButtons = new ZoomButtonsController(this);
mZoomButtons.getZoomControls();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
setLayoutParams(layoutParams);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d("TAG", "touch");
mZoomButtons.setVisible(true);
return true;
}
}
I've tested with WRAP_CONTENT in the parameters, but this only makes the zoom buttons disappear.
As a matter of fact, I couldn't position the ZoomButtonsController in any way and in the end had to accept the default placement.

How to add view components dynamically from activity

I am attempting to create a user interface dynamically. I have successfully create a view and loaded my background image. I have created two additional small view items to display on the background. My problem is that I have not been able to find any advice/instruction that tells me how to draw the small views. It seems that it should be a trivial exercise and I am guessing it is just finding the correct referencing. Hope someone out there can point me in the right direction.
Here is my Activity:
public class GhostActivity extends Activity implements OnTouchListener
{
private DrawView ghostView;
public Card mCard1, mCard2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// ToDo add your GUI initialization code here
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ghostView = new DrawView(this);
setContentView(ghostView);
//get the window size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Context context = getApplicationContext();
//create view items with initial positions
Point startPoint;
startPoint = new Point();
startPoint.x = 5;
startPoint.y = 3;
mCard1 = new Card(context, 1, R.drawable.bol_geel, startPoint);
startPoint.x = 5;
startPoint.y = 43;
mCard2 = new Card(context, 2, R.drawable.bol_rood, startPoint);
//now display them on the ghostView *****************HOW?
// set the callbacks
ghostView.setOnTouchListener(this);
mCard1.setOnTouchListener(this);
mCard2.setOnTouchListener(this);
}
and here is the View;
public class DrawView extends View
{
Drawable bg ;
public DrawView(Context context) {
super(context);
//setFocusable(true);
Drawable bg = this.getResources().getDrawable(R.drawable.bubbleblue480x800);
setBackgroundDrawable(bg);
}
#Override protected void onDraw(Canvas canvas) {
// canvas.drawColor(0x0000000); //if you want another background color
//draw on the canvas
}
}
edit: I believe my problem is needing to pass a pointer to the ghostView canvas. what makes me think that is if I create the children within ghostView then call their .draw method they appear exactly as I would expect.
#Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0x0000000); //if you want another background color
//draw the cards on the canvas
mCard1.draw(canvas);
mCard2.draw(canvas);
}
so at this point I am wondering how to get a reference pointer to the ghostView canvas.
To be honest I am finding the whole Activity - View relationship confusing.
Edit: I have taken a different approach based on detail in this tutorial
http://www.kellbot.com/2009/06/android-hello-circle/
It uses a FrameLayout and it seems I can achieve my objective.
To add view dynamically to view your class must extends from ViewGroup or LinearLayout class then you will able to call method addView.
Inside your ghost view first add a layout e.g Linear or Relative. Then only you could able to add views inside that layout you cant simply add a view to a xml file.
Or you can create a dynamic layout then only u can add view inside that layout.
RelativeLayout relative= new RelativeLayout(findViewById(R.id.your relativeLayoutID));
relative.addView(child);
child could be anything button textview and widget.

Categories

Resources