Component created does not get the width of the screen

Asked

Viewed 104 times

1

I created a component that would be a bar with buttons at the bottom of the screen. It should respect both in Portrait and in Landscape the width of the screen at least. If the width of the component passes that of the screen I am using a Horizontalscrollview. The component is not getting the width of the screen at least:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn1"
        style="@style/ButtonApp_Theme"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:background="@color/Transparente"
        android:drawableTop="@drawable/1"
        android:padding="10dp"
        android:text="@string/1"
        android:textColor="@color/White"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn2"
        style="@style/ButtonApp_Theme"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="25"
        android:background="@color/Transparente"
        android:drawableTop="@drawable/2"
        android:padding="2dp"
        android:text="@string/2"
        android:textColor="@color/White"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn3"
        style="@style/ButtonApp_Theme"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="25"
        android:background="@color/Transparente"
        android:drawableTop="@drawable/3"
        android:padding="2dp"
        android:text="@string/3"
        android:textColor="@color/White"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn4"
        style="@style/ButtonApp_Theme"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="25"
        android:background="@color/Transparente"
        android:drawableTop="@drawable/4"
        android:padding="2dp"
        android:text="@string/4"
        android:textColor="@color/White"
        android:textSize="16sp" />
</LinearLayout>

In the layout where I insert the component created I am using android:layout_width="fill_parent" and even so is not being respected the width independent of the width (the width of the screen being larger than the component).

public class BottomBarButton extends HorizontalScrollView implements
    OnClickListener {

private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private LayoutInflater inflater;
private Context context;

public BottomBarButton(Context context) {
    super(context);
    intitializer(context);
}

public BottomBarButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    intitializer(context);
}

private void intitializer(Context context) {
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.layout_bottom_bar_button, this);

    btn1 = (Button) findViewById(R.id.btnV1);
    btn1.setOnClickListener(this);
    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setOnClickListener(this);
    btn3 = (Button) findViewById(R.id.btn3);
    btn3.setOnClickListener(this);
    btn4 = (Button) findViewById(R.id.btn4);
    btn4.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
    case R.id.btn1:
        Toast.makeText(context, "btn1", Toast.LENGTH_SHORT)
                .show();
        break;

    case R.id.btn2:
        Toast.makeText(context, "btn2", Toast.LENGTH_SHORT)
                .show();
        break;

    case R.id.btn3:
        Toast.makeText(context, "btn3", Toast.LENGTH_SHORT)
                .show();
        break;

    case R.id.btn4:
        Toast.makeText(context, "btn4", Toast.LENGTH_SHORT)
                .show();
        break;

    default:
        break;
    }
}

}

  • Is using fillViewPort="true" in the HorizontalScrollView?

  • @Wakim, that’s right! I had only informed fillViewPort in the component layout and not in the view layout. Thank you!

  • I will put as an answer, so that I can accept the solution, okay?

  • Okay all right! Thank you!

1 answer

1


To force the HorizontalScrollView occupy all available space in the parent use attribute fillViewport="true" in the component declaration in XML.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.