0

Dynamically added buttons to a linearlayout in my toolbar do render a weird way : It looks like the width of the button created in my code and passed to my view has a random width. I have set layoutparams to "wrap content" but it isn't making any difference. Any clue on how to fix it ? I'd like my buttons to fit to the text they contain, not bigger nor smaller.

Toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar    xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:minHeight="?attr/actionBarSize"
  android:background="@color/Blue"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  <LinearLayout
    android:id="@+id/layout_toolbar"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="fill_parent">
    <Button
      android:id="@+id/app_name_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingTop="3dp"
      android:background="@android:color/transparent"
      android:textColor="@android:color/white"
      android:textSize="14dp"
      android:textStyle="bold"
      android:gravity="left|center_vertical"
      android:text="@string/app_name" />

      <!-- dynamically button will be here-->
   </LinearLayout>
</android.support.v7.widget.Toolbar>`

My code to add the buttons :

var layout_toolbar = FindViewById<LinearLayout>(Resource.Id.layout_toolbar);

//Add arrow image
ImageView arrow = new ImageView(context);
arrow.SetBackgroundResource(Resource.Drawable.arrow);
arrow.Id = ariane_lvl_count;

layout_toolbar.AddView(arrow);

//set layoutparams for arrow image
LinearLayout.LayoutParams lp_arrow = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
lp_arrow.Gravity = GravityFlags.Left|GravityFlags.CenterVertical;
lp_arrow.LayoutParameters = lp_arrow;

//Add button
Button btn = new Button(this);
btn.Id = ariane_lvl_count;
btn.TextSize = 15;
var folder = (currentFolder + gridViewString[e.Position] + "/");
btn.Hint = folder;
btn.Text = gridViewString[e.Position];
btn.SetBackgroundColor(Color.Yellow);
btn.SetSingleLine(true);
btn.SetTextColor(Resources.GetColor(Color.Yellow));

layout_toolbar.AddView(btn);

//set  layoutparams for button
LinearLayout.LayoutParams lp_btn = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.FillParent);
lp_btn.Gravity = GravityFlags.Left|GravityFlags.CenterVertical;
btn.LayoutParameters = lp_btn;

How it renders : elements underlined in red take too much space and do not fit to text like others do

elements underlined in red take too much space and do not fit to text like others do

SirPsycho
  • 67
  • 1
  • 7

1 Answers1

0

i have an idea for you. Why not try to create sample layouts (example : a button layout) and load it into your Toolbar with layoutInflater. Or make a weightSum in the toolbar.

Understanding Android LayoutInflater Understanding Android weightSum

Community
  • 1
  • 1
  • Thanks for helping but the weightsum is not what I need, I already have used it somewhere else. I gave it a try with the inflater but it behaves the same way. – SirPsycho Mar 23 '17 at 15:57