-1

I've tried with below code

dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView);

still, it is not exact size what I want. These things are the same for CheckBox view as well. I need to draw this view with an exact pinpoint position with size.

Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
Swapon
  • 409
  • 6
  • 16

3 Answers3

1

Try this way:

dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int) measureWidth, (int)measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView, params);
Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • It worked with TextView. Thank you. Though it is not worked with CheckBox, (CheckBox has no text). Is there any solution for this? – Swapon Apr 04 '19 at 07:53
  • You have to create a separate params object for the CheckBox, give it the rules it needs to obey, and add the CheckBox to the rootview the same way that with the TextView. – Fustigador Apr 04 '19 at 07:56
  • Followed same way but somehow it is not working, creating ChecbBox with default width, height dCheckBox = new CustomCheckBox(getContext()); CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors)); dCheckBox.setChecked(item.isCheckState()); rootView.addView(dCheckBox,new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight)); – Swapon Apr 04 '19 at 08:11
  • You want to create the CheckBox with the same dimensions than TextView...? – Fustigador Apr 04 '19 at 08:59
  • Have you seen this? https://stackoverflow.com/questions/2151241/android-how-to-change-checkbox-size – Fustigador Apr 04 '19 at 09:00
0

Why do you need to do it like this.

You could create a textview component and add it programmatically to your UI with the help of ListView, Recyclerview etc.

  • I've a PDF file and that PDF file has a blank form input and I need to fill those form using dynamic textview or checkbox or edittext and also it needs to scale up when user zoom in or out. I've already fixed the zoom in and out with PDF so all the view holding current position but I need exact pin point width and heith of view. – Swapon Apr 04 '19 at 07:37
0

Finally, I've created all views (Checkbox, EditText, and TextView) with exact width and height. EditText and TextView was an easy solution:

       dEditText = new EditText(getContext());
        dEditText.setPadding(10,5,10,5);
        dEditText.setTextSize(6);
        dEditText.setTextColor(Color.WHITE);
        dEditText.setBackgroundColor(Color.GRAY);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                (int) measureWidth, (int) measureHeight);
        dEditText.setLayoutParams(params);
        rootView.addView(dEditText);

        setMargin(dEditText, item,params);


// set margin of every created view
private void setMargin(View view, TagsItem item,RelativeLayout.LayoutParams layoutParams) {



    layoutParams.setMargins(item.getCurrentXPos(), item.getCurrentYPos(), 0, 0);

    // Apply the updated layout parameters to TextView
    view.setLayoutParams(layoutParams);
}

But when I try to create CheckBox with exact width and height with this procedure it didn't create a view with exact width and height. I need to follow the below code[Need to set the background and set null for setButtonDrawable ]

 int states[][] = {{android.R.attr.state_checked}, {}};
        int colors[] = {Color.BLACK, Color.BLACK};

        dCheckBox = new CheckBox(getContext());
        CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors));
        dCheckBox.setChecked(item.isCheckState());
        //dCheckBox.setText("");
        dCheckBox.setBackgroundResource(R.drawable.check_box_selector);
        dCheckBox.setButtonDrawable(null);



        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                (int) measureWidth, (int) measureHeight);
        dCheckBox.setLayoutParams(params);
       // checkBoxLayout.addView(dCheckBox);
        rootView.addView(dCheckBox);

        setMargin(dCheckBox, item,params);
Swapon
  • 409
  • 6
  • 16