I want the text inside my TextView to fit the screen. I need to implement something like this:
The letter 'A' needs to be at the center of the screen with height equal to the height of the device screen. I have created a Custom TextView for this as follows but that doesn't seem to work. What I mean by this is that my text (letter A) isn't fitting the height of the screen. I tried manually adjusting the text font size but that isn't the right way I guess. Can someone point out a better solution for this?
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
public class FontFitTextView extends TextView
{
private Paint mTestPaint;
private float maxFontSize;
private static final float MAX_FONT_SIZE_DEFAULT_VALUE = 20f;
public FontFitTextView(Context context)
{
super(context);
initialise(context, null);
}
public FontFitTextView(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
initialise(context, attributeSet);
}
public FontFitTextView(Context context, AttributeSet attributeSet, int defStyle)
{
super(context, attributeSet, defStyle);
initialise(context, attributeSet);
}
private void initialise(Context context, AttributeSet attributeSet)
{
if(attributeSet!=null)
{
TypedArray styledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.FontFitTextView);
maxFontSize = styledAttributes.getDimension(R.styleable.FontFitTextView_maxFontSize, MAX_FONT_SIZE_DEFAULT_VALUE);
styledAttributes.recycle();
}
else
{
maxFontSize = MAX_FONT_SIZE_DEFAULT_VALUE;
}
mTestPaint = new Paint();
mTestPaint.set(this.getPaint());
//max size defaults to the initially specified text size unless it is too small
}
private void refitText(String text, int textWidth, int textHeight)
{
if (textWidth <= 0)
return;
int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
int targetHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom();
float hi = maxFontSize;
float lo = 2;
final float threshold = 1f; // How close we have to be
mTestPaint.set(this.getPaint());
Rect bounds = new Rect();
while ((hi - lo) > threshold)
{
float size = (hi + lo) / 2;
mTestPaint.setTextSize(size);
mTestPaint.getTextBounds(text, 0, text.length(), bounds);
if (bounds.width() >= targetWidth || bounds.height() >= targetHeight)
hi = size; // too big
else
lo = size; // too small
}
// Use lo so that we undershoot rather than overshoot
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int height = getMeasuredHeight();
refitText(this.getText().toString(), parentWidth, height);
this.setMeasuredDimension(parentWidth, height);
}
#Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after)
{
refitText(text.toString(), this.getWidth(), this.getHeight());
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
if (w != oldw)
{
refitText(this.getText().toString(), w, h);
}
}
}
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res-auto="http://schemas.android.com/apk/res-auto"
android:id="#+id/home_Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical">
<com.example.FontFitTextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"
android:textSize="80sp"
res-auto:maxFontSize="55sp" />
</LinearLayout>
</RelativeLayout>
Try calling super.onMeasure() method at the end of the onMeasure() method with the updated width and height (parentWidth, height).
Related
I'm using this class to create custom edit text but I can't change the direction of input text
Do you have any idea how can I change it?
I already tried textDirection and LayoutDirection but doesn't work
thanks in advance
(Building a PinEntryEditText in Android)
this is the class
package com.example.jorvajoor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.View;
import androidx.appcompat.widget.AppCompatEditText;
public class OTPeditText extends AppCompatEditText {
private float mSpace = 24; //24 dp by default, space between the lines
private float mNumChars = 4;
private float mLineSpacing = 8; //8dp by default, height of the text from our lines
private int mMaxLength = 5;
private float mLineStroke = 2;
private Paint mLinesPaint;
private OnClickListener mClickListener;
public OTPeditText(Context context) {
super(context);
}
public OTPeditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public OTPeditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
float multi = context.getResources().getDisplayMetrics().density;
mLineStroke = multi * mLineStroke;
mLinesPaint = new Paint(getPaint());
mLinesPaint.setStrokeWidth(mLineStroke);
mLinesPaint.setColor(getResources().getColor(R.color.black));
setBackgroundResource(0);
mSpace = multi * mSpace; //convert to pixels for our density
mLineSpacing = multi * mLineSpacing; //convert to pixels for our density
mNumChars = mMaxLength;
super.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// When tapped, move cursor to end of text.
setSelection(getText().length());
if (mClickListener != null) {
mClickListener.onClick(v);
}
}
});
}
#Override
public void setOnClickListener(OnClickListener l) {
mClickListener = l;
}
#Override
public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {
throw new RuntimeException("setCustomSelectionActionModeCallback() not supported.");
}
#Override
protected void onDraw(Canvas canvas) {
int availableWidth = getWidth() - getPaddingRight() - getPaddingLeft();
float mCharSize;
if (mSpace < 0) {
mCharSize = (availableWidth / (mNumChars * 2 - 1));
} else {
mCharSize = (availableWidth - (mSpace * (mNumChars - 1))) / mNumChars;
}
int startX = getPaddingLeft();
int bottom = getHeight() - getPaddingBottom();
//Text Width
Editable text = getText();
int textLength = text.length();
float[] textWidths = new float[textLength];
getPaint().getTextWidths(getText(), 0, textLength, textWidths);
for (int i = 0; i < mNumChars; i++) {
canvas.drawLine(startX, bottom, startX + mCharSize, bottom, mLinesPaint);
if (getText().length() > i) {
float middle = startX + mCharSize / 2;
canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, bottom - mLineSpacing, getPaint());
}
if (mSpace < 0) {
startX += mCharSize * 2;
} else {
startX += mCharSize + mSpace;
}
}
}
}
and this is how I use it in XML file
<com.example.jorvajoor.OTPeditText
android:id="#+id/word1"
android:layout_width="#dimen/_200sdp"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:inputType="text"
android:gravity="center"
android:letterSpacing="#integer/material_motion_duration_short_1"
android:textIsSelectable="true"
android:imeOptions="actionDone"
android:textSize="20sp"/>
Add Manifest File.
<manifest ... >
...
<application ...
android:supportsRtl="true">
</application>
</manifest>
I couldn't figure it out
but I use PinEntryEditText instead and solve my problem easily
just need to add text direction
I created a custom view to draw some shapes on it but my problem is that nothing is showed on the view. I get no error messages or crash. My custom class looks the like:
public class DrawableView extends View {
private Paint mPaint;
private MyDrawable mDrawableObj; // object to draw
private RectF mRectBounds;
public DrawableView(Context context) {
super(context);
init();
}
public DrawableView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawableView(Context context, AttributeSet attrs, int defStyleAttr){
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(w, h);
}
public void setDrawableObj(MyDrawable obj, boolean forcePrepare) {
final boolean needPrepare = forcePrepare || (mDrawableObj != null) ;
mDrawableObj = obj;
mDrawableObj.setView(this);
if (needPrepare) {
prepareObject();
invalidate();
}
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
final int padLeft = getPaddingLeft();
final int padTop = getPaddingTop();
final float xpad = (float) (padLeft + getPaddingRight());
final float ypad = (float) (padTop + getPaddingBottom());
final float ww = (float) w - xpad - 1;
final float hh = (float) h - ypad - 1;
mRectBounds = new RectF(0, 0, ww, hh);
mRectBounds.offsetTo(padLeft, padTop);
prepareObject();
}
public void prepareObject() {
if (mDrawableObj != null)
mDrawableObj.prepare(mRectBounds);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawableObj != null)
mDrawableObj.draw(canvas, mRectBounds);
else {
canvas.drawRect(mRectBounds, mPaint);
}
}
My layout file is the following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hello.myApp.Draw">
<DrawableView.DrawableView
android:id="#+id/drawtest_drawableView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintLeft_toRightOf="#+id/leftMenu"
android:padding="4dp" />
</android.support.constraint.ConstraintLayout>
Do you have any idea why I have just a white screen?
use this
public void init() {
if (isInEditMode())
return;
//code
//...
}
I copy/pasted your code into my IDE and ran it (though I had to create a fake MyDrawable interface to satisfy the compiler). I'm just putting an instance of DrawableView into a LinearLayout and I see it drawing just fine.
My layout xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.stackoverflow.DrawableView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="12dp"/>
</LinearLayout>
What I see:
It makes sense that I see only the blue rectangle, because this is your onDraw() implementation:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawableObj != null)
mDrawableObj.draw(canvas, mRectBounds);
else {
canvas.drawRect(mRectBounds, mPaint);
}
}
Given that setDrawableObj(MyDrawable obj, boolean forcePrepare) is never called in the code you posted, the else is always what's being executed/drawn.
I have a number of buttons in a LinearLayout.
The buttons have 1 weight, so they are placed evenly into the layout.
I want to do something like this
for (int i = 0; i < texts.size(); ++i) {
Button button = new Button(/*context*/);
button.setLayoutParams(/*WRAP_CONTENT, MATCH_PARENT, 1.0f*/)
button.setText(texts[i]);
float fontSize = 20.0f;
button.setTextSize();
while (textDoesNotFitIntoButton(button)) {
fontSize -= 2.0f;
button.setTextSize(fontSize);
}
linearLayout.addView(button);
}
How do I check if text does not fit into the button?
The point is to fill all the button with text in such a way, that text occupies all available space and gets neither truncated nor split into lines.
E.g.
String[] texts = new String[] { "0", "sin^-1" };
If I choose a single text size for "0" to occupy the whole space then I either get "sin" split into two lines "sin^" and "-1" or "0" appears smaller than it should.
I have edited the answer given by #Jaswanth Manigundan to make it extend from android.support.v7.widget.AppCompatButton. Have a look at the below scripts:
FontFitTextView.java
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
public class FontFitTextView extends android.support.v7.widget.AppCompatButton
{
private static final float THRESHOLD = 0.5f;
private enum Mode { Width, Height, Both, None }
private int minTextSize = 1;
private int maxTextSize = 50;
private Mode mode = Mode.None;
private boolean inComputation;
private int widthMeasureSpec;
private int heightMeasureSpec;
public FontFitTextView(Context context) {
super(context);
}
public FontFitTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FontFitTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray tAttrs = context.obtainStyledAttributes(attrs, R.styleable.FontFitTextView, defStyle, 0);
maxTextSize = tAttrs.getDimensionPixelSize(R.styleable.FontFitTextView_maxTextSize, maxTextSize);
minTextSize = tAttrs.getDimensionPixelSize(R.styleable.FontFitTextView_minTextSize, minTextSize);
tAttrs.recycle();
}
private void resizeText() {
if (getWidth() <= 0 || getHeight() <= 0)
return;
if(mode == Mode.None)
return;
final int targetWidth = getWidth();
final int targetHeight = getHeight();
inComputation = true;
float higherSize = maxTextSize;
float lowerSize = minTextSize;
float textSize = getTextSize();
while(higherSize - lowerSize > THRESHOLD) {
textSize = (higherSize + lowerSize) / 2;
if (isTooBig(textSize, targetWidth, targetHeight)) {
higherSize = textSize;
} else {
lowerSize = textSize;
}
}
setTextSize(TypedValue.COMPLEX_UNIT_PX, lowerSize);
measure(widthMeasureSpec, heightMeasureSpec);
inComputation = false;
}
private boolean isTooBig(float textSize, int targetWidth, int targetHeight) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
measure(0, 0);
if(mode == Mode.Both)
return getMeasuredWidth() >= targetWidth || getMeasuredHeight() >= targetHeight;
if(mode == Mode.Width)
return getMeasuredWidth() >= targetWidth;
else
return getMeasuredHeight() >= targetHeight;
}
private Mode getMode(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if(widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY)
return Mode.Both;
if(widthMode == MeasureSpec.EXACTLY)
return Mode.Width;
if(heightMode == MeasureSpec.EXACTLY)
return Mode.Height;
return Mode.None;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(!inComputation) {
this.widthMeasureSpec = widthMeasureSpec;
this.heightMeasureSpec = heightMeasureSpec;
mode = getMode(widthMeasureSpec, heightMeasureSpec);
resizeText();
}
}
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
resizeText();
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw || h != oldh)
resizeText();
}
public int getMinTextSize() {
return minTextSize;
}
public void setMinTextSize(int minTextSize) {
this.minTextSize = minTextSize;
resizeText();
}
public int getMaxTextSize() {
return maxTextSize;
}
public void setMaxTextSize(int maxTextSize) {
this.maxTextSize = maxTextSize;
resizeText();
}
}
style.xml:
<resources>
<declare-styleable name="FontFitTextView">
<attr name="minTextSize" format="dimension" />
<attr name="maxTextSize" format="dimension" />
</declare-styleable>
</resources>
And in my activity_main.xml, I used the FontFitTextView tag instead of Button:
<com.example.myname.testsidebar.FontFitTextView
android:id="#+id/userDetailsBtn"
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableTop="#drawable/user_details"
android:gravity="center"
android:background="#00FFFFFF"
android:paddingTop="10sp"
android:textSize="15sp"
android:text="Your Details"
android:drawableTint="#ED1B2F"
android:textColor="#797369"
android:layout_weight="1"
android:layout_gravity="center"/>
Hope it helps.
I searched the topic for several days, but finally confused. The idea is to create own alarm app with some special tricks. Firstly, I need clock hands. For creating custom clock, I used own class, which extends View. Hour and minute hands are PNG images.
They should be located in the center of the screen, but they dont. Actually, I can't even see them. And that is the question.
Here is the Clock class
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
//import android.os.Handler;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
#SuppressLint("NewApi")
public class Clock extends View {
public Clock(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
private Drawable mHourHand;
private Drawable mMinuteHand;
private boolean mAttached;
static private float mMinutes;
static private float mHour;
private boolean mChanged;
Context mContext;
private boolean mSeconds;
// Gettes & setters. These clock must present alarm time which user sets in the next view
protected float getmMinutes() {
return mMinutes;
}
protected static void setmMinutes(float mMinutes) {
Clock.mMinutes = mMinutes;
}
protected float getmHour() {
return mHour;
}
protected static void setmHour(float mHour) {
Clock.mHour = mHour;
}
private Point size;
// ctors
public Clock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Clock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = context.getResources();
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.AnalogClock, defStyle, 0);
mContext=context;
mHourHand = r.getDrawable(R.drawable.hours);
mMinuteHand = r.getDrawable(R.drawable.minuts);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
// getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 150; // and yes, 150 what? px, inches, dpi-s? I draw it just randomly
int desiredHeight = 150;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//Measure Width
if (widthMode == MeasureSpec.EXACTLY) {
//Must be this size
width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
width = Math.min(desiredWidth, widthSize);
} else {
//Be whatever you want
width = desiredWidth;
}
//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
//Must be this size
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
height = Math.min(desiredHeight, heightSize);
} else {
//Be whatever you want
height = desiredHeight;
}
setMeasuredDimension(width, height);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}
#SuppressWarnings({ "deprecation" })
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
boolean changed = mChanged;
if (changed) {
mChanged = false;
}
boolean seconds = mSeconds;
if (seconds ) {
mSeconds = false;
}
int w = 100; //These are too made randomly
int h = 100;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// if (android.os.Build.VERSION.SDK_INT <= 13)
// {
w = display.getWidth(); // deprecated
h = display.getHeight(); // deprecated
//}
// else if (android.os.Build.VERSION.SDK_INT > 13)
//{
//size = null;
//display.getSize(size);
//w = size.x;
//h = size.y;
//} ... I cant figure out, why, but size returns null. So I'll use deprecated ones just for
now.
// **Here are my measures. I suggest that if height of an hour hand should be about 1/4 of
screen width, then following the proportion - width of that hand should be old width*new
height/old height, or smthng**
int sizeXHour = w/3;
int sizeYHour = mHourHand.getIntrinsicHeight()*sizeXHour/mHourHand.getIntrinsicWidth();
int xHour = sizeXHour/2;
int yHour = sizeYHour/2;
canvas.rotate(mHour / 12.0f * 360.0f, xHour, yHour);
final Drawable hourHand = mHourHand;
if (changed) {
hourHand.setBounds((w / 2) - xHour, (h / 2) - yHour, sizeXHour, sizeYHour);
}
hourHand.draw(canvas);
canvas.restore();
int sizeYMinute = h/4;
int sizeXMinute = mMinuteHand.getIntrinsicWidth()*sizeYMinute/mMinuteHand.getIntrinsicHeight();
int xMinute = sizeXMinute/2;
int yMinute = sizeYMinute/2;
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f, xMinute, yMinute);
final Drawable minuteHand = mMinuteHand;
if (changed) {
minuteHand.setBounds((w / 2 - xMinute), (h / 2 - yMinute), sizeXMinute, sizeYMinute);
}
minuteHand.draw(canvas);
canvas.restore();
canvas.save();
}
I feel that something is very wrong, but can not figure what. The XML is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ee.st.running.dreamyclock.MainActivity"
android:background = "#drawable/clockk" >
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<st.running.dreamyclock.Clock
android:id="#+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical = "true"
android:layout_marginTop="124dp" />
</RelativeLayout>
the attrs are:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AnalogClock">
<attr name="hand_hour" format="reference"/>
<attr name="hand_minute" format="reference"/>
</declare-styleable>
</resources>
Any ideas where it came from? Thank you
Was this ever working? Try to add this to Clock constructor:
setWillNotDraw(false);
"If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(android.graphics.Canvas) you should clear this flag."
Edit:
If you want to re draw your view, you should call invalidate() method
"Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. "
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
this.invalidate();
}
You don't even have to call invalidate(); if you just want to for example rotate the view:
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
setRotation(20);
}
Edit 2:
When you are setting bounds for a Drawable, the "right" value should be higher than "left" for example:
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2) + 4, y + (h / 4));
or
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2) + minuteHand.getIntrinsicWidth(), y + (h / 4));
I'm trying to add multiple gridviews to a scrollview in linear layout at runtime.
But only first row displays.
Pls help
You can't nest scrollable Views in Android - i.e. ListView, GridView, ScrollView.
You could give a look at the following code:
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
public class ScrollableGridView extends GridView {
boolean expanded = true;
public ScrollableGridView(Context context)
{
super(context);
}
public ScrollableGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ScrollableGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
It is a little better version of GridView allowing it to almost work when nested in ScrollView. I said "almost work" since I found it sometimes being 20-30 pixels too short or too long.
You should note that this would prevent the views from being reuse, so it is way heavier than normal GridView.
In my case I ended up extending LinearLayout and using it to align its children in columns. It wasn't really hard - I can give you examples if you want. :)
I got the GridView example from this answer.
Here is an example for a GridView based on LinearLayout:
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class GridLikeLayout extends LinearLayout {
private static final int DEFAULT_ITEMS_PER_ROW = 1;
private final int DEFAULT_COLUMN_WIDTH = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 150, getContext().getResources()
.getDisplayMetrics());
private int itemsPerRow = DEFAULT_ITEMS_PER_ROW;
private List<View> innerViews = null;
private int columnWidth;
public GridLikeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.GridLikeLayout);
// itemsPerRow = a.getInt( R.styleable.GridLikeLayout_columns,
// DEFAULT_ITEMS_PER_ROW);
try {
columnWidth = (int) a.getDimension(
R.styleable.GridLikeLayout_column_width, DEFAULT_COLUMN_WIDTH);
} catch (UnsupportedOperationException uoe) {
columnWidth = (int) a.getInt(
R.styleable.GridLikeLayout_column_width, DEFAULT_COLUMN_WIDTH);
}
setOrientation(LinearLayout.VERTICAL);
}
public GridLikeLayout(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
}
public void setInnerViews(List<View> innerViews) {
this.innerViews = innerViews;
processViews();
}
public List<View> getInnerViews() {
return innerViews;
}
protected void processViews() {
if (null != innerViews) {
LinearLayout innerContainer = null;
innerContainer = generateInnerContainer();
int childrenCount = innerViews.size();
for (int index = 0; index < childrenCount; ++index) {
if (isFull(innerContainer)) {
addInnerContainer(innerContainer);
innerContainer = generateInnerContainer();
}
View child = innerViews.get(index);
if (null != child.getParent()) {
((ViewGroup) child.getParent()).removeView(child);
}
addInnerView(innerContainer, child);
}
addInnerContainer(innerContainer);
}
}
protected boolean isFull(LinearLayout innerContainer) {
return 0 == (innerContainer.getChildCount() % itemsPerRow)
&& 0 < innerContainer.getChildCount();
}
protected void addInnerView(LinearLayout innerContainer, View child) {
int width = LayoutParams.WRAP_CONTENT;
int height = LayoutParams.WRAP_CONTENT;
LayoutParams innerParams = new LayoutParams(width, height);
innerParams.weight = 1;
innerParams.gravity = Gravity.CENTER;
innerContainer.addView(child, innerParams);
}
protected void addInnerContainer(LinearLayout innerContainer) {
LayoutParams params = generateDefaultLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
addView(innerContainer, params);
}
protected LinearLayout generateInnerContainer() {
LinearLayout innerContainer;
innerContainer = new LinearLayout(getContext());
innerContainer.setGravity(Gravity.CENTER);
return innerContainer;
}
public void setOnInnerViewClickListener(OnClickListener listener) {
for (View innerView : innerViews) {
innerView.setOnClickListener(listener);
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED) {
if (columnWidth > 0) {
widthSize = columnWidth + getPaddingLeft() + getPaddingRight();
} else {
widthSize = getPaddingLeft() + getPaddingRight();
}
widthSize += getVerticalScrollbarWidth();
}
int childWidth = widthSize - getPaddingLeft() - getPaddingRight();
int columnsNumber = determineColumns(childWidth);
if (columnsNumber > 0 && columnsNumber != itemsPerRow) {
itemsPerRow = columnsNumber;
removeAllViews();
processViews();
}
}
protected int determineColumns(int availableSpace) {
int columnsNumber = itemsPerRow;
if (0 < columnWidth) {
columnsNumber = availableSpace / columnWidth;
}
return columnsNumber;
}
}
Here is my resource file for the custom attributes:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="column_width" format="dimension|integer">
<enum name="single_column" value="-1" />
</attr>
<declare-styleable name="GridLikeLayout">
<attr name="column_width" />
</declare-styleable>
</resources>
Here is an example usage:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/itemsScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/itemsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<your_package.view.GridLikeLayout
xmlns:my="YOUR APPLICATION PACKAGE"
android:id="#+id/MyGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
my:column_width="260dp"
android:focusable="true"
android:gravity="center_horizontal"
android:padding="5dp" >
</your_package.GridLikeLayout>
<View
android:id="#+id/viewSeparator"
android:layout_width="fill_parent"
android:layout_height="2dp" />
<your_package.GridLikeLayout
xmlns:my="YOUR APPLICATION PACKAGE"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
my:column_width="single_column" >
</your_package.GridLikeLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
Please tell me if you have any troubles running the example - I'm not able to try it at the moment. So if you have issues, I'll check it up later. Don't forget to change the package names - "your_package" should be the the package where you store the GridLikeLayout and "YOUR APPLICATION PACKAGE" is your app's package - the one specified in the application manifest. :)
Until you put here the layout I could suppose only that you have set wrong horizontal/vertical parameter in your linear layout.
Your first solution does work but does not take into account that GridViews often imply image caching such as in my case. Here your first solution leads to OutOfMemoryError every time I start to scroll.
Has anybody already got an example solution which does take into account image caching?