I'm trying to use SurfaceView and want to paint on it, use code:
public class TestPaint extends SurfaceView {
public TestPaint(Context context) {
super(context);
setWillNotDraw(false);
setBackgroundColor(color.white);
}
#Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(color.black);
canvas.drawText("test", 10, 10, p);
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestPaint p=new TestPaint(this);
p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout rl=(LinearLayout) findViewById(R.id.rltMain);
rl.addView(p);
}
}
The TestPaint display black background only. I try Why do I get it?
Using paint class better then that you have use frame layout and set text view or whatever u want.
Check this,
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</SurfaceView>
</LinearLayout>
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="test"
android:background="#color/abc_search_url_text_normal"
android:textColor="#android:color/black"
android:textSize="20sp" />
</FrameLayout>
Related
I am programming a compass android app
now I'm facing a problem that I want to add a canvas in the main XML file with a back button i designed to let the user go back to the menu
I used the addview() try to add the canvas compass into the main.xml but still error
the error was an NULLPOINTEREXCEPTION on "mainLayout.addView(compassView);" at the MAIN.JAVA code
here is my code
MAIN.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compassView = new MyCompassView(this);
setContentView(compassView);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.compasslayout);
LayoutParams imageViewLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
compassView.setLayoutParams(imageViewLayoutParams);
mainLayout.addView(compassView);
MyCompassView.java
public class MyCompassView extends View {
private Paint paint;
private float position = 0;
public MyCompassView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeMiter(position);
}
XML FILE
<LinearLayout 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:orientation="vertical"
tools:context=".Compass" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="#+id/compasslayout">
<Button
android:id="#+id/buttona"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/b_select" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp">
</LinearLayout>
</LinearLayout>
Please help me, I stuck in this already a day and I can't continue without finish this task
The problem is here:
compassView = new MyCompassView(this);
setContentView(compassView);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.compasslayout);
You are setting a new MyCompassView as the content view instead of your XML file. This means that when you call findViewById(), the View of id R.id.compasslayout cannot be found. Your call to setContentView() should be setContentView(R.layout.mylayout).
I'm trying to make simple app that displays rectangle and has ability to change its color by button.
Rectangle class is:
public class DrawView extends View{
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.YELLOW);
canvas.drawRect(300, 550, 150, 400, paint );
}
public void setColorRed()
{
paint.setColor(Color.RED);
invalidate();
}
My app is Tab Layout app. This class is displayed in 3rd Tab this way:
main.xml
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bRedColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<com.thms.systemy3.DrawView
android:id="#+id/yourID"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.thms.systemy3.DrawView>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
I was trying to get access to setColorRed() from my MainClass.java class by
DrawView drawview;
and then use
drawview.setColorRed()
MainClass.java:
public class MainClass extends Activity implements OnClickListener{
TabHost th;
DrawView drawview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
th = (TabHost) findViewById(R.id.tabhost);
//tab3
Button bColorRed = (Button) findViewById(R.id.bRedColor);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("TAB1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("TAB2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("TAB3");
th.addTab(specs);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRedColor:
drawview.setColorRed();
break;
}
What am I doing wrong? Could anyone correct this code or give me a proper example of setting up simple app that draws rectangle and is able to change color by a button?
Thank you for reply.
try using this:
paint.setStyle(Paint.Style.FILL);
I wrote the following view:
public class EllipseView extends View {
private final Paint paint = new Paint();
public EllipseView(Context context) {
super(context);
paint.setColor(Color.RED);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawOval(new RectF(0, 0, getWidth(), getHeight()), paint);
}
}
How to add it to layout in XML? The following does not work (debugger does not connect):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View class="com.incubation.EllipseView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
ADDON
There were also a problem with communicating with Eclipse and Device, which required restart to fix
Have you tried:
<com.incubation.EllipseView
android...
/>
try
<com.incubation.EllipseView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="200dp"
/>
I'm writing a program that is supposed to draw a line in a canvas at the bottom of the screen below a button, etc. My layout shows up, but the line doesn't. Anyone know why my canvas doesn't show up?
Here is the code:
public class Vectors extends Activity{
VectorsView vectorsView;
LinearLayout l;
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vectors);
vectorsView = new VectorsView(this);
l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView);
.......
}
public class VectorsView extends View{
public VectorsView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawLine(0, 0, 100, 100, paint);
vectorsView.draw(canvas);
vectorsView.setLayoutParams(new LayoutParams(25, 25));
}
}
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical">
<ImageView android:layout_height="wrap_content"
android:src="#drawable/vectors"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:adjustViewBounds="true">
</ImageView>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button android:text="Choose Program"
android:id="#+id/bChsProgV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="97dp"
android:adjustViewBounds="true">
</Button>
<ImageButton android:layout_height="wrap_content"
android:src="#drawable/help"
android:id="#+id/ibHelpV"
android:layout_width="wrap_content"
android:layout_marginLeft="65dp"
android:background="#null"
android:layout_marginTop="10dp">
</ImageButton>
</LinearLayout>
<LinearLayout android:id="#+id/llCanvasV"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</LinearLayout>
</LinearLayout>
onMeasure method missing in VectorsView class. and also you can add VectorsView from Layout by keeping the complete package <package.VectorsView android:id= android:width= android:height= ></package.VectorsView>
I just created a view for displaying PDF file
public class PDFGraphic extends View{
String mText;
float mLastX;
float mLastY;
public float mOffX;
public float mOffY;
Canvas mCan;
Bitmap mBi;
public PDFGraphic(Context context, AttributeSet attrs){
super(context,attrs);
setPageBitmap();
setBackgroundColor(Color.TRANSPARENT);
}
public void uiInvalidate() {
postInvalidate();
}
public void setPageBitmap() {
mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
mCan = new Canvas(mBi);
mCan.drawColor(Color.RED);
}
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(mBi, 0, 0, paint);
}
}
And this is my xml file(pdfview):
<?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" android:orientation="vertical">
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_width="match_parent" android:gravity="bottom|center" android:layout_height="wrap_content" android:minHeight="30px" android:visibility="visible">
<com.shawnprojectPDF.PDFGraphic android:id="#+id/pdfview1" android:layout_width="match_parent" android:layout_above="#+id/editText1" android:layout_alignParentTop="true" android:layout_height="match_parent"></com.shawnprojectPDF.PDFGraphic>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/leftarray" android:id="#+id/left" android:layout_alignParentBottom="true" android:layout_marginLeft="68px"></ImageButton>
<EditText android:id="#+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="#+id/left" android:layout_alignParentBottom="true" android:gravity="center" android:width="100px" android:singleLine="true" android:maxLength="12"></EditText>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/right" android:src="#drawable/rightarray" android:layout_toRightOf="#+id/editText1" android:layout_alignParentBottom="true"></ImageButton>
</RelativeLayout>
</LinearLayout>
In my main activity,if setContentView(R.Layout.pdfview), the view(PDFGraphic) will not be invalidated, if setContentView(New PDFGraphic(this)),it invalidates successfully.
How to refresh the view in the whole layout.
I don't see the problem. These are the results that I get with your code (next time, you do this part). With setContentView(R.layout.pdfview), this is the result:
With setContentView(new PDFGraphic(this, null) -- note that I used the two-arg constructor because you didn't define PDFGraphic(Context) -- this is the result:
In either case, your onDraw() is happening correctly.