My Android EditText class is appearing without "edit box" - android

I am doing a custom editbox class. It starts something like:
public class AmountField extends EditText {
and has overriden the constructor to implement the EditText widget style
/**
* Make a new AmountField Object.
*
* #param context
* the context of the field
* #param attrs
* attributes for the view
*/
public AmountField(Context context, AttributeSet attrs) {
this(context, attrs, R.style.Widget_EditText);
}
However when I implement this on my XML I get an editable TextView instead of the EditText (there is only text but not the surrounding white box) and also when I click on it nothing happens, only text color changes.
Are there any ideas on why this happens? I've done list components the same way and style is not altered.
Thanks in advance

Forget it, i have just realized that the constructor must call super instead of other constructor with more parameters.

Related

Pass external parameter to Custom View

I just get going on drawing, canvas, basic animations but have stumbled upon this annoying issue:
I have a CustomView
public class CustomView extends View{
//
//Edited code ****************************
bool dir; // true -- right-to-left, false -- left-to-right
public void setDirection(bool b)
this.bool = b
// ****************************************
public CustomView(Context context) {
super(context);
...
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs)
...
}
...
//stuff for animation
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
}
...
and inside I have created a little animation, basically something flying from right to left over and over again.
Now, let's say I wanted to have 2 of these views in my layout. But on the second one, stuff should fly from left to right.
Is it possible to somehow pass this "parameter" to the Custom view? or do I really have to create the exact same class and change a plus sign to a minus sign and make it thereby a new class. This would mean animations created by extending view are not tunable at all.
If the latter is the case, then is there a better way to have tunable animations?
Is it possible to somehow pass this "parameter" to the Custom view?
Step #1: Add a field to your custom view to hold your animation
Step #2: Add a setter method to populate the field
Step #3: Call that setter method from something (activity, fragment, etc.) to tell it what animation to use

Android TextView, autoLink="all" showing all numbers as clickable

I have a Scrollview and I have the attribute android:clickable="true" and android:autoLink="all".
I have a string for the ScrollView, and the emails, tel numbers etc, appear and are correctly clickable.
However, The string contains other numbers, such as Years, which also appear clickable and I don't want this; how can I stop this from happening?
Don't use autoLink="all", use the ones you need.
android:autoLink="web|email|phone" will probably cover your use cases.
The clickable="true" on the ScrollView isn't needed for this; rather you should set the autoLink attribute on the TextViews themselves; perhaps extracting a style if you have other common properties.
Add the new Linkify class to your project. From a place that you have access to the TextView (e.g. the Activity):
TextView myTextView = // get a reference to your textview
int mask = Linkify.ALL;
Linkify.addLinks(myTextView, mask);
The addLinks(TextView, int) method is static, so you can use it without creating an instance of Linkify. The return value (boolean) indicates whether something was linkified, but you probably don't need this information, so we don't bother with it.
You'll need to ensure that you don't put the autoLink attribute on the TextViews, otherwise the setText(...) implementations will still linkify years (unless you completely override the setText(...) implementations without calling super.setText(...))
For extra brownie points, you can create a subclass of TextView which will do the linkify for you when you set text on it:
public class AutoLinkifyTextView extends TextView {
public AutoLinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoLinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setText(String text) {
super.setText(text);
parseLinks();
}
#Override
public void setText(int stringRes) {
super.setText(stringRes);
parseLinks();
}
private void parseLinks() {
Linkify.addLinks(this, Linkify.ALL);
}
}
For top marks of course, you'd read the attributes from the attrs and use the correct mask from the XML attributes, but I'd prefer to get rid of that option and do it here.

How do you pass R.Drawable as a parameter in a custom ImageView

