Romi
Romi

Reputation: 4921

How to add images on textview on some event

In my android application, i have a list and a list header. for list header i used this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
    android:weightSum="1">

    <TextView android:text="#" android:layout_height="wrap_content"
        android:layout_width="0dp" android:background="@drawable/back"
        android:layout_weight=".05" android:gravity="center_horizontal"
        android:id="@+id/header_num"/>:
    <TextView android:text="Summary" android:layout_height="wrap_content"
        android:layout_width="0dp" android:background="@drawable/back"
        android:layout_weight=".55" android:gravity="center_horizontal"
        android:id="@+id/header_summ" />
</LinearLayout>

for android:background="@drawable/back":

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#4fa5d5" />


</shape>

Now on the basis of header item, am applying sorting on list item as:

ticketnum = (TextView) findViewById(R.id.header_num);
        ticketnum.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Comparator<Ticket> mc;
                mc = new TicketIdComparator();
                Collections.sort(tickets, mc);
                System.out.println(tickets);
                ticket_adapter = new TicketAdapter(getApplicationContext(),
                        R.layout.ticket_details_row, tickets);
                setListAdapter(ticket_adapter);

                ticket_adapter.notifyDataSetChanged();

            }
        });

Now i want two images on this header, an upward and an downward arrow to show sorting is in ascending or descending order.

please suggest how can i add these two images.

Thanks.

Upvotes: 1

Views: 3273

Answers (1)

Peter Ajtai
Peter Ajtai

Reputation: 57685

You can set an up or down arrow to the left of the text in the textView (to make it look like an icon... I think this is what you want?)

First drop a png in your drawable folder and call it upArrow.png and add one called downArrow.png

Then to set the arrow to show to the left of the a textView text:

myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.upArrow, 0, 0, 0);

See setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) Use the other arguments to set your arrow above, right, or below the text.

Now, if instead you want to change the background image (for example switch out R.drawable.back), then you would also start by saving upArrow.png and downArrow.png in your drawbles folder and then you can call

myTextView.setBackgroundDrawable(R.drawable.upArrow);

See setBackgroundDrawble(Drawable d)

Also, note that (since 2.1) you can put the method call for your onclick listener in your XML. This can make the code cleaner. In you case to add an onclick listenr to headerNum:

 <TextView android:text="#" android:layout_height="wrap_content"
        android:layout_width="0dp" android:background="@drawable/back"
        android:layout_weight=".05" android:gravity="center_horizontal"
        android:id="@+id/header_num" 
        android:onClick="myOnclickListener"/>

Then in your Activity class simply add the method:

public void myOnclickListener(View v) {
    ...
}

You could also programmatically add an imageView, though this is more involved. Or you can add in both the imageViews of up and down arrows next to each header, and turn the visibilities on and off for the appropriate once programatically using:

myImageView.setVisibility(View.VISIBLE);

and

myImageView.setVisibility(View.GONE);

Though I think using bg images or left drawables is the easiest and keeps your xml minimal. If you actually add imageViews, you'd probably want to switch to RelativeLayout.

Upvotes: 1

Related Questions