how to use colorpicker with my application? - android

I have made a simple android drawing application,In that i have taken different buttons for different colors,Now i want is that when i click on red button my pen color should be chaned to red.same as for diffrent colors..My code is as below,Please suggest me friends..
SingleTouch.java
package com.example.singletouch;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;
public class SingleTouchView extends View {
public static int width;
public int height;
public Bitmap mBitmap;
public Canvas mCanvas;
public Path mPath;
public Paint mBitmapPaint;
Context context;
public Paint mPaint;
public Paint circlePaint;
public Path circlePath;
public enum DrawingPens {
PEN_1(6), PEN_2(4), PEN_3(2), PEN_4(1);
public Paint mPaint;
private DrawingPens(final int width) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(width);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
Paint getPaint() {
return mPaint;
}
}
public enum DrawingColors{
Black(Color.parseColor("#000000")),Blue(Color.parseColor("#0000FF")),Cofee(Color.parseColor("#D2691E")),Cyan(Color.parseColor("#00FFFF"))
,Fuchiya(Color.parseColor("#FF00FF")),Gray(Color.parseColor("#808080")),Green(Color.parseColor("#00FF00")),Indigo(Color.parseColor("#4B0082")),
Khaki(Color.parseColor("#F0E68C")),Lavendar(Color.parseColor("#E6E6FA")),Magenta(Color.parseColor("#FF00FF")),Mango(Color.parseColor("#FF8C00"))
,Maroon(Color.parseColor("#800000")),Orange(Color.parseColor("#FFA500")),Pink(Color.parseColor("#FFC0CB")),Pista(Color.parseColor("#9ACD32")),
Purple(Color.parseColor("#800080")),Red(Color.parseColor("#FF0000")),Tan(Color.parseColor("#0000A0")),Yellow(Color.parseColor("#FFD801"));
public Paint mPaint;
private DrawingColors(final int color) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(width);
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
Paint getPaint() {
return mPaint;
}
}
public SingleTouchView(final Context context) {
super(context);
init(context);
}
public SingleTouchView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
}
public SingleTouchView(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
private Path mCurrentPath;
private Path mCurrentPath1;
private void init(final Context context) {
setPen(DrawingPens.PEN_1);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Map.Entry<Path, DrawingPens> entry : mPaths) {
canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
}
}
#Override
public boolean onTouchEvent(MotionEvent me) {
float eventX = me.getX();
float eventY = me.getY();
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
mCurrentPath.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
mCurrentPath.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
public void setPen(final DrawingPens pen) {
mCurrentPath = new Path();
mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(
mCurrentPath, pen));
}
public void eraser() {
// TODO Auto-generated method stub
mPaint = new Paint();
/* Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show();
mPaint.setXfermode(null);
mPaint.setAlpha(0x00FFFFFF);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));*/
// invalidate();
}
public void setColor(final DrawingColors color){
mCurrentPath = new Path();
mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingColors>(
mCurrentPath, color));
}
}
main.java
package com.example.singletouch;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
ImageView pen, color;
SingleTouchView mDrawView;
RelativeLayout layout, layout1;
ImageView remove;
ImageView eraser;
LinearLayout pens;
HorizontalScrollView myplate;
private Path mPath;
public Canvas mCanvas;
LinearLayout pen1, pen2, pen3, pen4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawView = (SingleTouchView) findViewById(R.id.myview);
layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout = (RelativeLayout) findViewById(R.id.layout);
pen = (ImageView) findViewById(R.id.pen);
pens = (LinearLayout) findViewById(R.id.linear);
pens.setVisibility(View.GONE);
pen1 = (LinearLayout) findViewById(R.id.pen1);
pen2 = (LinearLayout) findViewById(R.id.pen2);
pen3 = (LinearLayout) findViewById(R.id.pen3);
pen4 = (LinearLayout) findViewById(R.id.pen4);
color = (ImageView) findViewById(R.id.color);
myplate = (HorizontalScrollView) findViewById(R.id.myplate);
eraser = (ImageView) findViewById(R.id.eraser);
remove = (ImageView) findViewById(R.id.remove);
/*
* pen1.setOnClickListener(this); pen2.setOnClickListener(this);
* pen3.setOnClickListener(this); pen4.setOnClickListener(this);
*/
pen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// layout.addView(mDrawView);
pens.setVisibility(View.VISIBLE);
}
});
pens.setVisibility(View.GONE);
pen1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
pens.setVisibility(View.GONE);
}
});
pen2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
pens.setVisibility(View.GONE);
}
});
pen3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);
}
});
pen4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);
}
});
remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
layout.removeView(mDrawView);
mDrawView = new SingleTouchView(MainActivity.this);
layout.addView(mDrawView);
}
});
eraser.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
color.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/linearLayout1" >
<com.example.singletouch.SingleTouchView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/pen" />
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/myplate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:visibility="gone" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- > -->
<Button
android:id="#+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/red"
android:padding="15dp" />
<Button
android:id="#+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/green"
android:padding="15dp" />
<Button
android:id="#+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/blue"
android:padding="15dp" />
<Button
android:id="#+id/cyan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cyan"
android:padding="15dp" />
<Button
android:id="#+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/yellow"
android:padding="15dp" />
<Button
android:id="#+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/orange"
android:padding="15dp" />
<Button
android:id="#+id/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/black"
android:padding="15dp" />
<Button
android:id="#+id/cofee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cofee"
android:padding="15dp" />
<Button
android:id="#+id/fuchiya"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/fuchiya"
android:padding="15dp" />
<Button
android:id="#+id/gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gray"
android:padding="15dp" />
<Button
android:id="#+id/indigo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/indigo"
android:padding="15dp" />
<Button
android:id="#+id/khaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/khaki"
android:padding="15dp" />
<Button
android:id="#+id/lavendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/lavendar"
android:padding="15dp" />
<Button
android:id="#+id/magenta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/magenta"
android:padding="15dp" />
<Button
android:id="#+id/mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mango"
android:padding="15dp" />
<Button
android:id="#+id/maroon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/maroon"
android:padding="15dp" />
<Button
android:id="#+id/pista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pista"
android:padding="15dp" />
<Button
android:id="#+id/pink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pink"
android:padding="15dp" />
<Button
android:id="#+id/purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:padding="15dp" />
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/menubar"
android:padding="2dp"
android:weightSum="4" >
<ImageView
android:id="#+id/pen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/pen" />
<ImageView
android:id="#+id/eraser"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/eraser" />
<ImageView
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/color" />
<ImageView
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/remove" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="4" >
<LinearLayout
android:id="#+id/pen1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/pen1" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/pen2" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/pen3" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/pen4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

