Search code examples
androidviewandroid-linearlayoutandroid-relativelayout

how to add a horizontal divider programmatically


in the bleow posted code, i am trying to add a horizontal divider after each added view to the linearlayout as shown in the code. the problem i have is, at run time the divider is not showing

would please let me know why the divider is not showing and how to make it appears?

code:

private void inflateView(String bez, String ges) {
    LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);

    //divider
    View viewDivider = new View(this);
    viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    viewDivider.setBackgroundColor(Color.parseColor("#000000"));

    LayoutInflater inflator = this.getLayoutInflater();
    View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key

    ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
    TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
    TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
    TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
    Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
    } else {
        imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
    }

    texVieBez.setText(bez);
    texVieGes.setText(bez);
    btnMore.setVisibility(View.VISIBLE);

    linLay.addView(view);

    linLay.addView(viewDivider);
}

Solution

  • viewDivider has height WRAP_CONTENT and as the view is empty, its height is calculated to 0.

    You have to set desired height of the divider.

    int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
    viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));