0

I have this layout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="MyPackage.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:id="@+id/content_main">


    <TextProgress
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textprogress_day"
        android:background="@drawable/layout_border">
    </TextProgress>


</android.support.constraint.ConstraintLayout>

I want to create dynamically TextProgerss(My widget) without xml

TextProgress class

   public class TextProgress extends ConstraintLayout {
        private TextView mTextView;
        private ProgressBar mProgressBar;

        public TextProgress(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            this.setBackgroundColor(Color.GREEN);

            initializeViews(context);
        }
.
.
.

I tried below code

    ConstraintLayout cl= (ConstraintLayout) findViewById(R.id.content_main);
            TextProgress tp=new TextProgress(this,null);
            ConstraintLayout.LayoutParams tpParams=new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
            tp.setLayoutParams(tpParams);
            cl.addView(tp);

But MATCH_PARENT does not work right

and I don't know, how to create dynamically below atrribute

android:background="@drawable/layout_border"

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kaveh
  • 11
  • 5

1 Answers1

0

I was able to solve part of the problem. MATCH_PARENT problem solved look below code

 ConstraintLayout cl= (ConstraintLayout) findViewById(R.id.content_main);
            TextProgress tp=new TextProgress(this,null);
            ConstraintLayout.LayoutParams tpParams=new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
            tp.setLayoutParams(tpParams);
            cl.addView(tp);

    ConstraintSet set = new ConstraintSet();

    set.constrainWidth(tp.getId(),ConstraintSet.MATCH_CONSTRAINT);
    set.constrainHeight(tp.getId(),ConstraintSet.WRAP_CONTENT);
    set.connect(tp.getId(), ConstraintSet.LEFT,
            ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
    set.connect(tp.getId(), ConstraintSet.RIGHT,
            ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
        set.applyTo(cl);

the border problem is solved in here

Kaveh
  • 11
  • 5