android-custom-view
Android onClick method doesn’t work on a custom view
I just had the same Problem – I created a custom view and when I registered a new Listener for it in the activity by calling v.setOnClickListener(new OnClickListener() {…}); the listener just did not get called. In my custom view I also overwrote the public boolean onTouchEvent(MotionEvent event) {…} method. The problem was that I … Read more
How can a custom view get access to its activity? [duplicate]
The getContext() method in the View class returns the context that was passed on its constructor. Usually that’s the Activity you want (Activity extends Context). So this probably works for you: Java: ((Activity)getContext()).someMethod(…); Kotlin: (context as? Activity)?.someMethod(…)
Using an array reference as an XML attribute for custom android view
Just going to piggyback off your question here, since your post shows up first if you google something like “array reference XML attribute custom view”, so someone might find this helpful. If you want your custom view to reference an array of strings, you can use Android’s existing android:entries XML attribute, instead of creating a … Read more
Dynamic height viewpager
Made a few tweaks in your code and it is working fine now. 1] onMeasure function wasn’t proper. Use below logic @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mCurrentView == null) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } int height = 0; mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = mCurrentView.getMeasuredHeight(); if (h > height) height = … Read more
How can I get the canvas size of a custom view outside of the onDraw method?
For drawing purposes, you should not really use the dimensions of the Canvas object. Just use the dimensions provided to you in the onSizeChanged method. You can either store the dimensions for use in the onDraw method or resize/draw to a backing bitmap that you can draw with later. Update: Quickly whipped up some code, … Read more
Error inflating custom button class. NoSuchMethodException
This constructor is missing: public TitledValueButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } Add this constructor to your custom button class.
Android: Start the circular progress bar from top (270°)
Try specifying rotation degrees to your progress items. <?xml version=”1.0″ encoding=”UTF-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android” > <item android:id=”@android:id/progress”> <rotate android:fromDegrees=”270″ android:toDegrees=”270″ android:pivotX=”50%” android:pivotY=”50%” > <shape android:innerRadiusRatio=”2.5″ android:shape=”ring” android:thicknessRatio=”25.0″ > <gradient android:centerColor=”@color/gray” android:endColor=”@color/gray” android:startColor=”@color/gray” android:type=”sweep” /> </shape> </rotate> </item> <item android:id=”@android:id/secondaryProgress”> <rotate android:fromDegrees=”270″ android:toDegrees=”270″ android:pivotX=”50%” android:pivotY=”50%” > <shape android:innerRadiusRatio=”2.5″ android:shape=”ring” android:thicknessRatio=”25.0″ > <gradient android:centerColor=”@color/green” android:endColor=”@color/green” android:startColor=”@color/green” android:type=”sweep” /> … Read more