How to add Image in Editbox with some text? - java

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 ");
}
}

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=""

The feature you want is known as CompoundDrawable.
There are attributes which you can set inside your EditText:
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
In your case, as you want to display image on right hand side, use android:drawableRight.

I got the image simply with the following method
favButton.setCompoundDrawablesWithIntrinsicBounds( 0, 0,R.drawable.white_star, 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...

Related

Android : Rating bar not occupying complete space available

I am working on an Android application in which I have a RatingBar. Currently for testing, I am setting its value randomly between 1-5. For some reason, the rating bar is always starting from center, and is never occupying the complete space available. Here is how it looks in the app :
The entire space left of rating bar is what I have given in XML, but it's not getting filled.
One other problem with Rating bar is, how can I add a text to left of the RatingBar. Something like Text...(XXXXX). Thank you
Code :
<RatingBar
android:layout_width="135dp"
android:layout_height="40dp"
android:id="#+id/productStars"
android:numStars="5"
android:scaleX="0.3"
android:scaleY="0.3"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:paddingLeft="20dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/description"
android:layout_alignEnd="#+id/description" />
Update
Entire layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="5dip"
android:background="#color/common_action_bar_splitter"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<ImageView
android:layout_width="150dp"
android:layout_height="200dp"
android:id="#+id/productImage"
android:background="#drawable/layout_image"
/>
<TextView
android:id="#+id/comments"
android:layout_width="50dp"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/abc_input_method_navigation_guard"
android:gravity="left"
android:singleLine="true"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_above="#+id/productStars"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/description"
android:layout_alignStart="#+id/description" />
<TextView
android:id="#+id/productName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="left"
android:scrollHorizontally="true"
android:visibility="visible"
android:layout_toEndOf="#+id/productImage"
android:layout_toRightOf="#+id/productImage"
android:textColor="#color/abc_input_method_navigation_guard"
android:typeface="monospace"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:singleLine="true"
/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center_horizontal|top"
android:ellipsize="end"
android:singleLine="true"
android:gravity="left"
android:scrollHorizontally="true"
android:visibility="visible"
android:layout_above="#+id/comments"
android:layout_toEndOf="#+id/productImage"
android:layout_below="#+id/productName"
android:layout_toRightOf="#+id/productImage"
android:textColor="#color/abc_input_method_navigation_guard"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp" />
<RatingBar
android:layout_width="135dp"
android:layout_height="40dp"
android:id="#+id/productStars"
android:numStars="5"
android:scaleX="0.3"
android:scaleY="0.3"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/description"
android:layout_alignEnd="#+id/description" />
<ImageButton
android:layout_width="45dp"
android:layout_height="45dp"
android:id="#+id/swapButton"
android:src="#drawable/swappossible"
android:background="#null"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/productStars"
android:layout_toStartOf="#+id/productStars" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Try to replace your rating bar with this
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/productStars"
android:stepSize="1"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:layout_alignRight="#+id/description"
android:layout_alignEnd="#+id/description" />
Don't give fix width to rating bar, keep it wrap_content. To make rating bar smaller use this instead
style="?android:attr/ratingBarStyleSmall"
Add this to your rating bar
According to your code it should be like this
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/productStars"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:stepSize="1"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/description"
android:layout_alignEnd="#+id/description"
android:layout_gravity="center"/>
Make height wrap_content too and remove scaleX and scaleY too

Android Textview settext issue in Samsung J5

I have a "problem" with my application and I don't know what has happened.
I have this interface:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#color/white"
android:gravity="center"
android:textColor="#color/black" />
<TextView
android:id="#+id/detailServiceTxtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="-35dp"
android:ellipsize="end"
android:gravity="center"
android:maxLength="50"
android:singleLine="true"
android:textColor="#color/black" />
</LinearLayout>
I get the field value from a web service and set text in java code:
m_txtPhone.setText(m_currentService.PassengerPhone);
In all the devices where I check it this is the result:
But I have a problem in with Samsung J5 (5.1.1). When I try to set text in that TextView (and similars) I have this result:
It seems that I have some problem setting the text or I don't know. Any idea?
NOTE:
I implement the interface in this way because in other cases I do something like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<Button
android:id="#+id/detailServiceBtnClient"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#color/white"
android:drawableRight="#drawable/icon_detail"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/detailServiceBtnClient"
android:textColor="#color/black" />
<TextView
android:id="#+id/detailServiceTxtClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="25dp"
android:layout_marginTop="-35dp"
android:ellipsize="end"
android:gravity="center"
android:maxLength="50"
android:singleLine="true"
android:textColor="#color/black" />
</LinearLayout>
You are using LinearLayout. In this layout items are added after each element. Your orientation is vertical, hence, it is below the button. Use RelativeLayout and then use alignTop and alignBottom properties of TextView.
android:alignTop="#+id/button"
android:alignBottom="#+id/button"
android:gravity="center"

How to fix the button position in Linear Layout

I define two components (One text View and one Button) in Linear layout.
<LinearLayout
android:id="#+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/edit_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "15sp"
android:layout_weight="5"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:gravity="center"
android:hint="Write Greeting" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="#+id/edit_view1"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:src="#drawable/e301"/>
</LinearLayout>
If I write long text, the position of my ImageButton is changed. I want to fix the position. Any suggestion?
Replace your layout contents as :
<LinearLayout
android:id="#+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/edit_view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize = "15sp"
android:layout_weight="5"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="Write Greeting" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#drawable/e301"/>
</LinearLayout>
You can't use RelativeLayout's properties of alignment inside Linear layout.
See your modified code:
<LinearLayout
android:id="#+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:singleLine="false"
android:id="#+id/edit_view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize = "15sp"
android:layout_marginTop="20dp"
android:text="etegtdrgrgbvcvbghgfffffffffffffffffffffffffffff"
android:gravity="center"
android:hint="Write Greeting"
android:layout_weight="2"/>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#drawable/ic_launcher"
/>
</LinearLayout>
Keep layout width as 0 dp using weight and weightSum, so it will adjust. Also, keep sengleLine as false for textview.
Output:
Hope this helps.
Try below code:-
<LinearLayout
android:id="#+id/layout_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/edit_view1"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="Write Greeting"
android:textSize="15sp" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/edit_view1"
android:background="#android:color/transparent"
android:src="#drawable/e301" />
</LinearLayout>
Using weight then you must have width = 0dp(horizontal) and heigth = 0dp(vertically)
Use weights: you didn't define weightSum of layout so your weights are now useless. Set parent layout's weightSum to 6 (in order to use your values) or 100 and set children's weights as percents. And change children views' widths to 0dp.
[optional] Set button's width to match_parent so it'll take all remaining space.
In LinearLayout layout_toRightOf, layout_alignParentRight, layout_alignParentLeft are redundant. Use gravity for children views instead. (for Button it'll be `gravity="right").
Use the below coding. Tell me if you want better alignment. I have used default default image for alignment. If you want better than this, then give buttom image also.
<LinearLayout
android:id="#+id/layout_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="#+id/edit_view1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="Write Greeting dfdsfdsfsdfadsfdfadf"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<ImageButton
android:id="#+id/imageButton1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>

Android - strange layout display in custom DialogPreference

I have a custom DialogPreference with my own layout defined. The layout file is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_password_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword">
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm_password_label"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
On the displayed dialog both TextViews are missing:
What is the reason of TextViews not being displayed?
One of the reason for this behavior is due to manufacturer's customization of themes. Other reasons could be that the selected text appearance has same color as of background. To avoid this, you may simply set a color to your TextView, for e.g.:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_password_label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
*Also make sure android:text does have a text to show on screen

Android: auto-resize for each device

I've create a very simple app and i download it in 15 different devices. For some of them the layout have no resize problem and appear like this:
Layout that should be in all layouts
But when i installed the app in a Sansung Galaxy Neo Plus and in the tablet Archos 101 Copper the items' layout were not in the position that i want. In the tablet are up of the background different color view and in the Galaxy Neo Plus are like this:
Layout in the Galaxy Neo Plus
To auto-resize, in the activities xml, i used thse codes:
android:layout_marginTop="125dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
to set the buttons' margin from top/bottom/left/right
android:textSize="30dp"
to set the size of buttons' text
The layout code is this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Al meglio di:"
android:textSize="22dp"
android:id="#+id/almegliodi"
android:layout_alignParentTop="true"
android:layout_marginTop="220dp"
android:layout_centerHorizontal="true"
android:typeface="serif"
android:textStyle="bold|italic"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" UNO "
android:textStyle="bold"
android:textSize="35dp"
android:textColor="#android:color/white"
android:id="#+id/uno"
android:background="#android:drawable/dialog_holo_dark_frame"
android:onClick="uno"
android:layout_below="#+id/almegliodi"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" TRE "
android:textStyle="bold"
android:textSize="35dp"
android:textColor="#android:color/white"
android:id="#+id/tre"
android:background="#android:drawable/dialog_holo_dark_frame"
android:onClick="tre"
android:layout_centerHorizontal="true"
android:layout_below="#+id/uno"
android:layout_marginTop="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" CINQUE "
android:textStyle="bold"
android:textSize="35dp"
android:textColor="#android:color/white"
android:id="#+id/cinque"
android:background="#android:drawable/dialog_holo_dark_frame"
android:onClick="cinque"
android:layout_centerHorizontal="true"
android:layout_below="#+id/tre"
android:layout_marginTop="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ESCI"
android:textStyle="bold"
android:textSize="30dp"
android:id="#+id/esci"
android:background="#android:color/transparent"
android:layout_marginBottom="7dp"
android:layout_marginLeft="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:onClick="esci"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INDIETRO"
android:textStyle="bold"
android:textSize="30dp"
android:textColor="#android:color/white"
android:id="#+id/indietro"
android:background="#android:color/transparent"
android:onClick="indietro"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignTop="#+id/esci"
android:layout_marginRight="30dp"
android:layout_marginBottom="7dp"
/>
</RelativeLayout
I searched on google but i found that use the 'dp' is the best way to auto-resize margin and text in all device.
What are my mistakes? What i have to do to solve the compatibility?
Thank you
Here is an example how your layout could look like:
<?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:background="#005"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:src="#android:drawable/btn_star_big_on"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_margin="24dp"
android:layout_weight="1"
android:background="#cfff"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
<android.support.v4.widget.Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
</LinearLayout>
</LinearLayout>
You should of cause use #string/ for each button to make your app translatable, but that basic layout should give you the right idea how to make a layout which scales correctly.
Basically I use some linear layouts which just say how the views should been arranged (horizontal or vertical). Then I told the view which contains the three main buttons that its height should be zero (just for performance) and the most important part that the layout weight is 1 (android:layout_weight="1") that tells the system to split up the open space to all views with a weight attribute. So this one will fill your display as far as possible. The other views will still been rendered as you want in my case wrap_content.

Categories