Question

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!

Was it helpful?

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

OTHER TIPS

Use a style for the spinner, the style you used in the picture is:

style="@android:style/Widget.Holo.Light.Spinner"

You can write your own SpinnerAdapter for the Spinner (you should extend BaseAdapter for this). Implement getView() to define the view that the spinner shows as the selected item. Implement getDropDownView() to define the views that the spinner dropdown shows. You can easily make getView() return something like a LinearLayout to have text on the left and the right.

Below link work for me.

https://android--code.blogspot.in/2015/08/android-spinner-border-between-items.html

res/drawable/spinner_item_border.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff2632"/>
        </shape>
    </item>
    <item android:bottom="2dp">
    <shape android:shape="rectangle">
        <solid android:color="#d7ffa2"/>
    </shape>
</item>

and then

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@drawable/spinner_item_border"
    />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top