I have a super simple class, it extends ImageView. I would like to add (in it's constructor) a parameter for an R.Drawable. ideally, I can then instantiate the custom Char class, and I would have a nice little ImageView with a background. I understand that this is fundamentally possible by just making an imageview and that I'm not customizing anything. This is more for learning though. How to I pass R.Drawable as a parameter? The code below states I can't assign a drawable to an int.
DOUBLE UPDATE:
For a brief moment I saw an answer that said to just use int as a parameter. I thought of this, but that doesn't seem to be graceful at all. I figure Android probably has a more graceful way of approaching this. I tried looking through source doc but got lost in the maze. Apparently that IS the right answer though.
*imports*
public class Char extends ImageView {
Char (Context context, R.drawable drawable){
super(context);
setBackgroundResource(drawable);
}
}
public class Char extends ImageView {
/**
* Constructor for <code>Char</code>
*
* #param context The {#link Context} to use
* #param resId The resource identifier of the {#link Drawable}
*/
public Char(Context context, int resId) {
super(context);
setImageResource(resId);
}
}
To use it call: new Char(your_context, R.drawable.your_drawable))
The answer you saw of passing an int is the correct one. Either that or you can create the drawable, pass it in and use that
public class Char extends ImageView {
Char (Context context, Drawable drawable) {
super(context);
setBackgroundDrawable(drawable);
}
}
R.drawable.xxx is an int, so just pass an int parameter to your function.

Supplying text from fragment to custom TextView

How do I set up a constructor in a custom TextView to be able to pass text from a fragment?
In other words, I'm confused how to send text from my fragment (Fragment1) to the custom view (View1):
public class View1 extends TextView {
//constructors:
public View1(Context context, AttributeSet ats, int ds) {
super(context, ats, ds);
init();
}
public View1(Context context) {
super(context);
init();
}
public View1(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
...
canvas.drawText(myString, margin1, margin2, paint); //myString is from Fragment1
....
}
I asked a similar question here, but didn't really get much help. Example code would go a long way towards clearing up my confusion. Thanks in advance!
You are extending a TextView anyway. As A--C mentioned, you can use getText(), as well as setText() to get and set the text.
In your context, I am not sure if it is a good idea to use TextView to implement your custom view/widget. View might be a better starting point, as TextView carries all kind of stuff around for formatting, icon/drawable display, click/button logic etc.
You need to define the standard constructors if you want to be able to have the system instantiate/inflate your components from an XML layout. Then you can use standard getters/setters for your data, same way as all other controls do it.
If you instantiate your widget/view yourself (in your code), you are free to define whatever constructors you want to (I believe).

android custom imageview with textview to the right

I am currently writing a custom ImageView. what im actually turning this imageview into is a multiple state button.
In my case, the button has three states, answered with yes, answered with no and unanswered.
I strictly don't want to use a radiogroup/button.
I have the basic setup for the imageview. It has onclick functionality to properly change the image depending on the state. Which for now is just a checkbox with a cross, an empty checkbox and a checkbox with a tick.
What I want is, when the state changes(the image is clicked) i want text, which should Always be around, and never ontop of the imageview. To change its value aswell. I want to be able to call a method similar to customCompInstance.getText(); customCompInstance.getText();
What im thinking is, the custom imageview class should have a TextView member, but I have NO clue whatsoever how to place it on the left, horizontally aligned to the imageview.
It is NOT an option to just use two different elements in xml.
Is adding a textview and placing it to the right of the imageview the right solution, if not, what do you suggest, if so, please give me some tips on how to actually achieve this.
Simplified version of my class:
public class AnswerImageView extends ImageView {
private AnswerState mSelectionState;
/**
* #param context
*/
public AnswerImageView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
setImageResource(R.drawable.answer_none);
setTag(R.drawable.answer_none);
mSelectionState = AnswerState.ANSWER_NONE;
}
/**
* #param context
* #param attrs
*/
public AnswerImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
setImageResource(R.drawable.answer_none);
setTag(R.drawable.answer_none);
mSelectionState = AnswerState.ANSWER_NONE;
}
/**
* #param context
* #param attrs
* #param defStyle
*/
public AnswerImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
setImageResource(R.drawable.answer_none);
setTag(R.drawable.answer_none);
mSelectionState = AnswerState.ANSWER_NONE;
}
public AnswerState getSelectionState() {
return mSelectionState;
}
public void setSelectionState(AnswerState selectionState) {
this.mSelectionState = selectionState;
}
}
I have a method that reacts to onclicks, that all works fine. im purely asking about adding a textview in this component. Essentially combinding two components into one.
You should extend from TextView instead of ImageView and have a look at methods TextView.setCompoundDrawableXXX. Here is the link for the doc.

Categories

Resources