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!!!!!!
Related
I am trying build simple library for button,
inside library folder I created below class
public class SimpleImageButton extends AppCompatImageView implements AppCompatImageView.OnClickListener{
public Context mContext;
Activity activity;
public SimpleImageButton (Context context) {
super(context);
mContext = context;
setCustomTypeface(context, null);
}
public SimpleImageButton (Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setCustomTypeface(context, attrs);
}
public SimpleImageButton (Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
mContext = context;
setCustomTypeface(context, attrs);
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setCustomTypeface(Context context, AttributeSet attrs) {
if(isInEditMode())
return;
TypedArray a = context.obtainStyledAttributes(attrs,
android.support.v7.appcompat.R.styleable.TextAppearance);
setBackground(ContextCompat.getDrawable(mContext,
R.drawable.applogo_ads));
a.recycle();
}
public void onClick(View view) {
// here i have some functions to execute
}
}
and my Mainclass in App folder
SimpleImageButton imgBtn= (SimpleImageButton )findViewById(R.id.clickButton);
imgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imgBtn.onClick(view);
// without this line how can i reach to onclick() of simpleImageButton class
}
});
so it is woking fine when i click button. but i want to make library button to work directly, without onClick function inside main activity, on clicking button should directly redirect to SimpleImageButton class onclcik method
I am very new to stack overflow , if any mistakes in the grammar / way of asking question please never mind.
thank you.
Use setOnClickListener(this) inside your view's constructor.
public SimpleImageButton (Context context) {
super(context);
mContext = context;
setCustomTypeface(context, null);
setOnClickListener(this);
}
I just override the this dispatchTouchEvent and dispatchKeyEvent and
create own public function setOnClickListener(OnClickListener listener) to set on click listener
public class SimpleImageButton extends AppCompatImageView{
//...
private OnClickListener listener;
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (listener != null) listener.onClick(this);
}
return super.dispatchTouchEvent(event);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
if (listener != null) listener.onClick(this);
}
return super.dispatchKeyEvent(event);
}
public void setOnClickListener(OnClickListener listener) {
this.listener = listener;
}
}
Now I hope it will be clickable just like other buttons.
I'm trying to make a popup menu appear when the user clicks on an EditText but I don't want the EditText itself to be editable. I've tried many things like setting its KeyListener to null, setting it's InputType to null, but what always happens is that the first click gives the View focus and the second click actually registers with my OnClickListener. So user has to click twice to get the menu to popup. Any ideas?
public class PopupEditText extends EditText implements OnClickListener
{
private PopupMenu mMenu;
private Context mContext;
public PopupEditText(Context context)
{
super(context);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
mContext = context;
setKeyListener(null);
mMenu = new PopupMenu(context, this);
}
#Override
public void onClick(View view)
{
mMenu.show();
}
You're not far off. I'd recommend just overriding onTouchEvent() directly, and responding only to ACTION_UP events. Optionally, force it to be disabled and non-focusable. For example:
public class UnmodifiableEditText extends EditText {
private PopupMenu mPopupMenu;
public UnmodifiableEditText(Context context) {
super(context);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
super.setEnabled(false);
super.setFocusable(false);
mPopupMenu = new PopupMenu(ctx, this);
}
#Override public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getContext(), "Showing Popup", Toast.LENGTH_SHORT).show();
mPopupMenu.show();
}
return true;
}
#Override public void setEnabled(boolean enabled) {
// Do not allow enabling the EditText
}
#Override public void setFocusable(boolean focusable) {
// Do not allow focusability changes
}
}
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.
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.
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.