I am using the DroidParts library for Clearable EditTexts (http://droidparts.org/widgets.html#clearableedittext). The question is how to keep the cross/clear button on the first line. I have no idea where to start looking so any help would be great! Thanks
Code is this:
import static org.droidparts.util.Strings.isNotEmpty;
import org.droidparts.adapter.widget.TextWatcherAdapter;
import org.droidparts.adapter.widget.TextWatcherAdapter.TextWatcherListener;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
public class ClearableEditText extends EditText implements OnTouchListener,
OnFocusChangeListener, TextWatcherListener {
public interface Listener {
void didClearText();
}
public void setListener(Listener listener) {
this.listener = listener;
}
private Drawable xD;
private Listener listener;
public ClearableEditText(Context context) {
super(context);
init();
}
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
xD = getCompoundDrawables()[2];
if (xD == null) {
xD = getResources()
.getDrawable(android.R.drawable.presence_offline);
}
xD.setBounds(0, 0, xD.getIntrinsicWidth(), xD.getIntrinsicHeight());
setClearIconVisible(false);
super.setOnTouchListener(this);
super.setOnFocusChangeListener(this);
addTextChangedListener(new TextWatcherAdapter(this, this));
}
#Override
public void setOnTouchListener(OnTouchListener l) {
this.l = l;
}
#Override
public void setOnFocusChangeListener(OnFocusChangeListener f) {
this.f = f;
}
private OnTouchListener l;
private OnFocusChangeListener f;
#Override
public boolean onTouch(View v, MotionEvent event) {
if (getCompoundDrawables()[2] != null) {
if (event.getAction() == MotionEvent.ACTION_UP) {
boolean tappedX = event.getX() > (getWidth()
- getPaddingRight() - xD.getIntrinsicWidth());
if (tappedX) {
setText("");
if (listener != null) {
listener.didClearText();
}
return true;
}
}
}
if (l != null) {
return l.onTouch(v, event);
}
return false;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(isNotEmpty(getText()));
} else {
setClearIconVisible(false);
}
if (f != null) {
f.onFocusChange(v, hasFocus);
}
}
#Override
public void onTextChanged(EditText view, String text) {
if (isFocused()) {
setClearIconVisible(isNotEmpty(text));
}
}
protected void setClearIconVisible(boolean visible) {
Drawable x = visible ? xD : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], x, getCompoundDrawables()[3]);
}
}
Is there another method that will have the same effect?
Related
How can one detect click events on compound drawables of a TextInputEditText?
Use the following overriden version of TextInputEditText, and call setOnDrawableClickedListener.
You may fare better if you set your drawable at the end of the edit text than at the start, because the current version of TextInputLayout produces fairly ugly results when the drawable is at the start.
Sample layout is given further down. (Note the use of android:drawablePadding="10dp" particularly).
Code is for androidx, but you can backport to AppCompat trivially.
package com.twoplay.netplayer.controls;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.google.android.material.textfield.TextInputEditText;
public class TextInputEditTextEx extends TextInputEditText {
private OnDrawableClickedListener onDrawableClickedListener;
public TextInputEditTextEx(Context context) {
super(context);
init();
}
public TextInputEditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextInputEditTextEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setOnTouchListener(new OnTouchListener() {
private Rect hitBounds = new Rect();
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
int hitDrawable = -1;
if (x < getCompoundPaddingLeft())
{
hitDrawable = 0;
hitBounds.set(0,0,getCompoundPaddingLeft(),getHeight());
}
if (x > getWidth()-getCompoundPaddingRight())
{
hitDrawable = 2;
hitBounds.set(getCompoundPaddingRight(),0,getWidth(),getHeight());
}
if (hitDrawable != -1)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_UP)
{
onDrawableClicked(hitDrawable,hitBounds);
}
return true;
}
return false;
}
});
}
private void onDrawableClicked(int i, Rect bounds) {
if (onDrawableClickedListener != null)
{
onDrawableClickedListener.onDrawableClicked(this,i,bounds);
}
}
public interface OnDrawableClickedListener {
void onDrawableClicked(View v, int drawable, Rect bounds);
}
public void setOnDrawableClickedListener(OnDrawableClickedListener listener)
{
this.onDrawableClickedListener = listener;
}
}
Sample layout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/playlist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/playlist_name" >
<com.twoplay.netplayer.controls.TextInputEditTextEx
android:id="#+id/playlist_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:drawableEnd="#drawable/ic_more_horiz_black_24dp"
android:drawablePadding="10dp" />
</com.google.android.material.textfield.TextInputLayout>
I want to make horizontally infinite viewpager, which scroll continue both side left and right.
Anyone have idea then please let me know.
I have implement these all classes below please check it and let me know if any one have any new idea.
1. Make **HorizontalInfiniteCycleViewPager** class.
package com.test.HorizontalInfiniteCycleViewPager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
/**
* Created by GIGAMOLE on 7/27/16.
*/
public class HorizontalInfiniteCycleViewPager extends ViewPager implements ViewPageable {
private InfiniteCycleManager mInfiniteCycleManager;
public HorizontalInfiniteCycleViewPager(final Context context) {
super(context);
init(context, null);
}
public HorizontalInfiniteCycleViewPager(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(final Context context, final AttributeSet attributeSet) {
mInfiniteCycleManager = new InfiniteCycleManager(context, this, attributeSet);
}
public float getMinPageScaleOffset() {
return mInfiniteCycleManager == null ? 0.0F : mInfiniteCycleManager.getMinPageScaleOffset();
}
public void setMinPageScaleOffset(final float minPageScaleOffset) {
if (mInfiniteCycleManager != null)
mInfiniteCycleManager.setMinPageScaleOffset(minPageScaleOffset);
}
public float getCenterPageScaleOffset() {
return mInfiniteCycleManager == null ? 0.0F : mInfiniteCycleManager.getCenterPageScaleOffset();
}
public void setCenterPageScaleOffset(final float centerPageScaleOffset) {
if (mInfiniteCycleManager != null)
mInfiniteCycleManager.setCenterPageScaleOffset(centerPageScaleOffset);
}
public float getMinPageScale() {
return mInfiniteCycleManager == null ? 0.0F : mInfiniteCycleManager.getMinPageScale();
}
public void setMinPageScale(final float minPageScale) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.setMinPageScale(minPageScale);
}
public float getMaxPageScale() {
return mInfiniteCycleManager == null ? 0.0F : mInfiniteCycleManager.getMaxPageScale();
}
public void setMaxPageScale(final float maxPageScale) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.setMaxPageScale(maxPageScale);
}
public boolean isMediumScaled() {
return mInfiniteCycleManager != null && mInfiniteCycleManager.isMediumScaled();
}
public void setMediumScaled(final boolean mediumScaled) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.setMediumScaled(mediumScaled);
}
public int getScrollDuration() {
return mInfiniteCycleManager == null ? 0 : mInfiniteCycleManager.getScrollDuration();
}
public void setScrollDuration(final int scrollDuration) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.setScrollDuration(scrollDuration);
}
public Interpolator getInterpolator() {
return mInfiniteCycleManager == null ? null : mInfiniteCycleManager.getInterpolator();
}
public void setInterpolator(final Interpolator interpolator) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.setInterpolator(interpolator);
}
public boolean isVertical() {
return mInfiniteCycleManager != null && mInfiniteCycleManager.isVertical();
}
public OnInfiniteCyclePageTransformListener getOnInfiniteCyclePageTransformListener() {
return mInfiniteCycleManager == null ?
null : mInfiniteCycleManager.getOnInfiniteCyclePageTransformListener();
}
public void setOnInfiniteCyclePageTransformListener(
final OnInfiniteCyclePageTransformListener onInfiniteCyclePageTransformListener
) {
if (mInfiniteCycleManager != null)
mInfiniteCycleManager.setOnInfiniteCyclePageTransformListener(onInfiniteCyclePageTransformListener);
}
#Override
public void setPageTransformer(final boolean reverseDrawingOrder, final PageTransformer transformer) {
super.setPageTransformer(
false, mInfiniteCycleManager == null ?
transformer : mInfiniteCycleManager.getInfinityCyclePageTransformer()
);
}
#Override
protected void setChildrenDrawingOrderEnabled(final boolean enabled) {
super.setChildrenDrawingOrderEnabled(InfiniteCycleManager.DEFAULT_DISABLE_FLAG);
}
#Override
public void setClipChildren(final boolean clipChildren) {
super.setClipChildren(InfiniteCycleManager.DEFAULT_DISABLE_FLAG);
}
#Override
public void setDrawingCacheEnabled(final boolean enabled) {
super.setDrawingCacheEnabled(InfiniteCycleManager.DEFAULT_DISABLE_FLAG);
}
#Override
protected void setChildrenDrawingCacheEnabled(final boolean enabled) {
super.setChildrenDrawingCacheEnabled(InfiniteCycleManager.DEFAULT_DISABLE_FLAG);
}
#Override
public void setWillNotCacheDrawing(final boolean willNotCacheDrawing) {
super.setWillNotCacheDrawing(InfiniteCycleManager.DEFAULT_ENABLE_FLAG);
}
#Override
public void setPageMargin(final int marginPixels) {
super.setPageMargin(InfiniteCycleManager.DEFAULT_PAGE_MARGIN);
}
#Override
public void setOffscreenPageLimit(final int limit) {
super.setOffscreenPageLimit(InfiniteCycleManager.DEFAULT_OFFSCREEN_PAGE_LIMIT);
}
#Override
public void setOverScrollMode(final int overScrollMode) {
super.setOverScrollMode(OVER_SCROLL_NEVER);
}
#Override
protected boolean addViewInLayout(final View child, final int index, final ViewGroup.LayoutParams params) {
return super.addViewInLayout(child, 0, params);
}
#Override
public void addView(final View child, final int index, final ViewGroup.LayoutParams params) {
super.addView(child, 0, params);
}
#Override
public void setAdapter(final PagerAdapter adapter) {
if (mInfiniteCycleManager == null) super.setAdapter(adapter);
else {
super.setAdapter(mInfiniteCycleManager.setAdapter(adapter));
mInfiniteCycleManager.resetPager();
}
}
#Override
public PagerAdapter getAdapter() {
if (mInfiniteCycleManager == null) return super.getAdapter();
return mInfiniteCycleManager.getInfiniteCyclePagerAdapter() == null ? super.getAdapter() :
mInfiniteCycleManager.getInfiniteCyclePagerAdapter().getPagerAdapter();
}
#Override
public boolean onTouchEvent(final MotionEvent ev) {
try {
return mInfiniteCycleManager == null ? super.onTouchEvent(ev) :
mInfiniteCycleManager.onTouchEvent(ev) && super.onTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
}
#Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
try {
return mInfiniteCycleManager == null ? super.onInterceptTouchEvent(ev) :
mInfiniteCycleManager.onInterceptTouchEvent(ev) && super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
return true;
}
}
#Override
public void onWindowFocusChanged(final boolean hasWindowFocus) {
if (mInfiniteCycleManager != null)
mInfiniteCycleManager.onWindowFocusChanged(hasWindowFocus);
super.onWindowFocusChanged(hasWindowFocus);
}
#Override
protected void onDetachedFromWindow() {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
super.onDetachedFromWindow();
}
#Override
public void setCurrentItem(final int item) {
setCurrentItem(item, true);
}
#Override
public void setCurrentItem(final int item, final boolean smoothScroll) {
if (mInfiniteCycleManager != null)
super.setCurrentItem(mInfiniteCycleManager.setCurrentItem(item), true);
}
public int getRealItem() {
return mInfiniteCycleManager == null ?
getCurrentItem() : mInfiniteCycleManager.getRealItem();
}
public int getState() {
return mInfiniteCycleManager == null ?
ViewPager.SCROLL_STATE_IDLE : mInfiniteCycleManager.getState();
}
public void notifyDataSetChanged() {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.notifyDataSetChanged();
}
public void invalidateTransformer() {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.invalidateTransformer();
}
public void postInvalidateTransformer() {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.postInvalidateTransformer();
}
public void startAutoScroll(final boolean isAutoScrollPositive) {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.startAutoScroll(isAutoScrollPositive);
}
public void stopAutoScroll() {
if (mInfiniteCycleManager != null) mInfiniteCycleManager.stopAutoScroll();
}
}
I am trying to implement Locker App and swipe unlock functionality using android seekbar.
What I want is, swipe to unlock from right to left and if user release touch from drawable then it should come back to right side of the layout.
Using following code, I am able to achieve the same but for Left to Right direction, I want to do the same in opposite direction i.e Right to Left.
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class SlideButton extends SeekBar {
private Drawable thumb;
private SlideButtonListener listener;
public SlideButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
this.thumb = thumb;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (thumb.getBounds().contains((int) event.getX(), (int) event.getY())) {
super.onTouchEvent(event);
} else
return false;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (getProgress() > 70)
handleSlide();
setProgress(0);
} else
super.onTouchEvent(event);
return true;
}
private void handleSlide() {
listener.handleSlide();
}
public void setSlideButtonListener(SlideButtonListener listener) {
this.listener = listener;
}
}
interface SlideButtonListener {
public void handleSlide();
}
NOTE: In above code when user click on drawable to swipe from left to right, and if he releases his finger from drawable, with seekbar Progress < 70, then the icon/drawable comes back to original position i.e to left corner.
So, my question is simple how could I achieve same functionality using above code to swipe unlock from right to left.
I have tried to achieve this using following code, it do shows me drawable icon to right side but when i try to swipe it from right to left it doesn't functions as expected.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class ReversedSeekBar extends SeekBar {
private Drawable thumb;
private SlideButtonListener listener;
public ReversedSeekBar(Context context) {
super(context);
}
public ReversedSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReversedSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
this.thumb = thumb;
}
#Override
protected void onDraw(Canvas canvas) {
float px = this.getWidth() / 2.0f;
float py = this.getHeight() / 2.0f;
canvas.scale(-1, 1, px, py);
//canvas.rotate(180, px, py);
super.onDraw(canvas);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (thumb.getBounds().contains((int) event.getX(), (int) event.getY())) {
super.onTouchEvent(event);
} else
return false;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (getProgress() > 70)
handleSlide();
setProgress(0);
} else
super.onTouchEvent(event);
return true;
}
private void handleSlide() {
listener.handleSlide();
}
public void setSlideButtonListener(SlideButtonListener listener) {
this.listener = listener;
}
}
Thanks...
Try adding these two lines to xml. (It did the trick for me)
android:layoutDirection="rtl"
android:mirrorForRtl="true"
In Kotlin:
Check the default language code containing "ar"
when {
Locale.getDefault().language == "ar" -> {
clSeekbar.scaleX = -1
}
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);
}
}
My view is written as follow:
package com.mycompany;
import android.view.View;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.graphics.Paint;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.*;
public class GameEngineView extends View implements SensorEventListener {
GameLoop gameloop;
String txt_acc;
float accY;
ArrayList<Point> bugPath;
private SensorManager sensorManager;
private class GameLoop extends Thread {
private volatile boolean running = true;
public void run() {
while (running) {
try {
TimeUnit.MILLISECONDS.sleep(1);
postInvalidate();
pause();
} catch (InterruptedException ex) {
running = false;
}
}
}
public void pause() {
running = false;
}
public void start() {
running = true;
run();
}
public void safeStop() {
running = false;
interrupt();
}
}
public void unload() {
gameloop.safeStop();
}
public GameEngineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(context);
}
public GameEngineView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
public GameEngineView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context context) {
txt_acc = "";
// Adding SENSOR
sensorManager=(SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
// Adding UI Elements : How ?
Button btn_camera = new Button(context);
btn_camera.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
btn_camera.setClickable(true);
btn_camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("clicked the camera.");
}
});
gameloop = new GameLoop();
gameloop.run();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println("Width " + widthMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.FILL);
p.setAntiAlias(true);
p.setTextSize(30);
canvas.drawText("|[ " + txt_acc + " ]|", 50, 500, p);
gameloop.start();
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
//float x=event.values[0];
accY =event.values[1];
//float z=event.values[2];
txt_acc = "" + accY;
}
}
}
I would like to add a Button to the scene, but I don't know how to. Can anybody give me some lights?
UPDATE: Here is my Activity :
public class MyActivity extends Activity {
private GameEngineView gameEngine;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add Game Engine
gameEngine = new GameEngineView(this);
setContentView(gameEngine);
gameEngine.requestFocus();
}
}
And I am following the this tutorial .
detailListView = (LinearLayout) findViewById(R.id.DetailsLinearLayout); // Find the layout where you want to add button
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
,ViewGroup.LayoutParams.WRAP_CONTENT));
detailListView.addView(button);//add view to add