i have the following code, and the problem, that my function rlposition() isn't available from outside the class.
public class RLbadge extends TextView {
public RLbadge(Context context) {
super(context);
this.setTypeface(null, Typeface.BOLD);
this.setTextColor(Color.WHITE);
this.setBackgroundResource(R.drawable.badge);
this.setTextSize(18);
}
public void rlposition(Button pButton) {
// THIS FUNCTION ISNT SEEN FROM OUTSIDE WHY?
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
Why is the function rlposition not visible from outside of the class?
Isn't it possible to add functionality to an extended TextView?
<YOURPACKAGENAME.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="TEASTING" />
Here is Class
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
// Do your staff
}
}
}
i now answered the question for myself and put it here maybe somebody needs the answer in the future.
The problem was this line of calling:
TextView badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // here the error comes
changed to
RLbadge badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // the function is visible
Related
I am trying to subclass TextInputLayout, in a CustomTextInputLayout. This is the code:
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextInputLayout(Context context) {
super(context);
}
public View getComponent(){
return this.getChildAt(0);
}
public void setHint(String hint){
super.setHint(hint);
}
}
I am using it to wrap another custom component, with a button and an EditText:
public class ComponentesSeleccion extends RelativeLayout {
private EditText editText;
private Button button;
public ComponentesSeleccion(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ComponentesSeleccion(Context context) {
super(context);
inicializar();
}
public ComponentesSeleccion(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.seleccionlayout, this, true);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button2);
}
public Button getButton() {
return button;
}
public EditText getEditText() {
return editText;
}
public void setInputType(int type){
editText.setInputType(type);
}
public void setImagen(Drawable drawable){
button.setBackgroundDrawable(drawable);
}
}
With this, I am not able to do in my code:
CustomTextInputLayout ctil=(CustomTextInputLayout)view.findViewById(R.id.ctil);
ctil.setHint(“a hint");
No exception, no errors, but the hint is not drawn. i can set the normal hint on the EditText, but I want the animated hint of the TextInputLayout.
How could I get it working?
Thank you.
I have a class which extends LinearLayout. This class has a method called addSubview and the warning message "Method 'actionXYZ' is never used" shows, but the method have public modifier!
Furthermore, this method does not appear when this class is instantiated in a activity, for example.
Why?
public class dummyLayout extends RelativeLayout {
LayoutInflater mInflater;
RelativeLayout rootItem;
public dummyLayout(Context context) {
super(context);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context ctx) {
mInflater = LayoutInflater.from(ctx);
rootItem = (RelativeLayout) mInflater.inflate(R.layout.dummyLayout, this, true);
}
public void addSubView(View v) {
rootItem.addView(v);
}
}
This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Custom Fonts and Custom Textview on Android
(3 answers)
Closed 8 years ago.
I want to use custom font in my application. Which is the best way to give font to entire application. I know how to assign custom font to a single TextView or Button.
Is it possible to mention the custom font in one place, for eg. in styles.xml, and the font will be applied to whole application (every TextView, Button, EditText and soon).
use this type of textview in your app
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
also edit text
public class CEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CEditText(Context context) {
super(context);
this.context=context;
init();
}
public CEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
this.attrs=attrs;
this.defStyle=defStyle;
init();
}
private void init() {
Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf);
}
Unfortunatly, android is not providing any method to apply custom font for the complete app in one place.
However, you can create your CustomTextView extends TextView and CustomButton extends Button. In that you can set the font by creating a FontInstance.
I'm trying something like this
public class CustomViewSubclass extends HorizontalScrollView{
private LinearLayout layout;
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
// This is called from the `Activity`
public void startAsyncTask() { // code }
// This method is called in the `onPostExecute()` of an `AsyncTask` subclass
public void doSomething(Context context) {
ImageView image = ImageView(context);
layout.addView(image); // NullPointerException here, layout seems to be null
}
but it seems that layout on doSomething() is null. How is that even possible? I'm initializing it on the constructor... and I never re-initialize it again;
I'm adding my custom view via XML
<com.mypackage.CustomViewSubclass
android:layout_width="wrap_content"
android:layout_width="match_parent" />
Ok I fixed it, it was an stupid mistake made by me:
I used super() on the 3 methods, instead of using this().
public CustomViewSubclass(Context context) {
super(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
super(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
Solution:
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
how can I find out when onDraw of a View has finished?
Thanks!
I don't know what you're ultimately trying to achieve, but if you need to run some logic once drawing has been completed in an Activity, then you can call View.post(Runnable) and put your logic inside there. onDraw would have likely taken place once the code in your Runnable has been reached since it put that Runnable on the message queue.
Place a boolean member in your View class and set it to true when onDraw is called (If you simply want to be able to test that a View has drawn).
Or if you want some sort of post-draw "event" call to execute a new thread- just put it at the end of onDraw.
Another suggestion how it could be done:
public class FVRTraceAbleListView extends ListView {
ListViewListener listener;
public interface ListViewListener {
void onPostDraw();
}
public FVRTraceAbleListView(Context context) {
super(context);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setListener(ListViewListener listener) {
this.listener = listener;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (listener != null) {
listener.onPostDraw();
}
}
}