How to have a Viewgroup containing multiple ImageButton in android? - android

While creating a custom Viewgroup,is it possible to have multiple Imagebutton within it?
is it possible to place Image buttons in our own position.
If all these are possible,how to call the click listener which extending this viewgroup?
My ViewGroup have to look like this image
EDIT 1:
public class CustomViewGroup extends ViewGroup{
public CustomViewGroup(Context context) {
super(context);
setWillNotDraw(false);
//addImageView();
// TODO Auto-generated constructor stub
}
public CustomViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
//addImageView();
// TODO Auto-generated constructor stub
}
public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs,
defStyle);
setWillNotDraw(false);
//addImageView();
// TODO Auto-generated constructor stub
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
System.out.println("onLayout");
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
drawBackground(canvas);
addImageView();
//super.onDraw(canvas);
}
private void drawBackground(Canvas canvas)
{
Bitmap background =BitmapFactory.decodeResource(getContext().getResources(), R.drawable.shape_quarter_circle);
canvas.drawBitmap(background, 0,0, null);
}
private void addImageView()
{
ImageView imageOne = new ImageView(getContext());
imageOne.setImageDrawable(getContext().getResources().getDrawable(R.drawable.round_icon));
//imageOne.setWillNotDraw(false);
addView(imageOne);
requestLayout();
}
I am trying to draw a background and place some ImageView on the top of the background.
In this code Background image is getting displayed correctly. but i could not see the ImageView drawn upon it.
Am i going in the correct path?

all the things you said are possible.
you just use
yourViewGroup.setOnClickListener()
for your view group click listener.

Related

Ondraw method not calling in custom view

I am making custom view using custom ViewGroup and custom View in Android.
public class Custom_ViewGroup extends ViewGroup
{
public Custom_ViewGroup(Context context)
{
super(context);
addView(new OwnView(context));
}
public Custom_ViewGroup(Context context,AttributeSet attrs)
{
super(context, attrs);
addView(new OwnView(context));
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub
}
class OwnView extends View
{
public OwnView(Context context)
{
super(context);
System.out.println("on constructor");
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
System.out.println("ondraw child");
}
}
}
onDraw() method of OwnView class is not calling. constructor of OwnView class called. I have used invalidate() method after adding view, but it did not work.
This is your problem
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub
}
Your view is never laid-out, so it will not draw. You need to implement the onLayout method properly. Also, if your ViewGroup contains only a single view, consider using FrameLayout instead of ViewGroup.

Android: Customized NumberPikcer can't roll

this is my custom numberpicker
public class CustomNumberPicker extends NumberPicker {
public CustomNumberPicker(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomNumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}
Because this numberpicker is in a scrollview, and it's hard to roll the number,
so i override onInterceptTouchEvent(). This customized method works well on TimePicker.
But after customing, the numberpicker can not roll,but the uparrow and downarrow works well.
Is there anyone who has solution to this problem? Thanks!!!!!!

How to get background color of EditText on android

I would like to get color of EditText, I could set it by setBackgroundColor, but there is not getBackgroundColor function
I found this
EditText edtxt;
edtxt.setBackgroundColor(Color.GREEN);
PaintDrawable drawable;
Log.d(TAG,"1");
drawable = (PaintDrawable)edtxt.getBackground();
if(drawable.getPaint().getColor()==(int)Color.GREEN).........
Log.d(TAG,"2");
but its not working and crashing
05-29 19:20:27.526: E/AndroidRuntime(20255): Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.PaintDrawable
This should work for API level 11 and up
ColorDrawable drawable = (ColorDrawable)edtxt.getBackground();
if(drawable.getColor()==(int)Color.GREEN)
System.out.println("It's Green");
If you want it to wok on earlier APIs, I would suggest using a Custom EditText and overriding the setBackgroundColor(int color) method.
public class NewEditText extends EditText {
private int color;
public NewEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public NewEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public NewEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void setBackgroundColor(int color) {
// TODO Auto-generated method stub
this.color=color;
super.setBackgroundColor(color);
}
public int getBackgroundColor() {
return color;
}
}
Now in the Layout use:
<com.aneesh.mypackage.NewEditText
android:layout_width="fill_parent"
android:id="#+id/customview"
android:layout_height="wrap_content"/>
and your Activity Code will change to
NewEditText custView = (NewEditText)findViewById(R.id.customview);
custView.setBackgroundColor(Color.GREEN);
if(custView.getBackgroundColor()==(int)Color.GREEN)
System.out.println("It's green");
If you are setting the color in runtime it could be better to save some kind of flag (boolean for example) to know which is the background color of the edit text.

rotating a TextView and remeasuring in Android

I'm trying to create a vertical textview with the following code
public class VerticalTextView extends TextView{
public VerticalTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
Log.i("VerticalTextView", "onDraw Called");
canvas.save();
canvas.rotate(-90, getWidth() / 2, getHeight() / 2);
super.onDraw(canvas);
canvas.restore();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
}
however I'm finding that it is not redefining the full width in the new height. and therefore the texts is really truncated.
Any ideas?
m
In this similar question (look at kostmo answer not the accepted one), he uses two custom methods to compute the size of the View (measureHeight and measureWidth).
So super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); looks wrong according to his answer.
It seems you can as well copy/paste his answer, I think it is what you are trying to achieve.

Creating Custom ImageView

I create a custom image view by extending ImageView that just draws some text on the screen, however I don't see anything painted in the Emulator Screen, but the log messages and the printlns get printed in the log console. Am I not doing something?
This is my activity
public class HelloAndroidActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
CustomImageView myView = new CustomImageView(getApplicationContext());
System.out.println("Setting the view");
myView.invalidate();
setContentView(myView);
System.out.println("Calling invalidate");
}
}
This is my CustomImageView
public class CustomImageView extends ImageView
{
/**
* #param context
*/
public CustomImageView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
setBackgroundColor(0xFFFFFF);
}
/**
* #param context
* #param attrs
*/
public CustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* #param context
* #param attrs
* #param defStyle
*/
public CustomImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
System.out.println("Painting content");
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG);
paint.setColor(0x0);
paint.setTextSize(12.0F);
System.out.println("Drawing text");
canvas.drawText("Hello World in custom view", 100, 100, paint);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
// TODO Auto-generated method stub
Log.d("Hello Android", "Got a touch event: " + event.getAction());
return super.onTouchEvent(event);
}
}
Even the log message in the onTouchEvent() gets printed, but nothing is painted.
This is my main.xml that has the layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/AbsoluteLayout">
</AbsoluteLayout>
Use color values Color.WHITE or Color.BLACK instead of hexa values.
Have you checked your canvas size? An image view expects the bitmap/drawable to return its size and based on the scaletype flags, determine the size of the view. I don't see anything in your code that determines the size of the view for layout needs.
-Rick

Categories

Resources