user1945196
user1945196

Reputation:

facing issues while placing buttons on the right and left of textview

I have created a layout with a button on either side of a textview. This is what I've got so far:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/grid_matrix_table_border_color"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:tag="0"
            android:drawableTop="@drawable/filter_ic"
            android:layout_width="25dp"
            android:layout_height="50dp"
            android:layout_marginRight="2dp"
            android:layout_marginLeft="10dp"
            android:gravity="center"/>

        <com.ui.widget.TextView
            android:id="@+id/txtview"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:layout_height="fill_parent"
            android:layout_marginRight="6dp"
            android:layout_weight="0.36"
            android:focusable="true"
            />

        <Button
            android:id="@+id/btn2"
            android:tag="ASC"
            android:drawableTop="@drawable/sort_ic"
            android:layout_width="25dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginRight="4dp"
            android:background="@color/grid_matrix_table_border_color"/>


    </LinearLayout>

</LinearLayout>

The View looks like:

Button1----- Textview-----Button2

But when I am hiding button1 and replacing with another drawable on onClick, the image is bigger compared to previous button. Also I have inserted the right resolution images inside the drawables. Below is the code

final Button btn1 = (Button) container.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                btn1.setBackgroundResource(R.drawable.btn_grey);
            }
        });

Upvotes: 0

Views: 37

Answers (2)

user1945196
user1945196

Reputation:

I have resolved my drawable image scaling issue by making the LinearLayout to Relative Layout and also it is always better to take ImageButton instead of Button if there is a image for the button.

Upvotes: 1

Avinash Joshi
Avinash Joshi

Reputation: 1308

here is how you do it

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="horizontal">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="2dp"
        android:layout_weight="2"
        android:gravity="center"
        android:tag="0" />

    <com.ui.widget.TextView
        android:id="@+id/txtview"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dp"
        android:layout_weight="3"
        android:focusable="true"
        android:gravity="center"
        android:scrollHorizontally="true"
        android:singleLine="true" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_marginRight="4dp"
        android:layout_weight="2"
        android:tag="ASC" />


    </LinearLayout>

</LinearLayout>

just adjust the weights to get a desired size of the buttons and text box

Upvotes: 0

Related Questions