If i understand your question you can extend FrameLayout and add to it the Button/ImageView with the pencil draw transparent.
With this you only should to change the FrameLayout background and can make whatever color button with the same View.

Related

How to align 8 little circles around of a centered big circle, like attached image shows?

I have to do this layout:
I was trying to align the views, using RelativeLayout and layout_toRightOf, layout_below, etc, but the best that I achieved was this:
Here are the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<RelativeLayout
android:id="#+id/big"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/circular"
android:layout_margin="10dp"
android:layout_centerInParent="true"/>
<RelativeLayout
android:id="#+id/right"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toRightOf="#+id/big"
android:layout_centerVertical="true"/>
<RelativeLayout
android:id="#+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toLeftOf="#+id/big"
android:layout_centerVertical="true"/>
<RelativeLayout
android:id="#+id/top"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_above="#+id/big"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:id="#+id/bottom"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_below="#+id/big"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:id="#+id/northeast"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toRightOf="#+id/big"
android:layout_alignTop="#+id/top"/>
<RelativeLayout
android:id="#+id/northwest"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toLeftOf="#+id/big"
android:layout_alignTop="#+id/top"/>
<RelativeLayout
android:id="#+id/southeast"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toRightOf="#+id/big"
android:layout_below="#+id/big"/>
<RelativeLayout
android:id="#+id/southwest"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circular"
android:layout_toLeftOf="#+id/big"
android:layout_below="#+id/big"/>
</RelativeLayout>
I'm trying to avoid using margin on the little circles, because the diagonal circles have to be aligned exactly to the center, in comparison with the top/bottom/right/left circles.
How can I do that?
I show you another approach.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class CircleMenu extends View {
private Paint mainPaint;
private Paint secondPaint;
private Paint textPaint;
private int radius_main =130;
private int menuRadialButtonsCount =7;
private int menuInnerPadding = 40;
private int radialCircleRadius = 60;
private int textPadding = 25;
private double startAngle = - Math.PI/2f;;
public CircleMenu(Context context) {
super(context);
}
public CircleMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mainPaint = new Paint();
mainPaint.setColor(Color.BLUE);
secondPaint = new Paint();
secondPaint.setColor(Color.DKGRAY);
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = canvas.getWidth()/2 ;
int centerY= canvas.getHeight()/2;
canvas.drawCircle(centerX,centerY,radius_main,mainPaint);
for(int i=0;i<menuRadialButtonsCount;i++){
double angle =0;
if(i==0){
angle = startAngle;
}else{
angle = startAngle+(i * ((2 * Math.PI) / menuRadialButtonsCount));
}
int x = (int) (centerX + Math.cos(angle)*(radius_main+menuInnerPadding+radialCircleRadius));
int y = (int) (centerY + Math.sin(angle)*(radius_main+menuInnerPadding+radialCircleRadius));
canvas.drawCircle(x,y,radialCircleRadius,secondPaint);
float tW = textPaint.measureText("Text "+i);
canvas.drawText("Text "+i,x-tW/2,y+radialCircleRadius+textPadding,textPaint);
}
}
}
You can extend this class, add methods to set dimmensions from resources, controlling numer of circles, their size, paddings, onTouch, shadows, colors ....
<your.package.CircleMenu
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Updated version:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
public class CircleMenu extends View {
public static interface IMenuListener{
public void onMenuClick(MenuCircle item);
}
public static class MenuCircle{
private int x,y,radius;
public int id;
public String text;
}
private Paint mainPaint;
private Paint secondPaint;
private Paint textPaint;
private int radius_main =130;
private int menuInnerPadding = 40;
private int radialCircleRadius = 60;
private int textPadding = 25;
private double startAngle = - Math.PI/2f;
private ArrayList<MenuCircle> elements;
private IMenuListener listener;
public void setListener(IMenuListener listener){
this.listener = listener;
}
public void clear(){
elements.clear();
listener=null;
}
public CircleMenu(Context context) {
super(context);
init();
}
public CircleMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
elements = new ArrayList<>();
}
public void addMenuItem(String text,int id){
MenuCircle item = new MenuCircle();
item.id = id;
item.text=text;
elements.add(item);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mainPaint = new Paint();
mainPaint.setColor(Color.BLUE);
secondPaint = new Paint();
secondPaint.setColor(Color.DKGRAY);
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = canvas.getWidth()/2 ;
int centerY= canvas.getHeight()/2;
canvas.drawCircle(centerX,centerY,radius_main,mainPaint);
for(int i=0;i<elements.size();i++){
double angle =0;
if(i==0){
angle = startAngle;
}else{
angle = startAngle+(i * ((2 * Math.PI) / elements.size()));
}
elements.get(i).x = (int) (centerX + Math.cos(angle)*(radius_main+menuInnerPadding+radialCircleRadius));
elements.get(i).y = (int) (centerY + Math.sin(angle)*(radius_main+menuInnerPadding+radialCircleRadius));
canvas.drawCircle( elements.get(i).x,elements.get(i).y,radialCircleRadius,secondPaint);
float tW = textPaint.measureText(elements.get(i).text);
canvas.drawText(elements.get(i).text,elements.get(i).x-tW/2,elements.get(i).y+radialCircleRadius+textPadding,textPaint);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
for(MenuCircle mc : elements){
double distance = Math.hypot(event.getX()-mc.x,event.getY()-mc.y);
if(distance<= radialCircleRadius){
//touched
if(listener!=null)
listener.onMenuClick(mc);
return true;
}
}
}
return super.onTouchEvent(event);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}
In Fragment:
CircleMenu cm = (CircleMenu) view.findViewById(R.id.c_menu);
cm.addMenuItem("one",1);
cm.addMenuItem("two",2);
cm.addMenuItem("three",3);
cm.addMenuItem("ten",10);
cm.addMenuItem("oh oh",156);
cm.addMenuItem("exit",134);
cm.setListener(new CircleMenu.IMenuListener() {
#Override
public void onMenuClick(CircleMenu.MenuCircle item) {
Toast.makeText(getActivity(),item.text+" "+item.id,Toast.LENGTH_LONG).show();
}
});
This helps you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RelativeLayout
android:id="#+id/rlBig"
android:layout_width="250dp"
android:layout_height="260dp"
android:layout_centerInParent="true">
<RelativeLayout
android:id="#+id/big"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/northwest"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/southeast"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/southwest"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/northeast"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/round_orange_schdule_meet" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/right"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/rlBig"
android:layout_marginLeft="-15dp"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginRight="-15dp"
android:layout_toLeftOf="#+id/rlBig"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/top"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/rlBig"
android:layout_marginBottom="-15dp"
android:layout_centerHorizontal="true"
android:background="#drawable/round_orange_schdule_meet" />
<RelativeLayout
android:id="#+id/bottom"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/rlBig"
android:layout_centerHorizontal="true"
android:layout_marginTop="-10dp"
android:background="#drawable/round_orange_schdule_meet" />
Output is:
You should go with a custom-view here - with a lot of force you might be able to do it with a layout - but it will be messy and not perform well

