Android edit text with custom shape - android

How can I edit text shape like this.
I mean that right rounded corners and exact same shape. As I understood 9 patch doesn't help me with that.

9 patch will be best way in your situation, create 9 patch and set it as background of your EditText.
Here i am giving you sample image, you can try with it.
editText.setBackground(R.drawable.edt_bg);

This ninepatches will help you. just apply this ninepatch images as backgroud of the editText

You have to create background image for your EditText. For the other layout, you have to create layout which will look like this. Its not the exact xml but showing you the idea.
<RelativeLayout layout_width="match_parent" layout_height="50dp">
<EditText layout_width="wrap_content" layout_height="wrap_content"
android:background="#drawable/editTextBackground" layout_alignParentLeft="true" layout_marginLeft="10dp">
<ImageView layout_width="wrap_content" layout_height="wrap_content"
centerVertical="true" marginLeft="15dp"
android:background="#drawable/smily" />
<ImageView layout_width="wrap_content" layout_height="wrap_content"
centerVertical="true"
alignRight="#id/editTExt"
android:background="#drawable/camera" />
<ImageView layout_width="wrap_content" layout_height="wrap_content"
centerVertical="true" marginLeft="15dp"
alignParentRight="true"
android:background="#drawable/mic" />
</RelativeLayout>
Hope this gives you the idea.

Related

how to make a button with a picture and text?

I need to create a button with the background and place it on the picture. I tried to create LinearLayout Horizontal bet on it, and put the background image and button. but it looks not nice. Tell me how to do this?
No layout is needed here, any TextViews can have drawableLeft attribute. Use appropriate XML attributes to set picture, padding, gravity of the text, etc.
Use button's xml attributes to set a drawable according to your need, and in your case it is drawableLeft. See the example below to make a think like your purpose :
<Button
android:id="#+id/backButton"
android:drawableLeft="#drawable/back_btn"
android:background="#android:color/transparent"
android:text="#string/home_screen_sell_new_card_back_button_text"
android:textColor="#026281"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And for the little enhancements, put paddings and other stuff.

Compound TextView Padding Issue

Please refer to this image:
The Top image is a TextView with no compound drawable and the padding all around seems perfect.
The second image has a compound image using android:drawableRight="#drawable/ic_action_expand" and the image messes up all the padding.
How to get rid of this unwanted padding in the second image ?
Here's the XML
<TextView
android:id="#+id/moreinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/translucent_black"
android:drawableRight="#drawable/ic_action_expand"
android:drawablePadding="#dimen/content_heading_padding"
android:singleLine="true"
android:padding="#dimen/content_heading_padding"
android:textSize="#dimen/content_size"
android:text="#string/more_info" />
As you can see in the XML i have used android:drawablePadding
Including or removing that line does changes the padding for the drawable but the mess that has happened for the textview still remains.
I have tried negative padding values for android:drawablePadding but it does not correct the issue.
One of the reasons could be that it must be set autoresizable(true) in your android manifest file set to autoresizable(true). Check your manifest.

How can I stretch the image in imagebutton to the whole area of the imagebutton?

