I am able to add one drawableRight in EditText in android easly and on click event working perfectly in case of one drawableRight. But I need two drawableRight in EditText.
So, How can I add two drawableRight in EditText? and I also need to perform click event on both drawableRight separately.For example I want to add yellow star in EditText like as given below image and on click on rightmost image I want to open contact book of phone and on click of yellow star I want to call user's favourite numbers list.
So How can I do this? Any Idea?
There is no native support for this, so you got two options here:
Easy solution: Create a Linear layout with tow image views on the right side, those will be your drawables.
The hard way: Extend a Drawable class and implement your own onDraw method where you will draw the two drawables. Than use that one for your text view.
You can't. The TextView can contain only one drawable on either of its sides. The only options you have are:
Create custom View.
Take some of the ViewGroup descendants (RelativeLayout/FrameLayout/etc) and put the TextView along with the two ImageViews in it.
Just place two Drawables into EditText using RelativeLayout.
To set inner padding, place an invisible drawableRight into EditText:
/res/values/dimens.xml
<resources>
<dimen name="iconSize">32dp</dimen>
</resources>
/res/layout/my_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textAutoComplete"/>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="#dimen/iconSize"
android:layout_height="#dimen/iconSize"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_1"/>
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="#dimen/iconSize"
android:layout_height="#dimen/iconSize"
android:layout_toLeftOf="#+id/imageButton1"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_2"/>
</RelativeLayout>
In your Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
EditText editText = (EditText) findViewById(R.id.editText);
int iconSize = (int) getResources().getDimension(R.dimen.iconSize)
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_1);
drawable.setBounds(0, 0, iconSize * 2, 0); // that is the trick!
editText.setCompoundDrawables(null, null, drawable, null);
}
Related
I need to set an icon (search icon) to a EditText's RightView, I've used the following xml:
android:drawableRight="#drawable/search_icon"
The problem is that the RightView doesn't fit inside the EditText, instead the EditText's height changes based on the icon height.. Is it possible to do the opposite, to edit the RightView's height based on the EditText's height?
XML of the EditText:
<EditText
android:id="#+id/regSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:ems="10"
android:hint="Search Test"
android:inputType="phone"
android:padding="5dip"
android:textColor="#color/lowBlack"
android:textSize="16sp"
android:drawableRight="#drawable/search_icon" />
Instead of using drawableRight, you can tweak your layout a little bit and have an imageview alongside the edittext, and then you can play with the height and width of the two views. e.g. this can be achieved using a RelativeLayout with the edittext and imageview within:
<RelativeLayout>
<EditText android:id="my_edittext"/>
<ImageView android:toRightOf="#+id/my_edittext"
android:layout_alignTop="#+id/my_edittext"
android:layout_alignBottom="#+id/my_edittext"/>
<RelativeLayout/>
The alignTop and alignBottom attribute will take care of the height issue you are facing. And toRightOf will put the imageview to the right of the edittext in your layout.
But if you want the imgeview to be over the edittext, you can achieve that too using this layout - just remove the "toRightOf" attribute and add alignParentRight="true" to the ImageView.
NOTE: I've not written the complete xml code here. Let me know if you need any more help with this.
Finally after searching for a while I've came up with the following solution that doesn't need to create any other "hackish" element:
regSearch.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
Drawable img = Registrazione.this.getResources().getDrawable(
R.drawable.search_icon);
img.setBounds(0, 0, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3, regSearch.getMeasuredHeight()-regSearch.getMeasuredHeight()/3); //you can edit this in order to resize the drawable
regSearch.setCompoundDrawables(null, null, img, null);
regSearch.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
This way you can set and edit the dimension of whatever drawable you want, hope it can help someone else because I got stuck on this problem for a while :P
I want to make a button, where left right top and bottom paddings will be same.
But I get something like this:
Here is a layout:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/people_number_label"
android:text="#string/number_of_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/people_number"
android:layout_toRightOf="#+id/people_number_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
So the way a Button works is its a text view with a background drawable. That drawable is the one you're seeing. The text will be centered within that drawable, as you see here. If you want the text to appear in line with the prompt, a Button isn't going to work for you. You're better off just making it a TextView and setting an xml background drawable with a rounded rect so it looks like a button, but it actually isn't.
Another thing you can try to do it align_baseline the two view. That should align the text, but may push the prompt down the screen
I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"
I have a textview, created inside a shape. I want to write text above this text view not inside it. !
I have added image for it. The outer rectangle displayed is a dialog box and inner rectangle is textview. : http://i.stack.imgur.com/tLUgM.png
My xml looks like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_gravity="top"
android:background="#drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
Now I want to write text above this inner rectangle and inside outer rectangle which is a dilog box.
You can't do that. You will have to add other TextView dynamically if you don't want to add it in the xml file.
And then set the text to newly added TextView. But the TextView must be there to hold text
use two textview one for shape and other one for text.
I'm using actionbarsherlock.
In order to get action modes on bottom(action modes on bottom) I use
android:uiOptions="splitActionBarWhenNarrow"
But I need to add a button to main action bar, so I use custom view.
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_view, null);
getSupportActionBar().setCustomView(customNav, new
ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
Here is custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_day"
android:onClick="onMoreClick"/>
It doesn't metter if I use ImageButton or ImageView with the same properties, so I'll talk about ImageButton.
If I use the ImageButton, I get what I want except of that background is gray.
If I add a line to previous custom_view.xml, in order to get transperent background(or use ImageView instead)
android:background="#00000000"
The alpha in icon will be "cropped" and size of button will change.
The question is. How to get button with such sizes as on first image(with default gray background) but with transperent background?
One of the ways to solve a problem is to make a color of the button's background the same as a color of actionbar and it will look like transperent, but I don't think it is a good solution.
You can use image with transparent background. It can be done in photoshop.
Have you considered using an ImageView instead of an ImageButton? Just set your required image in the imageview and use an onclicklistener to detect clicks like you would do for an ImageButton.
Additionally, you can also just specify the width of your ImageButton and set the padding as required (this is if you want to use android:background="#00000000").
i think that you can add to the button
android:background="yourimagethere"; or maybe android:background=NULL
and it will solve the problem