Draw images on evey touch co-ordinats Of Imageview

I want to draw small circular images at every point where user touches existing Image-view in android.And want to handle the click of every small ploted image.Right now I am trying to draw circluar image on ImageView but its not working for me.
Here is my code :
<?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:background="#FFFFFF"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:background="#EEEDEE"
android:gravity="center"
android:orientation="horizontal"
android:padding="20dp"
android:weightSum="4" >
<ImageView
android:id="#+id/img_overview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img_specifications"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img_features"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img_resources"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:orientation="vertical"
android:padding="20dp" >
<Textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Siss "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#CE561B"
android:textStyle="bold|italic" />
<Textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="dummy long text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black_color" />
<FrameLayout
android:id="#+id/ll_img_bigview_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.htmlcheck.CustomImageView
android:id="#+id/img_big_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/sj"/>
</FrameLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
dimension of sj image is 900 X 600 pixels.
My CustomImageView Class :
package com.example.htmlcheck;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
public class CustomImageView extends ImageView {
private ArrayList<Point> mTouches;
private Bitmap mMarker;
// Java constructor
public CustomImageView(Context context) {
super(context);
init();
}
// XML constructor
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mTouches = new ArrayList<Point>();
mMarker = BitmapFactory.decodeResource(this.getResources(),
R.drawable.cross_small);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Capture a reference to each touch for drawing
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouches.add(new Point((int) event.getX(), (int) event.getY()));
return true;
}
return super.onTouchEvent(event);
}
#Override
protected void onDraw(Canvas c) {
// Let the image be drawn first
super.onDraw(c);
// Draw your custom points here
Paint paint = new Paint();
for (Point p : mTouches) {
c.drawBitmap(mMarker, p.x, p.y, paint);
}
}
}
My activity :
public class MainActivity extends Activity implements OnClickListener {
private CustomImageView mImgBigImageview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgBigImageview = (CustomImageView) findViewById(R.id.img_big_imageview);
mImgBigImageview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.onTouchEvent(event)) {
}
return true;
}
});
}
Any Help will be appreciated.
Thanks.
Here you go :-
mImgBigImageview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CustomImageView mcustomImagview = (CustomImageView) v;
mcustomImagview.invalidate();
if (v.onTouchEvent(event)) {
// Do something with event.getX(), event.getY()
}
return true;
}
});

