Frequently Asked Question

Wrapped Content Editbox in Android
Last Updated a year ago

If you want your Editbox annotation to be wrap content instead of having fixed width and height, you need to modify com.radaee.util.PopupEditAct:

  • Setting layout params with WRAP_CONTENT instead of fixed width and height.
  • Adding a listener to pass the new dimensions to the annotation.

Note: this implementation does not handle the case of the new annotation's dimension may override other page content.

package com.radaee.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.radaee.viewlib.R;

public class PopupEditAct extends Activity {

    private float originalWidth;
    private float originalHeight;
    private EditText m_txt;
    private RelativeLayout m_layout;

    public interface ActRetListener
    {
        void OnEditValue(String val);
        void onSizeChanged(int w, int h);
    }
    static public ActRetListener ms_listener;
    private ActRetListener m_listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        m_listener = ms_listener;
        ms_listener = null;
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        setContentView(R.layout.pop_edit);
        m_layout = (RelativeLayout)findViewById(R.id.lay_root);
        m_txt = (EditText)findViewById(R.id.annot_text);
        m_txt.setVisibility(View.INVISIBLE);
        Handler handler = new Handler();
        Runnable runnable=new Runnable(){
            @Override
            public void run() {
                int[] location = new int[2];
                m_layout.getLocationOnScreen(location);
                Intent intent = getIntent();
                float x = intent.getFloatExtra("x", location[0]) - location[0];
                float y = intent.getFloatExtra("y", location[1]) - location[1];
                originalWidth = intent.getFloatExtra("w", 0);
                originalHeight = intent.getFloatExtra("h", 0);
                int type = intent.getIntExtra("type", 0);
                int maxlen = intent.getIntExtra("max", 0);
                float size = intent.getFloatExtra("size", 0);
                m_txt.setText(intent.getStringExtra("txt"));
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.leftMargin = (int)x;
                lp.topMargin = (int)y;
                m_txt.setMinWidth((int) originalWidth);
                m_txt.setMinHeight((int) originalHeight);
                m_txt.setLayoutParams(lp);
                m_txt.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
                BitmapDrawable bitmap = new BitmapDrawable();//add back
                m_txt.setBackgroundDrawable(bitmap);
                m_txt.setBackgroundColor(0xFFFFFFC0);
                m_txt.setPadding(2, 2, 2, 2);
                m_txt.setTextColor(0xFF000000);
                switch (type) {
                    case 1:
                        m_txt.setSingleLine();
                        m_txt.setInputType(InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_NORMAL);
                        break;
                    case 2:
                        m_txt.setSingleLine();
                        m_txt.setInputType(InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        break;
                    case 3:
                        m_txt.setSingleLine(false);
                        m_txt.setInputType(InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_NORMAL + InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                        break;
                }
                if(maxlen > 1020)
                    m_txt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1020)});
                else if (maxlen > 0)
                    m_txt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxlen)});
                else
                    m_txt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1020)});
                m_txt.setVisibility(View.VISIBLE);

                //show the keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(m_txt, InputMethodManager.SHOW_IMPLICIT);
            }
        };
        handler.postDelayed(runnable, 50);
    }

    @Override
    public void onBackPressed() {
        if(m_txt == null || !m_txt.isShown()) return;
        updateAnnot();
        super.onBackPressed();
        overridePendingTransition(0, android.R.anim.fade_out);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if(event.getActionMasked() == MotionEvent.ACTION_UP) {
            if (m_txt == null || !m_txt.isShown()) return false;
            updateAnnot();
            finish();
            overridePendingTransition(0, android.R.anim.fade_out);
        }
        return true;
    }

    private void updateAnnot() {
        Editable value = m_txt.getText();
        if(m_listener != null && (m_txt.getWidth() > originalWidth || m_txt.getHeight() > originalHeight))
            m_listener.onSizeChanged(m_txt.getWidth(), m_txt.getHeight());
        if(value != null && m_listener != null)
            m_listener.OnEditValue(value.toString());
    }
}

In onEditAnnot in com.Radaee.reader.PDFLayoutView, implement the created listener as follows:

@Override
public void onSizeChanged(int w, int h) {
   if(m_annot != null) {
      float[] rect = m_annot.GetRect();
      m_annot.SetRect(rect[0], rect[3] - (h / m_layout.vGetScale()), rect[0] + (w / m_layout.vGetScale()), rect[3]);
   }
}

Loading ...