Draw a circle partially inside on a button dynamically android - android

My class Triangle extends from View and its method onDraw consists of:
... onDraw(){
...
drawCircle(anyx, anyy, anyradius, anypaint);
}
And the Activity in which I want to create the Button and the Circle, both created dynamically.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newexample);
RelativeLayout viewGroup = (RelativeLayout)findViewById(R.id.visiont);
Button b1 = new Button(this);
viewGroup.addView(b1);
b1.setX(140); // Position of the button
b1.setY(140);
ViewGroup.LayoutParams params = b1.getLayoutParams();
params.height=80; // Size of the button
params.width=80;
b1.setLayoutParams(params); // Deleting this does not change the behaviour of what I am getting (only changes the height and width)
Circle c = new Circle(this);
c.setColor(Color.DKGRAY);
c.setCircle(150,130,80);
viewGroup.addView(c);
}
An example of what is the desired result, the rectangle is the button.
What I am getting now is the opposite, the button is over the circle.

I tried to switch Button to ImageButton since I wanted to have a custom image in the button (Actually I wanted a clickable component)
It appears that, with only switching Button to ImageButton, works as desired, so, for me it's a suitable solution.

Related

How to connect button actions with surfaceview canvas drawables?

My problem is that I don't know how to change position of a canvas drawable on click of
my button that it's located in the main activity and not to surfaceview so the code to work should be on surfaceview here is what I've got
public class JumpOnClick extends Activity implements OnClickListener{
DrawingSurface ds;
FrameLayout frm;
Button btnC;
int color=0xfff00000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ds=new DrawingSurface(this);
setContentView(R.layout.activity_jump_on_click);
frm=(FrameLayout)findViewById(R.id.frameLayout);
frm.addView(ds);
btnC=(Button)findViewById(R.id.buttonColor);
btnC.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// how to connect actions in here for my sprites on my surfaceview
// for example AnimatedSprite a = new AnimatedSprite();
// if(b == null)
//b=BitmapFactory.decodeStream(res.openRawResource(R.drawable.explosion));
// a.Initialize(b, 120, 159, 7, 20, true);
// mSprites.add(a);
}
}
if I understand correctly,
define two static float variables in your surfaceview class respectively x and y, and when you do your drawing in your surfaceview just use this variables as position parameters in your surfaceview's draw method. And then back in your activity in your onClick method of the button just change these x,y variables, as you can access them without any need to create object.
I believe that you need to add layout rules to your button to place it in the Surfaceview (Actually a button cant be added to a SurfaceView, you need to create an illusion with a Layout in your Activity and place the button to this Layout).
You can check this simple solution at this video https://www.youtube.com/watch?v=8qE9TQjQ5no&t=31s
that is placing buttons to a SurfaceView at the top and bottom using a Relativelayout. My opinion is with that technique you wont need the ontouchevent method neither the x,y position, you will need that if you create a button in the ondraw method (then wont be a button but a bitmap image)...My opinion is to stick with the first solution if the position is the problem so it can act as a button with the onclicklistener.

Adding button on top of Imageview at runtime

I am creating a imageview at runtime and now want a add a button on top of the Imageview dynamically at run time and if I pan the image in the imageview the button in it should also pan accordingly. The problem is that Imageview does not allow to add subviews in it.
I am able to add the button on top of the imageview but when I pan the imageview the button remains constant in the screen. Any idea on how to do this?
Here is what I did:
panImageView = new PanView(this); //Custom Imageview class to handle panning
panImageView.setScaleType(ImageView.ScaleType.CENTER);
//panimg.setAdjustViewBounds(true);
FrameLayout mainRL = (FrameLayout) findViewById(R.id.mainRelativeLayout);
mainRL.addView(panImageView);
ImageWorker imgTask = new ImageWorker(panImageView, is);
imgTask.execute(2000, 2000);
//Start button
Button startButton = new Button(this);
int iRndBtn = R.drawable.round_button;
startButton.setBackgroundResource(iRndBtn);
startButton.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
//layoutParams.setMargins(intialPage.mapStartX, intialPage.mapStartY, 0, 0);
layoutParams.setMargins(200, 200, 0, 0);
startButton.setLayoutParams(layoutParams);
//startButton.
mainRL.addView(startButton,layoutParams);
Add Image view first in any Layout and the add Button in that Layout.
Use a RelativeLayout with the ImageView & button. set the button visibility to gone at start. When you need change the visibility.

How I can increase the touch area for controls (ToggleButton)?