How can I implement a View in my Activity?

I have the Feldlayout class, the feldlayout.xml which is the layout of the Feldlayout class, and I have a View class, which can be used to make lines on the screen. I want now to make these lines on the feldlayout.xml layout, but I don't know how to connect my classes. So how can I do this?
Feldlayout.class (Main Class)
package com.example.volleyballapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class feldlayout extends Activity{
Button b7;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feldlayout);
b7 = (Button) findViewById(R.id.button7);
b7.setOnClickListener(handler);
}
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
if(v==b7){
Intent i = new Intent(feldlayout.this, Main_Layout.class);
startActivity(i);
}
}};}
Feldlayout.xml (Main Layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/farbe"
android:orientation="vertical" >
<ImageView
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="160dp"
android:background="#drawable/whiterectangle" />
<ImageView
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="74dp"
android:layout_marginLeft="74dp"
android:layout_marginRight="74dp"
android:layout_marginTop="164dp"
android:background="#drawable/volleyballfeld" />
<ImageView
android:id="#+id/view3meterlinie"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="100dp"
android:background="#drawable/dreimeterlinie" />
<ImageView
android:id="#+id/netz"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="310dp"
android:background="#drawable/netz" />
<ImageView
android:id="#+id/view3meterliniedef"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="520dp"
android:background="#drawable/dreimeterlinie" />
<Button
android:id="#+id/button8"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/view1"
android:layout_marginTop="1dp"
android:background="#drawable/bnt_black"
android:text="Speichern" />
<Button
android:id="#+id/button7"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignLeft="#+id/view2"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"
android:background="#drawable/bnt_black"
android:onClick="home"
android:text="Zurück" />
<Button
android:id="#+id/button9"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignTop="#+id/button7"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/button7"
android:background="#drawable/bnt_black"
android:text="Linien Anzeigen" />
<Button
android:id="#+id/Statistik"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignBaseline="#+id/button8"
android:layout_alignBottom="#+id/button8"
android:layout_marginRight="31dp"
android:layout_toLeftOf="#+id/button8"
android:background="#drawable/bnt_black"
android:text="Statistik" />
</RelativeLayout>
ViewFeld.class (View class, which makes lines)
package com.example.volleyballapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class ViewFeld extends View{
private Paint paint = new Paint();
private Path path = new Path();
int count = 1;
public ViewFeld(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setAntiAlias(true);
paint.setStrokeWidth(6f); //Breite des Strichs
paint.setColor(Color.BLACK); //Farbe des Strichs
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
protected void onDraw(Canvas canvas) {
if ((count%2 != 0)){
canvas.drawPath(path, paint);}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
count++;
if(count%2 != 1){
path.moveTo(eventX, eventY);
return true;
}
if(count%2 != 0){
path.lineTo(eventX, eventY);
break;
}
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
You can use your custom view in your layout xml:
<com.example.volleyballapp.ViewFeld
android:layout_width="....."
android:layout_height="....."
etc..... />

Android custom TextView not intercepting OnClickListener of parent

I've made a custom TextView that draws lines for every line of text.
This custom TextView is placed in a LinearLayout along with other standard TextViews and ImageViews.
Whenever I click the normal TextViews, ImageViews or outside the custom TextView, the click is intercepted.
But, if I click on the custom TextView, the click is not intercepted.
Please help!!!
Images:
Normal state(minimized)
Maximized state
Click not detected here
Code:
NoteTextView.java
package harsha.notes.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class NoteTextView extends TextView {
boolean isMaximized = false;
public NoteTextView(Context canvas, AttributeSet attributeSet) {
super(canvas, attributeSet);
}
#Override
protected void onDraw(Canvas canvas) {
int width = getWidth();//var2.getDefaultDisplay().getWidth();
int height = getHeight();//var2.getDefaultDisplay().getHeight();
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int scrollY = getScrollY();
int scrollX = getScrollX();
int lineHeight = getLineHeight();
int grossWidth = width + scrollX;
int grossHeight = height + scrollY;
int netViewHeight = lineHeight + (grossHeight - paddingTop - paddingBottom);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//paint.setColor(1722066084);
paint.setColor(1711276032);
float fontAdjustment = 15.826172F - (paint.getFontMetrics().bottom - paint.getFontMetrics().top);
float baseline = (float)(scrollY + lineHeight - scrollY % lineHeight) - fontAdjustment;
float linePadding;
if(width != 800 && height != 480) {
linePadding = 2;
} else {
linePadding = 4;
}
while(baseline < (float)netViewHeight) {
canvas.drawLine((float)paddingLeft, baseline + (float)paddingTop + linePadding, (float)grossWidth, baseline + (float)paddingTop + linePadding, paint);
baseline += (float)lineHeight;
}
super.onDraw(canvas);
}
public boolean isMaximized(){
return isMaximized;
}
public void setMaximized(boolean maximized){
isMaximized = maximized;
if(!isMaximized){
setLines(1);
}else{
setSingleLine(false);
}
}
}
NoteListAdapter.java
package harsha.notes.Items;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import harsha.notes.DB.NotesProvider;
import harsha.notes.NoteEditActivity;
import harsha.notes.R;
import harsha.notes.view.NoteTextView;
import harsha.notes.view.ViewUtils;
import java.util.ArrayList;
public class NoteListAdapter extends ArrayAdapter<Note> {
ArrayList<Note> mNotes;
final Context mContext;
public NoteListAdapter(Context context, ArrayList<Note> notes) {
super(context, R.layout.note_item, notes);
mContext = context;
mNotes = new ArrayList<Note>();
mNotes = notes;
}
public class NoteViewHolder {
public LinearLayout preview_layout;
public TextView position;
public TextView date;
public TextView time;
//public CheckBox check;
public ImageView edit;
public ImageView delete;
public NoteTextView content;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
NoteViewHolder noteViewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.note_item, null);
noteViewHolder = new NoteViewHolder();
noteViewHolder.preview_layout = (LinearLayout) convertView.findViewById(R.id.preview);
noteViewHolder.position = (TextView) convertView.findViewById(R.id.position);
noteViewHolder.date = (TextView) convertView.findViewById(R.id.date);
noteViewHolder.time = (TextView) convertView.findViewById(R.id.time);
noteViewHolder.edit = (ImageView) convertView.findViewById(R.id.menu_edit);
noteViewHolder.delete = (ImageView) convertView.findViewById(R.id.menu_delete);
//noteViewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
noteViewHolder.content = (NoteTextView) convertView.findViewById(R.id.preview_text);
}else{
noteViewHolder = (NoteViewHolder) convertView.getTag();
}
Note note = mNotes.get(position);
noteViewHolder.content.setMaximized(note.getChecked());
noteViewHolder.content.setNextFocusUpId(R.id.preview);
noteViewHolder.preview_layout.setBackgroundResource(ViewUtils.getBackground(note.getColor(), noteViewHolder.content.isMaximized()));
noteViewHolder.content.setText(note.getText());
//noteViewHolder.check.setVisibility(View.GONE);
noteViewHolder.date.setText(note.getDate());
noteViewHolder.time.setText(note.getTime());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
LinearLayout preview_layout = (LinearLayout) view.findViewById(R.id.preview);
NoteTextView content = (NoteTextView) view.findViewById(R.id.preview_text);
Note note = (Note) preview_layout.getTag();
content.setMaximized(!note.getChecked());
preview_layout.setBackgroundResource(ViewUtils.getBackground(note.getColor(), !note.getChecked()));
note.setChecked(!note.getChecked());
}
});
noteViewHolder.edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout preview_layout = (LinearLayout) view.getRootView().findViewById(R.id.preview);
Note note = (Note) preview_layout.getTag();
Intent intent = new Intent(mContext, NoteEditActivity.class);
intent.setData(Uri.withAppendedPath(NotesProvider.CONTENT_URI, Uri.encode(String.valueOf(note.getNoteID()))));
mContext.startActivity(intent);
}
});
noteViewHolder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout preview_layout = (LinearLayout) view.findViewById(R.id.preview);
Note note = (Note) preview_layout.getTag();
}
});
noteViewHolder.position.setText(String.valueOf(position+1));
noteViewHolder.preview_layout.setTag(note);
convertView.setTag(noteViewHolder);
return convertView;
}
}
note_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#id/preview_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ff4A4A4A" android:padding="5dip" android:measureWithLargestChild="false"
android:clickable="true" android:focusable="true">
<LinearLayout android:id="#id/preview" android:focusable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#drawable/note_yellow_min"
android:focusableInTouchMode="true">
<LinearLayout android:layout_width="fill_parent" android:layout_height="40dip" android:layout_marginRight="0dp" android:layout_marginBottom="1dip" android:gravity="center_vertical" android:focusable="false"
android:layout_marginTop="1dip">
<ImageView android:id="#id/menu_edit" android:layout_width="37dip" android:layout_height="37dip" android:src="#drawable/menu_edit" android:contentDescription="#string/color_yellow"
android:clickable="true" android:layout_marginLeft="2dip"/>
<TextView android:id="#id/position" android:textSize="15.0dip" android:typeface="sans" android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MMM" android:layout_gravity="center" android:layout_marginLeft="5dip" android:gravity="right"
android:focusable="false"/>
<TextView android:id="#id/date" android:textSize="17.0dip" android:typeface="sans" android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="16 AUG 2001" android:layout_weight="1" android:layout_gravity="center" android:layout_marginLeft="8dip"
android:textIsSelectable="false" android:focusable="false"/>
<TextView android:id="#id/time" android:textSize="17.0dip" android:typeface="sans" android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20:11" android:layout_gravity="center" android:layout_marginLeft="8dip" android:layout_marginRight="8dip"
android:textIsSelectable="false" android:focusable="false"/>
<ImageView android:id="#id/menu_delete" android:layout_width="37dip" android:layout_height="37dip" android:src="#drawable/menu_delete" android:contentDescription="#string/color_yellow"
android:clickable="true" android:layout_marginLeft="2dip" android:layout_marginRight="3dip"
android:visibility="visible"/>
<ImageView android:id="#id/check" android:layout_width="37dip" android:layout_height="37dip" android:src="#drawable/menu_check" android:contentDescription="#string/color_yellow"
android:clickable="true" android:layout_marginLeft="0dp" android:layout_marginRight="3dip"
android:visibility="gone"/>
</LinearLayout>
<View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#77000000" android:focusable="false"/>
<harsha.notes.view.NoteTextView android:scrollbarStyle="outsideOverlay" android:autoLink="all" android:capitalize="sentences" android:lineSpacingExtra="4.0dip" android:text="123" android:textColor="#ff000000" android:minLines="1" android:id="#id/preview_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22dp" android:layout_marginLeft="20dp" android:layout_marginRight="25dip" android:layout_marginTop="3dip" android:layout_marginBottom="28dip" android:clickable="true"
android:typeface="sans" android:visibility="visible" android:focusable="false"
android:background="#android:color/transparent" android:linksClickable="true"
android:focusableInTouchMode="false" android:longClickable="true"
android:enabled="true"/>
</LinearLayout>
</LinearLayout>
try setting onFocusChanged listener to edittext.
Did you mean that, when you click on custom TextView, your convertView's OnClickListener didn't called onClick()?
If yes, you could try disable the custom TextView.
<harsha.notes.view.NoteTextView android:scrollbarStyle="outsideOverlay" android:autoLink="all" android:capitalize="sentences" android:lineSpacingExtra="4.0dip" android:text="123" android:textColor="#ff000000" android:minLines="1" android:id="#id/preview_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22dp" android:layout_marginLeft="20dp" android:layout_marginRight="25dip" android:layout_marginTop="3dip" android:layout_marginBottom="28dip" android:clickable="true"
android:typeface="sans" android:visibility="visible" android:focusable="false"
android:background="#android:color/transparent" android:linksClickable="true"
android:focusableInTouchMode="false" android:longClickable="true"
android:enabled="false"/>

