0

I have two buttons in xml like this:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">





    <Button
       android:background="@drawable/button1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"/>

    <Button
        android:background="@drawable/button2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"/>


    </LinearLayout>





</ScrollView>

However the buttons appear square while my image's height is 1/3 of its width. How can I make the buttons height 1/3 of its width?

2 Answers2

0

Why don't you use ImageButton instead of Button and then set the height as match_parent.

Something like this :

   <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • The difference with just Button is that I can set button's height with "dp" (something thath cant happen with ImageButton) but it won't fit in all screens – Thanos Tokmakis Jun 13 '16 at 19:22
0

Try it like this:

Button b = findViewById(R.id.button);
b.setHeight(b.getWidth() / 3);
victorantunes
  • 1,141
  • 9
  • 20
  • this will return zero. you should do it like here: http://stackoverflow.com/questions/4142090/how-to-retrieve-the-dimensions-of-a-view/4406090#4406090 – DAVIDBALAS1 Jun 13 '16 at 19:29
  • No.. nothing happened.. I also create a button programmatically but without success... It appears square... – Thanos Tokmakis Jun 13 '16 at 20:09