I'm trying as a user to copy paste the content of an editText of my application.
Unfortunately it doesn't work and it seems like it's not a common issue.
I'm suspecting a dumb reason but still can't find the why. Just in case here is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<com.adylitica.customlistview.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/default_enlightenment"
android:orientation="horizontal" >
<com.adylitica.components.EditTextNotes
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:paddingBottom="25dp"
android:paddingTop="10dp"
android:textColor="#301e1b"
android:textSize="24dp" />
<TextView
android:id="#+id/time"
android:layout_width="82dp"
android:layout_height="65dp"
android:background="#drawable/circle_background"
android:gravity="center"
android:textColor="#drawable/color_text"
android:textSize="18dp" />
Here is the code of the component extending EditText :
package com.adylitica.components;
import com.adylitica.activity.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
/**
* Defines a custom EditText View that draws lines between each line of text that is displayed.
*/
public class EditTextNotes extends EditText {
private Context contextP;
private Rect mRect;
private Bitmap line;
public static int nbLines = 0;
public EditTextNotes(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
contextP = context;
line = BitmapFactory.decodeResource(contextP.getResources(), R.drawable.line_thin);
line = Bitmap.createBitmap(line, 0, 0, 2000, 1);
mRect = new Rect();
}
public EditTextNotes(Context context, AttributeSet attrs) {
super(context, attrs);
contextP = context;
line = BitmapFactory.decodeResource(contextP.getResources(), R.drawable.line_thin);
line = Bitmap.createBitmap(line, 0, 0, 2000, 1);
mRect = new Rect();
}
public EditTextNotes(Context context) {
super(context);
contextP = context;
line = BitmapFactory.decodeResource(contextP.getResources(), R.drawable.line_thin);
line = Bitmap.createBitmap(line, 0, 0, 2000, 1);
mRect = new Rect();
}
#Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
int baseline = 0;
int addSize = 0;
canvas.drawBitmap(line, 0, baseline, null);
for (int i = 0; i < count; i++) {
baseline = getLineBounds(i, r) + addSize;
canvas.drawBitmap(line, 0, baseline, null);
nbLines++;
}
super.onDraw(canvas);
}
}
P.S : otherwise the behavior of the editText is ok, like I can get the focus, write in ... I just can't select the whole text and copy it.
Related
I'm trying to draw a rectangle over camera2 textureview, when i run the code I see the usual camera screen with moving square , and when I Click (touch) it, app crashes with the error in topic. I also not sure I implemented the custom view correctly, Here are all the relevant parts, Would love some help (I'm not sure I have a good layout xml, I added ViewGroup code under OnCrearte, not sure i even need to touch xml)
-------CameraActivity.java:
package com.example.android.camera2video;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
public class CameraActivity extends Activity {
private Context context;
CustomView customview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
customview = new CustomView(this);
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(new CustomView(this));
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2VideoFragment.newInstance())
.commit();
}
}
}
-------CustomView.java
package com.example.android.camera2video;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CustomView extends SurfaceView {
private Paint paint;
private SurfaceHolder mHolder;
private Context context;
public CustomView(Context context) {
super(context);
mHolder = getHolder();
mHolder.setFormat(PixelFormat.TRANSPARENT);
this.context = context;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// real work here
}
private void doAdditionalConstructorWork() {
// init variables etc.
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
invalidate();
if (mHolder.getSurface().isValid()) {
final Canvas canvas = mHolder.lockCanvas();
Log.d("touch", "touchRecieved by camera");
System.err.println("EXIT 1");
if (canvas != null) {
Log.d("touch", "touchRecieved CANVAS STILL Not Null");
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.TRANSPARENT);
canvas.drawCircle(event.getX(), event.getY(), 100, paint);
mHolder.unlockCanvasAndPost(canvas);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Canvas canvas1 = mHolder.lockCanvas();
if(canvas1 !=null){
canvas1.drawColor(0, PorterDuff.Mode.CLEAR);
mHolder.unlockCanvasAndPost(canvas1);
}
}
}, 1000);
}
mHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
-----fragment_camera2_video.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.android.camera2video.AutoFitTextureView
android:id="#+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#id/texture"
android:background="#4285f4">
<Button
android:id="#+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/record" />
<ImageButton
android:id="#+id/info"
android:contentDescription="#string/description_info"
style="#android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="4dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="20dp"
android:src="#drawable/ic_action_info" />
</FrameLayout>
</RelativeLayout>
Only one thing can be drawing to a View at a time; once the SurfaceView is connected to the camera, you're not able to lock it for drawing yourself.
Your crash is probably because you're calling mHolder.unlockCanvasAndPost(canvas); outside of the null check.
If you want to draw over the cameara preview, you need a second View positioned on top of the SurfaceView.
I wanted to create a custom button or view progrmatically with a simple image and text as shown in image,
where edge is of button not of image.
Please don't use xml.
Any help would be greatly appreciated. I wanted to learn and create custom view with canvas but since i am new one to canvas, i am not able to create it.
copy and paste below code, Hope this will give you your desired output..
Here is what I've got using this code Screenshot
XML 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="#color/black_alpha_30"
android:padding="15dp">
<RelativeLayout
android:id="#+id/relative_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relative_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Java code:
private RelativeLayout mRelativeLayout, mRelativeParent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_main);
mRelativeParent = (RelativeLayout) findViewById(R.id.relative_parent);
Button btnMain = new Button(MainActivity.this);
btnMain.setBackgroundColor(getResources().getColor(R.color.teal_600));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 80);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.setMargins(15,15,15,15);
btnMain.setLayoutParams(layoutParams);
mRelativeLayout.addView(btnMain);
Button btnImage = new Button(MainActivity.this);
btnImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.teal_bg));
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(150, 150);
layoutParams1.addRule(mRelativeParent.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
btnImage.setLayoutParams(layoutParams1);
mRelativeParent.addView(btnImage);
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class ContainerBox extends View {
private Paint textPaint;
private String mainText="Vikram Singh";
private String backgroundColour = "#FF8514";
private String textColour = "#1896bb";
private Bitmap leftIcon;
private Paint paintBackGround;
private Rect recBackGround;
private Paint paintImage ;
private Rect recImage;
public ContainerBox(Context context) {
super(context);
initializePaints(context);
}
public ContainerBox(Context context, AttributeSet attrs) {
super(context, attrs);
initializePaints(context);
}
public ContainerBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initializePaints(context);
}
private void initializePaints(Context context) {
leftIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_done);
paintImage = new Paint();
paintImage.setColor(Color.parseColor(backgroundColour));
paintImage.setStrokeWidth(3);
paintImage.setAntiAlias(true);
paintImage.setStyle(Paint.Style.FILL);
paintBackGround = new Paint();
paintBackGround.setColor(Color.parseColor(backgroundColour));
paintBackGround.setStrokeWidth(3);
paintBackGround.setAntiAlias(true);
paintBackGround.setStyle(Paint.Style.FILL);
textPaint = new Paint();
textPaint.setColor(Color.parseColor(textColour));
textPaint.setAntiAlias(true);
textPaint.setTextSize(4);
textPaint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),leftIcon.getHeight()+40);
}
#Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = leftIcon.getHeight()+40;
int differenceHeight=height-25;
int differenceWidth=width-leftIcon.getWidth()+15;
recBackGround=new Rect(0,25,differenceWidth,differenceHeight);
canvas.drawRect(recBackGround,paintBackGround);
textPaint.setTextSize(15f);
float textWidth = textPaint.measureText(mainText);
int x = (int) ((recBackGround.width() - textWidth) / 2);
int y = (int) ((recBackGround.centerY() - (textPaint.descent() + textPaint.ascent())/2));
// draw text
canvas.drawText(mainText, x, y, textPaint);
recImage=new Rect(recBackGround.right,0,width,height);
canvas.drawRect(recImage,paintImage);
int left=recImage.width()/2-leftIcon.getWidth()/2;
int top=recImage.height()/2-leftIcon.getHeight()/2;
canvas.drawBitmap(leftIcon,recImage.left,top,paintImage);
super.onDraw(canvas);
}
After extending the ScrollView class I was able to easily be notified of the scrolling in realtime.
Now I need to capture the content of this scrollview in a very specific part.
Let's say I want to capture the top of the screen (matching parent width and a defined height, like 100dp). But only the content of the ScrollView and not the rest, if there is anything else on the top but not as part of the ScrollView.
I tried using on the scrollview :
setDrawingCacheEnabled(true);
getDrawingCache(true);
setDrawingCacheEnabled(false);
Then I tried to crop so that I get the part I want :
Bitmap.createBitmap(complete, 0, 0, width, height);
Results are very far from what I want to achieve and performance are very very poor and at some point I would get either a SIGENV or getDrawingCache(true) tries to use a recycled bitmap...
So how can I easily capture the content in the desired area without too much performance hit ?
Note: this process must be done as I am scrolling the content, so inside ScrollView's onScrollChanged(final int x, final int y).
Thanks !
Since the problem was fun I implemented it, it seems to work fine. I guess that you are recreating a Bitmap each time that's why goes slow.
The idea is like this, you create an area in the ScrollView that you want to copy (see Rect cropRect and Bitmap screenshotBitmap), it's full width and you just need to set the height. The view automatically set a scroll listener on itself and on every scroll it will copy that area. Note that setDrawingCacheEanbled(true) is called just once when the view is instantiated, it basically tells the view that you will call getDrawingCache(), which will return the Bitmap on which the view is drawing itself. It then copy the area of interest on screenshotBitmap and that's the Bitmap that you might want to use.
ScreenshottableScrollView.java
package com.example.lelloman.screenshottablescrollview;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
/**
* Created by lelloman on 16-2-16.
*/
public class ScreenshottableScrollView extends ScrollView implements ViewTreeObserver.OnScrollChangedListener {
public interface OnNewScreenshotListener {
void onNewScreenshot(Bitmap bitmap);
}
private Bitmap screenshotBitmap = null;
private Canvas screenshotCanvas = null;
private int screenshotHeightPx = 0;
private OnNewScreenshotListener listener = null;
private Rect cropRect;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public ScreenshottableScrollView(Context context) {
super(context);
init();
}
public ScreenshottableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ScreenshottableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScreenshottableScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init(){
setDrawingCacheEnabled(true);
getViewTreeObserver().addOnScrollChangedListener(this);
}
public void setOnNewScreenshotListener(OnNewScreenshotListener listener){
this.listener = listener;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(screenshotHeightPx != 0)
makeScrenshotBitmap(w,h);
}
public void setScreenshotHeightPx(int q){
screenshotHeightPx = q;
makeScrenshotBitmap(getWidth(), getHeight());
}
private void makeScrenshotBitmap(int width, int height){
if(screenshotBitmap != null) screenshotBitmap.recycle();
if(width == 0 || height == 0) return;
screenshotBitmap = Bitmap.createBitmap(width, screenshotHeightPx, Bitmap.Config.ARGB_8888);
screenshotCanvas = new Canvas(screenshotBitmap);
cropRect = new Rect(0,0,width,screenshotHeightPx);
}
#Override
public void onScrollChanged() {
if(listener == null) return;
Bitmap bitmap = getDrawingCache();
screenshotCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
screenshotCanvas.drawBitmap(bitmap,cropRect, cropRect,paint);
listener.onNewScreenshot(screenshotBitmap);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.lelloman.screenshottablescrollview.MainActivity">
<com.example.lelloman.screenshottablescrollview.ScreenshottableScrollView
android:id="#+id/scrollView"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.example.lelloman.screenshottablescrollview.ScreenshottableScrollView>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#ff000000"/>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
MainActivity.java
package com.example.lelloman.screenshottablescrollview;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringBuilder builder = new StringBuilder();
Random random = new Random();
String AB = "abcdefghijklmnopqrstuvwxyz ";
for(int i=0;i<100;i++){
builder.append("\n\n"+Integer.toString(i)+"\n\n");
for(int j =0;j<1000;j++){
builder.append(AB.charAt(random.nextInt(AB.length())));
}
}
((TextView) findViewById(R.id.textView)).setText(builder.toString());
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
ScreenshottableScrollView scrollView = (ScreenshottableScrollView) findViewById(R.id.scrollView);
scrollView.setScreenshotHeightPx((int) (getResources().getDisplayMetrics().density * 100));
scrollView.setOnNewScreenshotListener(new ScreenshottableScrollView.OnNewScreenshotListener() {
#Override
public void onNewScreenshot(Bitmap bitmap) {
Log.d("MainActivity","onNewScreenshot");
imageView.setImageBitmap(bitmap);
}
});
}
}
I'm creating my Cell view by extending LinearLayout, it's creating parent, but not showing children. I really couldn't find the problem?
Cell cell = new Cell(ctx);
cell.setLetterAndPosition(new Point(1,1), new Letter("A");
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import pe.kor.kelime.Model.Letter;
import pe.kor.kelime.R;
/**
* Created by me on 7/29/15.
*/
public class Cell extends LinearLayout {
private Letter letter;
private Point position; /* 0-3 / 0-3 based position*/
private TextView text;
private boolean touched = false;
public Cell(Context context) {
super(context);
init(context);
}
public Cell(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public Cell(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void setLetterAndPosition(Point position, Letter letter) {
this.letter = letter;
this.position = position;
this.text.setText(letter.getLetter());
}
void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.view_cell, this);
text = (TextView) this.findViewById(R.id.text);
this.setBackgroundColor(Color.parseColor("#ffffff"));
}
public Letter getLetterObject() {
return letter;
}
public void setTouched(boolean e) {
this.touched = e;
if(this.touched) {
this.setBackgroundColor(Color.parseColor("#ff0000"));
}
else {
this.setBackgroundColor(Color.parseColor("#ffffff"));
}
}
public boolean isTouched() {
return touched;
}
public Point getPositionInBoard() {
return position;
}
}
View_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5.5dp"
android:clickable="true">
<TextView
android:id="#+id/text"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#ff0369"
android:baselineAligned="false"
android:clickable="true"
android:gravity="center|center_vertical"
android:includeFontPadding="false"
android:text="A"
android:textColor="#000"
android:textSize="36sp"
android:textStyle="bold"/>
</LinearLayout>
I had to override onLayout method.
onLayout(boolean paramBoolean, int left, int top, int right, int bottom)
#Override
protected void onLayout(boolean paramBoolean, int left, int top, int right, int bottom)
{
int widthOfCell = right - left;
getChildAt(0)
.layout(1,
1,
widthOfCell,
widthOfCell
);
}
I think that the problem is your Textview. You have not added the textview to your linearlayout.
Change your method init
void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.view_cell, this);
text = (TextView) this.findViewById(R.id.text);
this.addView(text);
this.setBackgroundColor(Color.parseColor("#ffffff"));
}
Having a problem with Android Custom components. Trying to draw an oval shape but nothing happening.
I have this line in layout xml file
<android.project.realtimedata.DemoView android:id="#+id/demoView"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
Here is the code for my custom component class.
package android.project.realtimedata;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
public class DemoView extends View{
ShapeDrawable thisGauge = null;
public DemoView(Context context){
super(context);
init();
}
public DemoView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
private void init(){
int x = 10;
int y = 10;
int width = 300;
int height = 50;
thisGauge = new ShapeDrawable(new OvalShape());
thisGauge.getPaint().setColor(0xff74AC23);
thisGauge.setBounds(x, y, x + width, y + height);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
thisGauge.draw(canvas);
}
}
I also have this line in onCreate method of Activity
demoView = (DemoView) findViewById(R.id.demoView);
Whenever I launch the application the custom component is not there.
I tried looking at it from LogCat and it definitely gets created.
What am I missing here?
Thanks in advance.
Make sure that you calling findViewById(R.id.demoView) after calling setContentView(...). To ensure that your view is being inflated, you can call Log.d("DemoView", "Created") from inside your DemoView constructor.