how to clear view in android?

I have created a simple demo application in android for drawing ,now can any one say me how can i remove view and clear screen in just one click .I have tried as below:please help me to do it....thanx in advance..!
main.java
package com.example.singletouch;
import com.example.singletouch.R.attr;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
ImageView pen;
SingleTouchView mDrawView;
ImageView remove;
LinearLayout pens;
LinearLayout pen1, pen2, pen3, pen4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawView = (SingleTouchView) findViewById(R.id.myview);
pen = (ImageView) findViewById(R.id.pen);
pens = (LinearLayout) findViewById(R.id.linear);
pens.setVisibility(View.GONE);
pen1 = (LinearLayout) findViewById(R.id.pen1);
pen2 = (LinearLayout) findViewById(R.id.pen2);
pen3 = (LinearLayout) findViewById(R.id.pen3);
pen4 = (LinearLayout) findViewById(R.id.pen4);
remove=(ImageView)findViewById(R.id.remove);
/*
* pen1.setOnClickListener(this); pen2.setOnClickListener(this);
* pen3.setOnClickListener(this); pen4.setOnClickListener(this);
*/
pen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pens.setVisibility(View.VISIBLE);
}
});pens.setVisibility(View.GONE);
pen1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
pens.setVisibility(View.GONE);
}
});
pen2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
pens.setVisibility(View.GONE);
}
});
pen3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);
}
});
pen4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);
}
});
remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
SingleTouchView.java
package com.example.singletouch;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
public class SingleTouchView extends View{
public int width;
public int height;
public Bitmap mBitmap;
public Canvas mCanvas;
public Path mPath;
public Paint mBitmapPaint;
Context context;
public Paint circlePaint;
public Path circlePath;
public enum DrawingPens {
PEN_1(6),
PEN_2(4),
PEN_3(2),
PEN_4(1);
final public Paint mPaint;
/**
* Constructor
*
* #param width width of stroke
* #param color color of stroke
*/
private DrawingPens(final int width) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(width);
//mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
/**
* #return corresponding paint
*/
Paint getPaint() {
return mPaint;
}
}
public SingleTouchView(final Context context) {
super(context);
init(context);
}
public SingleTouchView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SingleTouchView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init(context);
}
/** To store Paint - Path relation */
// TODO: depending on exact limits, more optimal ways can be found
private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
/** Cached current path, <b>NOTE:</b> this field is tail at mPaths and is used it only for caching, not drawing */
private Path mCurrentPath;
/**
* Inits internal views data, should be called from every constructor
*
* #param context {#link Context}
*/
private void init(final Context context) {
/* TODO: if some values of paints cannot be determined in static context (in enum),
then Paints should be created here and used via EnumMap */
// Initial pen
setPen(DrawingPens.PEN_1);
}
#Override
public void onDraw(Canvas canvas){
// just to draw background
super.onDraw(canvas);
// Draw all paths
for (Map.Entry<Path, DrawingPens> entry : mPaths) {
canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
}
}
#Override
public boolean onTouchEvent(MotionEvent me){
float eventX = me.getX();
float eventY = me.getY();
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
mCurrentPath.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
mCurrentPath.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
/**
* Setter for new pen
*
* #param pen {#link DrawingPens} to be used for next drawing
*/
public void setPen(final DrawingPens pen) {
// put latest item to the queue
mCurrentPath = new Path();
mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(mCurrentPath, pen));
}
public void clear(View v){
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<com.example.singletouch.SingleTouchView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/pen" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/menubar"
android:padding="2dp"
android:weightSum="4" >
<ImageView
android:id="#+id/pen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/pen" />
<ImageView
android:id="#+id/eraser"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/eraser" />
<ImageView
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/color" />
<ImageView
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="#drawable/remove" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="4" >
<LinearLayout
android:id="#+id/pen1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/pen1" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/pen2" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/pen3" />
</LinearLayout>
<LinearLayout
android:id="#+id/pen4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/pen4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Jus use ...
mCanvas.drawColor(Color.BLACK);
... for clearing the canvas or any other color you want to have in background. E.g.
remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// clear canvas contents
mCanvas.drawColor(Color.BLACK);
// create new path list; old one will be garbage collected
ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths =
new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
pens.setVisibility(View.VISIBLE);
}
p.s.: for removing a view dynamically from its container use removeView(....), e.g.
((ViewGroup)viewToRemove.getParent()).removeView(viewToRemove);
Hope this helps ... Cheers!
remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
layout.removeView(mDrawView);
mDrawView = new SingleTouchView(MainActivity.this);
layout.addView(mDrawView);
}
});
Put the portion of the screen you want cleared in a single layout in xml eg
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height= "wrap_content">
The Views that have to be gone on click
</RelativeLayout>
Then in your code in onClick() of the button give
((RelativeLayout) findViewById(R.id.layout)).setVisiblity(View.GONE));
EDIT :
If you want the ImageView cleared use :
((ImageView) findViewById(R.id.imageView)).setImageDrawable(null);
instead of drawing solid color you can draw transparent color this help you don't cover the other views if you use them as a background for the canvas
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
I got this from this answer: https://stackoverflow.com/a/10882301/8113211

Categories

Resources