when findsellectedtBt() called invalidate in the findSellectedBt() don't work.
so onDraw() is not called.
why these codes don't work correctly ?
public class CustomView extends View {
private Typeface t;
private Paint paint;
private Buttons sellectedBt;
public CustomView(Context context) {
... }
public CustomView(Context context, AttributeSet attrs) {
...}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
...}
#Override
protected void onDraw(Canvas canvas) {
if(isInEditMode()) return ;
canvas.drawColor(Color.YELLOW);
switch(sellectedBt) {
case ID:
t = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
paint.setTypeface(t);
canvas.drawText("ID: 0000", 50, 100, paint);
case PHOTO:
break;
case LINE:
break;
default:
break;
}
}
public void findSellectedBt(Buttons buttonId ) {
sellectedBt = buttonId;
invalidate();
}
}
public class MainActivity extends Activity {
private CustomView customView;
private Button btId;
private CheckBox btPhoto;
private ToggleButton btLineOff;
private Button btClaer;
private ToggleButton btBlue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customView = new CustomView(this);
btId = (Button)findViewById(R.id.id);
btPhoto = (CheckBox)findViewById(R.id.photo);
btId.setOnClickListener(new OnClickListener() {
public void onClick(View v){
System.out.println("idOnclick");
customView.findSellectedBt(Buttons.ID);
}
});
btPhoto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
System.out.println("photoChecked");
if(arg1 == true)
customView.findSellectedBt(Buttons.PHOTO);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.hw4_200921275;
public enum Buttons {ID, PHOTO, LINE, CLEAR, BLUE}
Double check you're somewhere calling
findSellectedBt(buttonsObject);
By the way, notice the typo in Sellected.
Related
I am trying set interface listener from activity to custom view, but it's throwing NullPointerException. When I touch CustomRelativeLayout it will handle by dispatchTouchEvent callback method but it's throws Exception
Please help me out...
CustomRelativeLayout.class
public class CustomRelativeLayout extends RelativeLayout {
private static final String TAG = "CustomRelative";
private MapsActivity.MapTouchable mapTouchable;
public CustomRelativeLayout(Context context) {
super(context);
}
public CustomRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "dispatchTouchEvent: Action Down");
mapTouchable.onMapTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "dispatchTouchEvent: Action Up");
mapTouchable.onMapTouchEvent(false);
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "dispatchTouchEvent: Action Move");
mapTouchable.onMapTouchEvent(true);
break;
}
return super.dispatchTouchEvent(event);
}
public void setOnMapTouchListner(MapsActivity.MapTouchable mapsActivity) {
mapTouchable = mapsActivity;
}
}
here it's activity class
public class MapsActivity extends FragmentActivity {
private static final String TAG = "MapsActivity";
private CustomRelativeLayout rl_screenview;
public interface MapTouchable {
void onMapTouchEvent(boolean isTouch);
}
MapTouchable mapTouchable = new MapTouchable() {
#Override
public void onMapTouchEvent(boolean isTouch) {
Log.e(TAG, "onMapTouchEvent() called with: " + "isTouch = [" + isTouch + "]");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Log.e(TAG, "onCreate: ");
rl_screenview = (CustomRelativeLayout)findViewById(R.id.rl_screenview);
// here i am setting listner to customrelativelayout
rl_screenview.setOnMapTouchListner(mapTouchable);
}
}
Help will be appreaciated...
I click dice button in arenalayout.xml (this xml is displayed by 'ArenaLayout' class). But canvas doesn't draw char 2.I want to... if I click dice button then I draw char 2.
Note : After I click dice button, value of 'haveFirstDiced' variable in Arena class change to TRUE. There's condition inside of onDraw in 'Arena' class. . . if 'haveFirstDiced' variable have TRUE value then drawing char 2.
public class ArenaLayout extends Activity {
private Arena arena;
ImageButton diceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arena_layout);
diceButton = (ImageButton) findViewById(R.id.dice);
dice();
}
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
}
This is 'Arena' class :
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char1;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
}
public void dicing() {
new Thread(new XXX()).start();
}
#Override
synchronized public void onDraw(Canvas canvas) {
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
class XXX implements Runnable {
#Override
public void run() {
threadHandler.post(new Runnable() {
#Override
public void run() {
haveFirstDiced = true;
}
});
}
}
}
This is the most convoluted solution I've ever seen. There's no need for a thread, a runnable, or for onDraw to be synchronized. Here's what it should be:
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char2;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
}
public void dicing() {
haveFirstDiced = true;
invalidate();
}
#Override
public void onDraw(Canvas canvas) {
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
}
SOLVED: Solution below as answer.
I have a custom view with a TransitionDrawable and when I draw it in the onDraw() method it scales automatically to fill the whole parent layout, even when it's set in the xml to wrap_content. The picture is in mdpi and hdpi and my testing device (samsung galaxy s) I think it's no more than hdpi.
package com.adyrsoft.pronunciationtrainer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class RecordButton extends View {
private static final String TAG = "RecordButton";
private TransitionDrawable mDrawable;
private boolean mActivated;
private OnClickListener mOnClickListenerInternal = new OnClickListener() {
#Override
public void onClick(View v) {
toggleState();
if(mOnClickListener != null) {
mOnClickListener.onClick(v);
}
}
};
private OnClickListener mOnClickListener = null;
public RecordButton(Context context) {
super(context);
init();
}
public RecordButton(Context context, AttributeSet attrib) {
super(context, attrib);
init();
}
public RecordButton(Context context, AttributeSet attrib, int defStyle) {
super(context, attrib, defStyle);
init();
}
public void setState(boolean activated) {
mActivated = activated;
if(mActivated){
mDrawable.startTransition(300);
}
else {
mDrawable.reverseTransition(300);
}
}
public void toggleState() {
if(mActivated) {
setState(false);
}
else {
setState(true);
}
}
#SuppressWarnings("deprecation")
private void init() {
mActivated = false;
mDrawable = (TransitionDrawable) getResources().getDrawable(R.drawable.btnrecord);
Log.d(TAG, "Drawable intrinsic width and height are: " +
Integer.toString(mDrawable.getIntrinsicWidth()) + " " +
Integer.toString(mDrawable.getIntrinsicHeight()));
mDrawable.setBounds(0,0,mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
Log.d(TAG, "The bounds for the button are: "+mDrawable.getBounds().flattenToString());
super.setBackgroundDrawable(mDrawable);
setClickable(true);
super.setOnClickListener(mOnClickListenerInternal);
invalidate();
}
public void setOnClickListener(View.OnClickListener listener) {
mOnClickListener = listener;
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
What am I doing wrong?
Thanks.
After hours trying to understand how I should use the drawables in a custom view in order to be displayed in its original size, I've figured out how to do it.
First a few things that I didn't know but are a must is:
The background drawable should be left to the parent class to be
drawn when using View as the parent. If not, the TransitionDrawable can't be seen fading between pictures.
Only if I am going to draw on the background drawable I should override onDraw() and do the drawing there.
And the last but not less important is that I should override onMeasure() to specify the size of the view. If I don't do it, it will fill all the free space in the parent layout, as it was happening to me.
I've passed the TransitionDrawable to the parent class with setBackgroundDrawable() and since I wasn't drawing in the background drawable, I've removed the onDraw() method. Also I've implemented onMeasure() with a quick and dirty solution specifying the size of the picture I am drawing.
This is the final result:
public class RecordButton extends View {
private static final String TAG = "RecordButton";
private static final int DESIRED_WIDTH = 180;
private static final int DESIRED_HEIGHT = 66;
private TransitionDrawable mDrawable;
private Rect mViewRect;
private boolean mActivated;
private OnClickListener mOnClickListenerInternal = new OnClickListener() {
#Override
public void onClick(View v) {
toggleState();
if(mOnClickListener != null) {
mOnClickListener.onClick(v);
}
}
};
private OnClickListener mOnClickListener = null;
public RecordButton(Context context) {
this(context, null, 0);
}
public RecordButton(Context context, AttributeSet attrib) {
this(context, attrib, 0);
}
public RecordButton(Context context, AttributeSet attrib, int defStyle) {
super(context, attrib, defStyle);
init();
}
public void setState(boolean activated) {
mActivated = activated;
if(mActivated){
mDrawable.startTransition(300);
}
else {
mDrawable.reverseTransition(300);
}
}
public void toggleState() {
if(mActivated) {
setState(false);
}
else {
setState(true);
}
}
#SuppressWarnings("deprecation")
private void init() {
mActivated = false;
mDrawable = (TransitionDrawable) getResources().getDrawable(R.drawable.btnrecord);
setBackgroundDrawable(mDrawable);
setClickable(true);
super.setOnClickListener(mOnClickListenerInternal);
invalidate();
}
public void setOnClickListener(View.OnClickListener listener) {
mOnClickListener = listener;
}
#Override
protected void onMeasure(int m, int n) {
setMeasuredDimension(DESIRED_WIDTH, DESIRED_HEIGHT);
}
}
I'm using Android Studio to create a small drag and drop application. I have followed all the rules I know and the code doesn't seem to have any errors, however when I run it on my device it simple crashes. Anyone know where its wrong?
The code is fine until initialise(); is called in public void blue(View v)
so I'm suspecting the error is there
public class MainActivity extends Activity {
private ImageView blueball;
private ImageView blueballdrag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void colourGen(View view){
int i =1;
if (i==i){
blue(view);
}
}
public void brown(View v){
setContentView(R.layout.activity_brown);
}
public void yellow (View v){
setContentView(R.layout.activity_yellow);
}
public void green (View v){
setContentView(R.layout.activity_green);
}
public void blue (View v){
setContentView(R.layout.activity_blue);
initialise();
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void initialise() {
final ImageView imageView = (ImageView) blueballdrag.findViewById(R.id.imageView4);
imageView.setOnDragListener(new View.OnDragListener() {
public boolean onDrag(View v, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
v.setBackgroundColor(Color.RED);
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(Color.BLACK);
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.GREEN);
case DragEvent.ACTION_DROP:
v.setBackgroundColor(Color.WHITE);
}
return false;
}
});
blueball = (ImageView) findViewById(R.id.imageView6);
blueball.setOnLongClickListener(new OnLongClickListener(){
#Override
public boolean onLongClick(View v) {
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(blueball);
v.startDrag(null, myShadow, null, 0);
return false;
}
});
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public MyDragShadowBuilder(View v) {
super(v);
shadow = new ColorDrawable(Color.RED);
}
public void onProvideShadowMetrics(Point size, Point touch){
int width, height;
width = getView().getWidth() * 2;
height = getView().getHeight() * 2;
shadow.setBounds(0, 0, width, height);
size.set(width, height);
touch.set(width*2, height*2);
}
public void onDrawShadow(Canvas canvas){
shadow.draw(canvas);
}
}
}
The problem is the line
final ImageView imageView = (ImageView) blueballdrag.findViewById(R.id.imageView4);
This line tells the Object blueballdrag to find a child view called imageView4. I'm guessing that your ImageViews don't have children. You want the findViewById() method of your Activity, not your View.
Changing the line to the following should solve your problem.
final ImageView imageView = (ImageView) findViewById(R.id.imageView4);
I'm using a simple Vertical SeekBar that is working fine if I initiate it on the onCreate function. But if I initiate the SeekBar on a different thread the thumb position does not work as expected.
The code I'm using to initiate the SeekBar:
public class MainActivity extends Activity {
AbsoluteLayout mainLayout;
VerticalSeekBar seekBar;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
mainLayout=(AbsoluteLayout)findViewById(R.id.main_layout_id);
Thread thread1 =new Thread(runnable1);
thread1.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private Runnable runnable1 = new Runnable(){
#Override
public void run() {
seekBar=new VerticalSeekBar(context);
loadMainScreen.sendEmptyMessage(0);
}
};
Handler loadMainScreen = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
AbsoluteLayout.LayoutParams parameters = new AbsoluteLayout.LayoutParams(40,200,150,30);
mainLayout.addView(seekBar,parameters);
}
};
}
I do not think the problem is on the SeekBar but I will show the code anyway.
public class VerticalSeekBar extends SeekBar implements OnSeekBarChangeListener {
public VerticalSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
I try changing the Context I'm passing to the variable context to getApplicationContext() and getBaseContext() but it did not work.
If I put the initialization on a runOnUiThread it crashes because the SeekBar is not initialized when I try to add to the layout. If I put both the initialization and the addView on a runOnUiThread it works but that is not what I want.
I was wondering if there is something I could do regarding the Context to make it work. Any different ideas are welcome.
Thanks in advance