Wednesday, 1 August 2018

Android- Textview getting disappeared on drag operation

I am creating one image editing application. In which I am giving facility to open camera or gallery to select picture. On selection of picture, user will navigate to other page. In other page to show picture I am using one view which implements AppCompactImageView. Now on it, I am giving facility to add text by using edittext. On click of "Done" button of soft keyboard, edittext will get disappeared and will get replaced by TextView. Now I want to drag this TextView all over the layout. But after drop operation, it's getting disappeared. Below is my code. Any help will be appreciable.

  1. DrawActivity.java

    public class DrawActivity extends AppCompatActivity {

    DrawView imgView;
    Button resetBtn, saveBtn;
    ImageButton undoBtn;
    Bundle extras;
    Context context;
    EditText addTxtBox;
    ImageView brushImg, fontImg;
    TextView addedTxtView;
    // LinearLayout mainLinear;
    RelativeLayout mainRelative;
    public int rowX, rowY;
    public String txtVal;
    private android.widget.RelativeLayout.LayoutParams layoutParams;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_draw);
    
        context = this.getApplicationContext();
    
        mainRelative = (RelativeLayout) findViewById(R.id.relativeLayout1);
        imgView = (DrawView) findViewById(R.id.frag_home_iv_main);
        resetBtn = (Button) findViewById(R.id.clearBtn);
        undoBtn = (ImageButton) findViewById(R.id.undoBtn);
        saveBtn = (Button) findViewById(R.id.saveBtn);
        addTxtBox = (EditText) findViewById(R.id.addTxt);
        brushImg = (ImageView) findViewById(R.id.imageBrush);
        fontImg = (ImageView) findViewById(R.id.imageFont);
    
        addedTxtView = (TextView) findViewById(R.id.drawTextView);
        addedTxtView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
                String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
    
                ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
                View.DragShadowBuilder myShadow = new View.DragShadowBuilder(addedTxtView);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Log.d("Vishal sdk","in if");
                    v.startDragAndDrop(dragData, myShadow, null, 0);
                } else {
                    Log.d("Vishal sdk","in else");
                    v.startDrag(dragData, myShadow, null, 0);
                }
                return false;
            }
        });
    
    
        addedTxtView.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                Log.d("Vishal check", String.valueOf(event.getAction()));
                switch(event.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
                        Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_STARTED");
                        return true;
                        // Do nothing
    
                    case DragEvent.ACTION_DRAG_ENTERED:
                        Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENTERED");
                        int x_cord = (int) event.getX();
                        int y_cord = (int) event.getY();
                        return true;
    
                    case DragEvent.ACTION_DRAG_EXITED :
                        Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_EXITED");
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        layoutParams.leftMargin = x_cord;
                        layoutParams.topMargin = y_cord;
                        v.setLayoutParams(layoutParams);
                        v.setVisibility(View.VISIBLE);
                        // view.setVisibility(View.VISIBLE);
                        return true;
    
                    case DragEvent.ACTION_DRAG_LOCATION  :
                        Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_LOCATION");
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        return true;
    
                    case DragEvent.ACTION_DRAG_ENDED   :
                        Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENDED");
                        return true;
                        // Do nothing
    
                    case DragEvent.ACTION_DROP:
                        Log.d("Vishal Drag", "ACTION_DROP event");
                       /* x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        View view = (View) event.getLocalState();
                        view.setX(x_cord - (view.getWidth() / 2));
                        view.setY(y_cord - (view.getWidth() / 2));
                        view.setVisibility(View.VISIBLE);*/
    
                        return true;
                        // Do nothing
                    default:
                        return true;
    
                }
                //return true;
            }
        });
    
    
        addTxtBox.setImeOptions(EditorInfo.IME_ACTION_DONE);
        addTxtBox.setRawInputType(InputType.TYPE_CLASS_TEXT);
    
        addTxtBox.setVisibility(View.GONE);
        // addedTxtView.setVisibility(View.GONE);
    
    
        extras = getIntent().getExtras();
        imgView.setImageURI(Uri.parse(extras.getString("selectedImg")));
    
        imgView.setDrawingCacheEnabled(true);
    
        brushImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgView.setBrushActive(true);
            }
        });
    
        fontImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgView.setBrushActive(false);
                addTxtBox.setVisibility(View.VISIBLE);
                addTxtBox.requestFocus();
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
    
        addTxtBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(DrawActivity.this, addTxtBox.getText(), Toast.LENGTH_SHORT).show();
                    addedTxtView.setVisibility(View.VISIBLE);
                    txtVal = addTxtBox.getText().toString();
                    imgView.getTxtValue(addTxtBox.getText().toString());
                    addedTxtView.setText(addTxtBox.getText().toString());
                    addedTxtView.setTag(addTxtBox.getText().toString());
                    addTxtBox.setVisibility(View.GONE);
                    try {
                        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(addTxtBox.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return true;
                }
                return false;
            }
        });
    
    
        resetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgView.resetPaths();
            }
        });
    
        undoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgView.removeLastPath();
            }
        });
    
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = imgView.getDrawingCache();
                File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                path.mkdirs();
                Long tsLong = System.currentTimeMillis()/1000;
                String ts = tsLong.toString();
                File imageFile = new File(path, ts+".png"); // Imagename.png
    
                try{
                    FileOutputStream out = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
                    try {
                        out.flush();
                        out.close();
                    } catch (IOException e){
                        e.printStackTrace();
                    }
    
    
                    // Tell the media scanner about the new file so that it is
                    // immediately available to the user.
                    MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
    
                        }
                    });
                    Toast.makeText(context,"Saved", Toast.LENGTH_SHORT).show();
    
                    Intent intent = new Intent(DrawActivity.this, MainActivity.class);
                    startActivity(intent);
                } catch(FileNotFoundException e) {
                    Toast.makeText(context,"Error" + e.toString(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
    
    }
    
    
    
    public void redrawImage() {
        Bitmap bm = imgView.getDrawingCache();
        Bitmap proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(proxy);
        Paint paint = new Paint();
        // paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        paint.setTextSize(100);
        paint.setAntiAlias(true);
    
        c.drawBitmap(bm, new Matrix(), paint);
        c.drawText(txtVal, imgView.getWidth()+rowX, imgView.getHeight()+rowY, paint);
        imgView.setImageBitmap(proxy);
    }
    
    public void changeColor(View view) {
        Log.d("Vishal selected color", view.getTag().toString());
    
        switch (view.getTag().toString()){
            case "red":
                imgView.setBrushColor(Color.RED);
                break;
    
            case "blue":
                imgView.setBrushColor(Color.BLUE);
                break;
    
            case "green" :
                imgView.setBrushColor(Color.GREEN);
                break;
    
            case "yellow":
                imgView.setBrushColor(Color.YELLOW);
                break;
    
            case "purple":
                imgView.setBrushColor(getResources().getColor(R.color.purple));
                break;
    
            default:
                imgView.setBrushColor(Color.BLACK);
        }
        //
    }
    
    

    }

  2. activity_draw.xml

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >
    
        <ImageButton
            android:id="@+id/undoBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/undo"/>
    
        <Button
            android:id="@+id/clearBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:layout_toLeftOf="@+id/saveBtn"/>
    
        <Button
            android:id="@+id/saveBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save"
            android:layout_alignParentRight="true"/>
    
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.8"
        android:orientation="vertical"
        android:id="@+id/draw_image_linearView">
    
        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.example.mobilesolution.imgedit.DrawView
                android:id="@+id/frag_home_iv_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"/>
    
            <TextView
                android:id="@+id/drawTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:elevation="100dp"
                android:layout_centerInParent="true"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                />
    
            <EditText
                android:id="@+id/addTxt"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter some text"
                android:inputType="textMultiLine"
                android:maxLines="3"
                android:elevation="10dp"/>
    
    
        </RelativeLayout>
    
    </RelativeLayout>
    
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:scrollbars="none"
        android:layout_weight="0.2">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="447dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="10dp">
    
                <ImageView
                    android:id="@+id/redBall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:onClick="changeColor"
                    android:src="@drawable/red_ball"
                    android:tag="red" />
    
                <ImageView
                    android:id="@+id/blueBall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:onClick="changeColor"
                    android:src="@drawable/blue_ball"
                    android:tag="blue" />
    
                <ImageView
                    android:id="@+id/greenBall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:onClick="changeColor"
                    android:src="@drawable/green_ball"
                    android:tag="green" />
    
                <ImageView
                    android:id="@+id/yellowBall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:onClick="changeColor"
                    android:src="@drawable/yellow_ball"
                    android:tag="yellow" />
    
                <ImageView
                    android:id="@+id/purpleBall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:onClick="changeColor"
                    android:src="@drawable/purple_ball"
                    android:tag="purple" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/red_ball" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/red_ball" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/red_ball" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/green_ball" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/green_ball" />
    
            </LinearLayout>
        </LinearLayout>
    
    
    </HorizontalScrollView>
    
    
    <RelativeLayout
        android:id="@+id/brushBtnLayout"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:background="@android:color/black">
    
        <ImageView
            android:id="@+id/imageBrush"
            android:layout_width="65dp"
            android:layout_height="37dp"
            android:layout_marginLeft="66dp"
            android:layout_marginStart="66dp"
            android:background="@android:color/white"
            android:src="@drawable/brush"
            android:layout_alignTop="@+id/imageFont"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    
        <ImageView
            android:id="@+id/imageFont"
            android:layout_width="65dp"
            android:layout_height="37dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="52dp"
            android:layout_marginRight="52dp"
            android:background="@android:color/white"
            android:src="@drawable/font" />
    
    </RelativeLayout>
    
    


from Android- Textview getting disappeared on drag operation

No comments:

Post a Comment