I have a ToggleButton. I want to have more place for click on this button. If I add layout_width, layout_height etc. the image looks bad. I also tried using android:padding but it did not help me.
It is needed for the convenience of users.
Easy solution : If you have image for the button, then create transparent area around it (i.e. for touch area).
The grey area can be transparent to increase the touch area.
Or use TouchDelegate
You can also increase touch area by setting touch delegates Android Developer Training Blog Post
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the parent view
View parentView = findViewById(R.id.parent_layout);
parentView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
#Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
Rect delegateArea = new Rect();
ImageButton myButton = (ImageButton) findViewById(R.id.button);
myButton.setEnabled(true);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"Touch occurred within ImageButton touch region.",
Toast.LENGTH_SHORT).show();
}
});
// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);
// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 100;
delegateArea.bottom += 100;
// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
myButton);
// Sets the TouchDelegate on the parent view, such that touches
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}
Use TouchDelegate for your ToggleButton as ammar26 have commented you.
Or
Try this:
Make one parent layout like LinearLayout or RelativeLayout that cover the ToggleButton. And now put margin to that Parent layout.
Now, on click of that Parent layout do action for the toggle button.
Hope it will help you to increase touch area for your view.
Happy Coding.
Instead of putting the touch event in button put it in the layout containg the only the button..and fix the size of the layout as ur wish
increase the values of android:padding:
<SeekBar android:id="#+id/seek" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:paddingTop="5dp" android:paddingBottom="5dp"
android:progressDrawable="#drawable/green_scrubber_progress_horizontal_holo_ligh‌​t"
android:thumb="#drawable/thumb" />
Take the margin (place of padding) of your Button from its parent layout and then perform opration on your Layout
mLayout.setonTouchListener(View.onTouchListener{
// here your working code
});

How to add view components dynamically from activity

I am attempting to create a user interface dynamically. I have successfully create a view and loaded my background image. I have created two additional small view items to display on the background. My problem is that I have not been able to find any advice/instruction that tells me how to draw the small views. It seems that it should be a trivial exercise and I am guessing it is just finding the correct referencing. Hope someone out there can point me in the right direction.
Here is my Activity:
public class GhostActivity extends Activity implements OnTouchListener
{
private DrawView ghostView;
public Card mCard1, mCard2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// ToDo add your GUI initialization code here
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ghostView = new DrawView(this);
setContentView(ghostView);
//get the window size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Context context = getApplicationContext();
//create view items with initial positions
Point startPoint;
startPoint = new Point();
startPoint.x = 5;
startPoint.y = 3;
mCard1 = new Card(context, 1, R.drawable.bol_geel, startPoint);
startPoint.x = 5;
startPoint.y = 43;
mCard2 = new Card(context, 2, R.drawable.bol_rood, startPoint);
//now display them on the ghostView *****************HOW?
// set the callbacks
ghostView.setOnTouchListener(this);
mCard1.setOnTouchListener(this);
mCard2.setOnTouchListener(this);
}
and here is the View;
public class DrawView extends View
{
Drawable bg ;
public DrawView(Context context) {
super(context);
//setFocusable(true);
Drawable bg = this.getResources().getDrawable(R.drawable.bubbleblue480x800);
setBackgroundDrawable(bg);
}
#Override protected void onDraw(Canvas canvas) {
// canvas.drawColor(0x0000000); //if you want another background color
//draw on the canvas
}
}
edit: I believe my problem is needing to pass a pointer to the ghostView canvas. what makes me think that is if I create the children within ghostView then call their .draw method they appear exactly as I would expect.
#Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0x0000000); //if you want another background color
//draw the cards on the canvas
mCard1.draw(canvas);
mCard2.draw(canvas);
}
so at this point I am wondering how to get a reference pointer to the ghostView canvas.
To be honest I am finding the whole Activity - View relationship confusing.
Edit: I have taken a different approach based on detail in this tutorial
http://www.kellbot.com/2009/06/android-hello-circle/
It uses a FrameLayout and it seems I can achieve my objective.
To add view dynamically to view your class must extends from ViewGroup or LinearLayout class then you will able to call method addView.
Inside your ghost view first add a layout e.g Linear or Relative. Then only you could able to add views inside that layout you cant simply add a view to a xml file.
Or you can create a dynamic layout then only u can add view inside that layout.
RelativeLayout relative= new RelativeLayout(findViewById(R.id.your relativeLayoutID));
relative.addView(child);
child could be anything button textview and widget.

Google Map Marker Clickable Area

I am using this example: https://github.com/galex/android-mapviewballoons
My problem is that the clickable area is wider than the marker itself. For example, my Google Map marker is 25x25 then the clickable area would extend up to 70x70. This is a big problem for overlapping markers.
When I clicked on the tip of that arrow, onTap is activated, even though the tap area is far from the marker.
Please help me. Thanks.
This is the default behaivior of ItemizedOverlay. 25x25 px is generally not an adquate touchable area for most human fingers.
You should override the hitTest() method if you want to modify the way an overlay item hit is tested.
For debugging :
Try using a TouchDelegate for the View, you can specify the Touch rect for a give View
An example showing how to use the TouchDelegate :
public class TouchDelegateSample extends Activity {
Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_delegate_view);
mButton = (Button)findViewById(R.id.delegated_button);
View parent = findViewById(R.id.touch_delegate_root);
// post a runnable to the parent view's message queue so its run
after
// the view is drawn
parent.post(new Runnable() {
#Override
public void run() {
Rect delegateArea = new Rect();
Button delegate = TouchDelegateSample.this.mButton;
delegate.getHitRect(delegateArea);
delegateArea.top -= 200;
TouchDelegate expandedArea = new TouchDelegate(delegateArea,
delegate);
// give the delegate to an ancestor of the view we're
delegating the
// area to
if (View.class.isInstance(delegate.getParent())) {
((View)delegate.getParent()).setTouchDelegate(expandedArea);
}
}
});
}
}
hitTest()
See if a given hit point is within the bounds of an item's marker. Override to modify the way an item is hit tested. The hit point is relative to the marker's bounds. The default implementation just checks to see if the hit point is within the touchable bounds of the marker.

Categories

Resources