0

I am trying to add the image in Edit box with some text, but it is not showing the image. It is showing only texts.

Here is my xml.

 <Button
     android:id="@+id/favButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Add to "
     android:drawableRight="@drawable/white_star"
     android:layout_below="@id/priceText"
     android:layout_alignParentLeft="true"
     android:background="@drawable/rounded_corner_black"
     android:padding="10dip"
     style="@style/ButtonText"
     android:onClick="onAddFavClick" />

Here is my Java Code.

 private void updateFavButton() {
    if (atmItem.isFav()) {

      favButton.setText("Remove from ");

    } else {

      favButton.setText("Add to ");

    }
 }
joselufo
  • 3,393
  • 3
  • 23
  • 37
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36

4 Answers4

0

You can try this way also

<EditText

    android:drawableLeft="@drawable/your_image" />

Or for a reference you can StackOverFlow Answer

So for that you can use android:drawableRight=""

Community
  • 1
  • 1
Developer
  • 6,292
  • 19
  • 55
  • 115
0

The feature you want is known as CompoundDrawable.

There are attributes which you can set inside your EditText:

In your case, as you want to display image on right hand side, use android:drawableRight.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

This is the simple way if you want to go forward with XML

<RelativeLayout 
    android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchcards_back_relative" >

<ImageView 
                android:id="@+id/leftbutton"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="46dp"
                android:background="@drawable/searchleft"/>    

<LinearLayout android:focusable="true"
    android:id="@+id/searchcards_edit_search_lin"
    android:focusableInTouchMode="true" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/leftbutton"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="0px"
    android:background="@drawable/searchmiddle">

    <EditText 
        android:hint="Search Cards" 
        android:id="@+id/searchcards_edit_search"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:textColor="#000000" 
        android:textSize="18dp"
        android:singleLine="true"
        android:background="#0fff"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="21dp"
        android:gravity="center_vertical"
        android:layout_marginTop="0dp"
        android:imeOptions="actionSearch"
        android:layout_gravity="center_vertical"/>

</LinearLayout>
<ImageView 
            android:id="@+id/rightbutton"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="46dp"
            android:background="@drawable/searchright"/> 

    <TextView  
        android:id="@+id/searchcards_back_hitsearch"
        android:layout_width="35dp" 
        android:layout_height="40dp"
        android:textSize="25px"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white"
        android:layout_alignParentRight="true"  
        android:background="#0222"    
        android:text=""/>

</RelativeLayout>

<ListView
    android:id="@+id/searchcards_list"
    android:layout_width="wrap_content"
    android:background="@drawable/background"
    android:layout_height="fill_parent"
    android:layout_below="@+id/edittext"
    android:divider="#fff"
    android:dividerHeight="1px"
    android:cacheColorHint="@android:color/transparent"
        android:layout_marginBottom="4px"
    android:scrollbars="none"   />

 </RelativeLayout>

you should use .png images in drawable folder for left, middle and right images. you can also take relative layout for doing this ... for more keep googling...

Kumar Bankesh
  • 296
  • 4
  • 27
0

I got the image simply with the following method

    favButton.setCompoundDrawablesWithIntrinsicBounds( 0, 0,R.drawable.white_star, 0);
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36