0

I'm trying to poisition some textviews below another text view within a RelativeLayout, but for some reason it's not working:

enter image description here

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.TextViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.companyname.projectname.R;

import static com.companyname.projectname.R.id.FL_relativeLayout;


public class FragmentFL extends android.support.v4.app.Fragment {

    public FragmentFL() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_fl, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        RelativeLayout relativelayout = v.findViewById(FL_relativeLayout);

        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        // add text views
        TextView txt1 = new TextView(getActivity());
        txt1.setText("Blue");
        TextViewCompat.setTextAppearance(txt1, android.R.style.TextAppearance_Large);
        txt1.setTextColor(Color.BLACK);

        TextView txt2 = new TextView(getActivity());
        txt2.setText("Black");
        TextViewCompat.setTextAppearance(txt2, android.R.style.TextAppearance_Medium);
        txt2.setTextColor(Color.BLACK);

        TextView txt3 = new TextView(getActivity());
        txt3.setText("Green");
        TextViewCompat.setTextAppearance(txt3, android.R.style.TextAppearance_Large);
        txt3.setTextColor(Color.BLACK);

        TextView txt4 = new TextView(getActivity());
        txt4.setText("Red");
        TextViewCompat.setTextAppearance(txt4, android.R.style.TextAppearance_Medium);
        txt4.setTextColor(Color.BLACK);

        TextView txt5 = new TextView(getActivity());
        txt5.setText("Yellow");
        TextViewCompat.setTextAppearance(txt5, android.R.style.TextAppearance_Large);
        txt5.setTextColor(Color.BLACK);

        TextView txt6 = new TextView(getActivity());
        txt6.setText("White");
        TextViewCompat.setTextAppearance(txt6, android.R.style.TextAppearance_Medium);
        txt6.setTextColor(Color.BLACK);

        txt1.setId(View.generateViewId());
        txt2.setId(View.generateViewId());
        txt3.setId(View.generateViewId());
        txt4.setId(View.generateViewId());
        txt5.setId(View.generateViewId());
        txt6.setId(View.generateViewId());

        rlp.addRule(RelativeLayout.BELOW, txt1.getId());
        rlp.addRule(RelativeLayout.BELOW, txt2.getId());
        rlp.addRule(RelativeLayout.BELOW, txt3.getId());
        rlp.addRule(RelativeLayout.BELOW, txt4.getId());
        rlp.addRule(RelativeLayout.BELOW, txt5.getId());
        txt2.setLayoutParams(rlp);
        txt3.setLayoutParams(rlp);
        txt4.setLayoutParams(rlp);
        txt5.setLayoutParams(rlp);
        txt6.setLayoutParams(rlp);

        relativelayout.addView(txt1);
        relativelayout.addView(txt2);
        relativelayout.addView(txt3);
        relativelayout.addView(txt4);
        relativelayout.addView(txt5);
        relativelayout.addView(txt6);


        super.onActivityCreated(savedInstanceState);
    }
}
1
  • where do you specify the alignment? If you want the text view below each other then why not use LinearLayour with vertical orientation? Commented Aug 18, 2017 at 18:52

1 Answer 1

2

You need to use different LayoutParams for each TextView as in

 rlp.addRule(RelativeLayout.BELOW, txt1.getId());
 rlp1.addRule(RelativeLayout.BELOW, txt2.getId());
 rlp2.addRule(RelativeLayout.BELOW, txt3.getId());
 rlp3.addRule(RelativeLayout.BELOW, txt4.getId());
 rlp4.addRule(RelativeLayout.BELOW, txt5.getId());

    txt2.setLayoutParams(rlp);
    txt3.setLayoutParams(rlp1);
    txt4.setLayoutParams(rlp2);
    txt5.setLayoutParams(rlp3);
    txt6.setLayoutParams(rlp4);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.