Here is a picture that will help you understand my problem:
I want to stretch the image shown inside ImageButton to the whole area of the ImageButton.
As you can see, the picture with the number 1 only take about 95% of the ImageButton and you can also see the boundaries of the ImageButton.
I still want to maintain the image button style
Here's how I am using ImageView:
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:src="#drawable/111" />
I think I found out what is my problem.
I need a custom component that can contain only an image and have a clicking effect.
ImageButton is ImageView.
If you want your image to fill button
than set android:scaleType="fitXY", so the image you set in android:src will be stretched to fill whole image button.
ImageButton has a background under your image and padding.
If you do not want background
set android:background="#android:color/transparent".
If you do not want padding
set android:padding="0" or set background without padding.
You can also use
android:scaleType="centerCrop"
android:padding="0dp"
android:scaleType="fitXY" tested and works correctly.
Use below code :-
<ImageButton
android:id="#+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:background="#drawable/111" />
You should consider creating a 9-patch image to make the image fit correctly on an ImageButton in case you need to stretch it, but you don't want the image to become distorted.
To have a button with an image that completely covers it, you can use something like this:
<ImageButton android:id="#+id/iconBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:src="#drawable/icon_button"
android:scaleType="fitCenter"
android:background="#00000000"/>
For a full tutorial on creating buttons with custom skins and the use of selectors to manage their states, you can check a tutorial I've written some time ago here (Creating a custom Android button with a resizable skin).
If your image "111" has no padding itself then simply use android:background"#drawable/111"
Just set this two attributes on the ImageButton:
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
With those, you will get the provided src to be stretched, and also if you put some specific width and height on that ImageButton you won't get a rectangle around it.

Fixed-width TextView with stretched compound drawable

I'm trying to achieve the following layout: a fixed width TextView aligned to the left of its parent, with the text inside it aligned to the right side of that TextView (that's why fixed width, can it be done other way?) and the rest of the parent is filled with a drawable (simple line). Like this:
It's a ListView containing 2 types of rows and the layout for the rows with lines is quite trivial - LinearLayout with TextView and ImageView (I can post the exact code later if needed). And I'm getting a warning that it could be replaced with a single TextView with compound drawable.
I'm all for optimization so I really tried to follow that advice. Unfortunately I wasn't able to get the same result - the line is either constrained to TextView's width or text is aligned to the right side of the ListItem, now to fixed position.
Am I missing something?
Edit: Apparently it is not actually possible and since there are some other complications (the drawable is now a level-list drawable, which is not always a line and sometimes it has a non-fixed height that I have to set) I will leave it as it is now - linear layout, containing one TextView and one ImageView.
I don't think that you're missing anything. The TextView compound drawable features are not very customizable and in general are not worth the time you spend trying to get them to look right. Some lint warnings are a little overzealous and premature.
The optimization that the lint refers to is something that is better attributed for a fixed size image. In your case, the line has to stretch the rest of the screen length and as such it is not something that can be done with a textview with compound drawable. This kind of lint warning is more of a suggestion rather than something that MUST be done and is detected by just checking for a linear layout with only a textview and an imageview rather than checking what would need to go in the image view. If you already have it working the way you did it I think you should leave it alone.
Your view create from this -
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/time"
android:layout_width="#dimen/today_time_width"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="right"
android:layout_marginRight="5dp" />
<ImageView
android:layout_gravity="center"
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="#dimen/today_current"
android:src="?attr/item_boundary" />
</LinearLayout>
There is no way to achive this using only standart TextView. If you really want to reduce view count you can create your custom TextView class, set layoutWidth to matchParent and draw line from text end to right border. But it's not worth to be doing. Some extra views won't slow your list.
I am not sure if you will be able to achieve what you really want to , but then you could change the linear layout in the link you posted to something like this:
<RelativeLayout
android:id="#+id/relTrial"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="12:45 AM"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:id="#+id/lnrSep"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_toRightOf="#+id/txtTime"
android:layout_alignParentRight="true"></LinearLayout>
</RelativeLayout>
This way the time text will be right aligned although being at the left side, and the line will also be visible.
Hope that helps.
If I got you right, you want to add bottom border to list view item?
What about to try this:
android:drawableBottom="#drawable/line"

Creating a paperlike folded view in android

This is what i want to make for my textview. I have a 9 patch image . But i have no idea how can i convert this png image into the folded corner background for the textview.Many of you may have faced this problem and came up with a solution. So, please share how did you solved yours.
Heres the solution for you:
<TextView
android:id="#+id/yourTextViewWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_9_patch_image"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:text="Testing News" />
Let me expalain here: Setting the 9 patch image as the background of the textview and putting the padding to the textview will create the above image like background for your textview.

Categories

Resources