Change EditText setError drawable gravity - android

For EditText that going be filled with RTL text, is there a way to change the gravity of the error drawable (and popup of course) ?
here is an example of regular error drawable
so since the text entered is RTL i would like the pop up to show up at the LEFT side of the EditText
i tried to apply custom drawable , but Drawable doesnt seem to have any setGravity method.
thanks in advance.

It's not possible by normal means, however you can extend the Android TextView class and change the layout that Android uses to inflate the popup. I haven't tried this myself but it could work.
I did a quick look in the source of TextView and found this line
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null);
Which references to this layout.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/popup_inline_error"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
/>
Adding gravity here could do the trick. A similar approach for the drawable could apply.

It's over a year, but if it helps anyone else, you can use:
myEdit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_error, 0, 0, 0);
but it will stay until you remove it
myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

Related

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

How to make TextView's background to be transparent?

There is a tablelayout having four tablerows, the first tablerow contains a TextView :
As you can see the background of the TextView having the text "Vidy" is seen ! So how to make it transparent ? I tried to add android:alpha="0" to the attribute of the TextView but at runtime the text is not seen !
You can try:
android:background="#null"
Or:
android:background="#android:color/transparent"
Check also: Android Transparent TextView?
If you really need transparency, #Nermeen's answer is great, you can also get it done programmatically like:
myTextView.setBackgroundColor(Color.TRANSPARENT);
but as long as you have solid background color below, it's better due to performance to make your TextView's background color the same as the layout below (with alpha, app needs to redraw more regions). It won't really matter for such a simple layout, but it's worth remembering when you have many of them using transparency.

android :Customizing Button with dynamic values

I want to customize Buttons for my application. The application has a color picker where the user will select color and I have to set that particular start/end color to the buttons. These colro values will be stored in an object "Utility".
Basically from start only, I want to use "Utility" object to set colors for background, text color, font, etc. And again when the color is changed by the user I got to change it to the buttons and refresh them. And also to save colors in a file, so next time user starts app, it comes up with the last color selected.
I couldn't find <selector> to be the best option, as I wont be able to change the color in xml. What can be the best option for such requirement ?
UPDATIONS :
#jitendra, from your answer I got somethign helpful. I use GradientDrawable to set colors of my buttons. In my onCreate() of the Activity, I call a method RefreshComponents() that sets the background of root, text color/size of buttons and gradient colors of the buttons. It works properly, but the only problem I see is the on applying GradientDrawable to the button the gap between 2 buttons is lost.
This is the image WITHOUT applying GradientDrawable :
On applying GradientDrawable the output is :
You see the size of button is increased a bit from all the sides. If I apply to next button also, they both touch eachother. My xml for the above is :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainroot" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:paddingTop="35dip" android:paddingBottom="35dip"
android:paddingLeft="35dip" android:paddingRight="35dip"android:gravity="center" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainrow1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_marginBottom="15dip" android:gravity="center_horizontal" >
<Button android:text="Accounting" android:id="#+id/accBtn" android:layout_width="80dip" style="#style/TileButtonStyle" />
<Button android:text="Data" android:id="#+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"></Button>
<Button android:text="Information" android:id="#+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"></Button>
</LinearLayout>
..... Other lineasr layout with same parameters as above child
And the GradientDrawable that I create is :
public static GradientDrawable getButtonDrawable(Button btn) {
int colors[] = {getStartColor(), getEndColor()};
GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
grad.setCornerRadius(5f);
return grad;
}
And finally in my onCreate(), I add :
GradientDrawable btnGradient = Utility.getButtonDrawable(btn1);
btn1.setBackgroundDrawable(btnGradient);
What is going wrong here ? Is the margin around the button becoming 0 ? Do I have to set bounds for the grad, or again set LayoutParams for the button ?
Any help is appreciative to help me achieve my goal.
Thanks
You can Create StateListDrawable Object dynamically in java file and set as background and sources of applcation components.
Android has Themes and Styles, but they are a development-time feature and can't be manipulated at runtime. However, different predefined themes can be applied at runtime.
So you can have a set of predefined themes, with fixed view properties (colors, fonts, etc..) and give user an option to choose a theme at runtime.
But, if you need to change every particular view property, then you will need to roll your own "theme" system. Which means you will need to have properties stored somewhere and applied each time a view is built.
Tvd! I incline to agree with Peter Knego and Jitender Sharma. Furthermore, I think/believe you can setOnClickListener on those buttons of yours and perform your color changing stuff with the code that rests inside the setOnClickListenermethod assigned to every button. In addition to this, you'll have to configure your color.xml file and custom themes. There's a lot of work to be done. I'm not really sure which is the best way though. I strongly suggest you to go through the android learning stuff I'd provided to you in my previous answer. Only they can give you a detailed insight and a solid idea to go ahead. All the best!
Oh I got the solution :
I added layout_marginRight attribute to buttons and that did the work.
Though I am still concerned, without GradientDrawable the buttons had margin betweenthem then after applying GradientDrawable why is the default margin lost ? Why is the need of additional layout_marginRight to be added ?
If anyone yet has answer for this, please let me know.
Thanks

How to allow the user to select a text range in a TextView (Similar to EditText)

I've got a TextView that I would like to allow the user to select a range of text from within it. The TextView takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:
eic = new InputConnection( bookTextView );
eic.beginBatchEdit();
But it doesn't do anything noticable. Does anyone know how to use InputConnection correctly? Thanks.
Edit: I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView or an EditText which looks and feels like a TextView and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).
Here is an idea.. Add an EditText with a TextView background, Here is an example
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
add this to your xml in the place of TextView
You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.
See also:
http://developer.android.com/reference/android/text/Spanned.html
You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.
Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My idea is the same as sandy's.
My code is here, hope it may help you:
https://stackoverflow.com/a/11026292/966405
After long internet surfing to find a solution, i prefered create my own class
https://github.com/orionsource/SelectableTextViewer
Goal features:
Easy to use - only one class
Support for text and Html.fromHtml
Can be in ScrollView with correct touches
Cursors can be redefined
Color of selection can be redefined
All the above solutions either too long or not working for me.
What you need is to add just textView.setTextIsSelectable(true)
in your activity or fragment or adapter.

TwoLineListItem - Custom attributes

How can I override the XML attributes of TwoLineListItem. I don't want to do it Java. I'm trying to make the backgrounds of the view and the textViews transparent, but it isn't working when I do it in Java.
setBackGroundDrawable
setBackGroundColor
setBackGroundResource
These don't seem to do what I need them to do. I've tried making no background. I've tried setting an alpha value of 0 to the color, but I still get the same old white list boxes. I have a drawable background that is behind the TwoLineListItem view, and thats all I want to show for the background, not the Item background.
According to my understanding of your question you are trying to make your TextView's background to trasparent. this is what i use to do this:
TextView tv = (TextView)findViewById(R.id.textviewID);
tv.setBackgroundColor(Color.TRANSPARENT);
And I didn't get your point about TwoLineListItem. But if you want it to become single line, then write in xml properties of your TextView:
android:singleLine="true"
If this was not the question then please elaborate a bit.

Categories

Resources