Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi to all
I have below xml code
   <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

pay attention to below code

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);
    }
.
.
.


What I have tried:

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"
Posted
Updated 10-Dec-17 7:14am
Comments
Richard MacCutchan 10-Dec-17 7:38am    
The properties used in the XML can also be found as properties of the various objects in code. Check the documentation for the class you are referring to.
kave2011 10-Dec-17 8:09am    
Exactly
But "MATCH_PARENT" doest not work right.

1 solution

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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900