Search code examples
androidandroid-layouttextviewspinnerstyling

How to underline TextView next to Spinner (continue Spinner background for TextView)


I'm trying to create this UI:

hard to build UI

That is to say, there is a TextView on the left side and a Spinner on the right. And both are underlined with a single line. A Spinner default style already has underline (with the style and background image). So the task is to continue somehow it's background to a TextView.

Neeed your help, guys!


Solution

  • //example main

    public class MyActivity extends Activity {
    
        private Spinner spinner;
        private TextView text;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            spinner = (Spinner) findViewById(R.id.spinner);
            text = (TextView) findViewById(R.id.text);
    
            List<String> list = new ArrayList<String>();
            list.add("A");
            list.add("B");
            list.add("C");
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            spinner.setAdapter(adapter);
    
    
        }
    }
    

    //example R.layout.custom_spinner_item

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerItemStyle"
        android:singleLine="true"
        android:gravity="right"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee" />
    

    //example R.layout.main

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <!--stuff-->
        <View
            android:id="@+id/anotherField1"
            android:layout_width="match_parent"
            android:layout_height="40dp" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="?android:spinnerStyle"
            android:addStatesFromChildren="true"
            >
    
            <TextView
                android:layout_alignParentLeft="true"
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text Left" />
    
            <Spinner
                android:id="@+id/spinner"
                android:layout_alignParentRight="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                />
    
        </RelativeLayout>
    
        <!--stuff-->
        <View
            android:id="@+id/anotherField2"
            android:layout_width="match_parent"
            android:layout_height="40dp" />
    
    </LinearLayout>
    

    Results:

    pic1pic2