I want to put Circle class into my layout. I tried it but app got stopped on creating. I think the problem is in xml code in the first line which is under. Kindly guide me.
<com.package.Circle
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/circle"
android:layout_width="300dp"
android:layout_height="300dp" />
XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.zohaib.animatedcircle.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.package.Circle
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/circle"
android:layout_width="300dp"
android:layout_height="300dp" />
</RelativeLayout>
Circle.java
package com.example.zohaib.animatedcircle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class Circle extends View {
private static final int START_ANGLE_POINT = 90;
private final Paint paint;
private final RectF rect;
private float angle;
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
final int strokeWidth = 40;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
//Circle color
paint.setColor(Color.RED);
//size 200x200 example
rect = new RectF(strokeWidth, strokeWidth, 200 + strokeWidth, 200 + strokeWidth);
//Initial Angle (optional, it can be zero)
angle = 120;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
}
MainActivity.java
package com.example.zohaib.animatedcircle;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Circle circle = (Circle) findViewById(R.id.circle);
CircleAngleAnimation animation = new CircleAngleAnimation(circle, 240);
animation.setDuration(1000);
circle.startAnimation(animation);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The XML element name needs to be the fully qualified class name of your custom View class. In this case, it would be com.example.zohaib.animatedcircle.Circle, instead of com.package.Circle.
<com.example.zohaib.animatedcircle.Circle
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/circle"
android:layout_width="300dp"
android:layout_height="300dp" />
Related
I'm very new to Android development and trying to implement my own custom view which will allow me to use draw operations on a canvas. My custom view should draw a rectangle onto the screen in the onDraw() function, however it doesn't seem to work, no rectangle is drawn.
The very basic main activity that I'm trying to use the custom view with:
package com.example.testcanvas;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML files for the activity, which I have tried to add the custom view to:
fragment main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.testcanvas.MainActivity$PlaceholderFragment" >
</RelativeLayout>
activity main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testcanvas.MainActivity"
tools:ignore="MergeRootFrame" >
<com.example.testcanvas.CustomView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/custom_view" />
</RelativeLayout>
and finally, my custom view class:
package com.example.testcanvas;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class CustomView extends SurfaceView implements SurfaceHolder.Callback {
public CustomView (Context context) {
super(context);
}
public CustomView (Context context, AttributeSet attrs) {
super(context);
}
#Override
public void onDraw (Canvas canvas) {
int dCenter = 40; //Distance from center in px, NOT in dp
int centerX = (int)(getWidth()/2);
int centerY = (int)(getHeight()/2);
Paint paint = new Paint();
paint.setColor(Color.BLUE); //The square will draw in the color blue now
Rect rect = new Rect(centerX-dCenter, centerY-dCenter, centerX+dCenter, centerY+dCenter);
canvas.drawRect(rect, paint);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
#Override
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
postInvalidate();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
Could anybody possibly point out where I've gone wrong? Thanks.
When I use child view then the buttons which are in the root view are disabled. I could not click on them.
I am trying to develop application in which input is made by connecting buttons with lines. If user wants to enter
some thing then he have to touch buttons by connecting them with lines. It is very similar to android pattern
unlock. But in our application in place of dots of grid, numbers will be shown.
activity_demo_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DemoMainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="222.08203dp"
android:layout_marginTop="270.53426dp"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="162.08203dp"
android:layout_marginTop="314.12678dp"
android:text="2" />
<Button
android:id="#+id/button3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="87.917960dp"
android:layout_marginTop="314.12678dp"
android:text="3" />
<Button
android:id="#+id/button4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="27.917960dp"
android:layout_marginTop="270.53423dp"
android:text="4" />
</RelativeLayout>
DemoMainActivity.java :
package com.example.demo;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class DemoMainActivity extends Activity {
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_main);
rl = (RelativeLayout)findViewById(R.id.rty);
Button b1=(Button)findViewById(R.id.button2);
final NewPattern b = new NewPattern(this);
b1.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View arg0) {
try{
rl.getRootView();
rl.addView(b);
}
catch(Exception ex){}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.demo_main, menu);
return true;
}
}
NewPattern.java:
package com.example.demo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
//import com.example.newpattern.PatternMainActivity;
import android.widget.RelativeLayout;
public class NewPattern extends View
{
private Paint m_paint;
private static Bitmap m_bitmap;
private Canvas m_canvas;
private Path m_path;
private Paint m_bitmapPaint;
private DisplayMetrics m_metrics;
private int m_color = 0xFFFF0000;
static boolean m_pathDrawn = false;
private float m_X, m_Y;
private static final float TOUCH_TOLERANCE = 4;
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rty);
#SuppressLint("NewApi")
private void touchman(float g, float h){
final float btn1x = b1.getX();
//float btn2x= b1.getY();
if(g==btn1x){
rl.getRootView();
}
}
public NewPattern(Context context, AttributeSet attrs) {
super(context, attrs);
//Your code
// btn1.setText("fdsf");
}
public NewPattern(Context p_c)
{
super(p_c);
try{
m_paint = new Paint();
m_paint.setAntiAlias(true);
m_paint.setDither(true);
m_paint.setColor(m_color);
/*new ColorPickerDialog(p_c, new OnColorChangedListener()
{
public void colorChanged(int p_color)
{
m_paint.setColor(p_color);
m_color = p_color;
}
}, 0xFFFF0000).show();*/
m_paint.setStyle(Paint.Style.STROKE);
m_paint.setStrokeJoin(Paint.Join.ROUND);
m_paint.setStrokeCap(Paint.Cap.ROUND);
m_paint.setStrokeWidth(12);
m_metrics = p_c.getResources().getDisplayMetrics();
m_bitmap =
Bitmap.createBitmap(m_metrics.widthPixels, m_metrics.heightPixels,
Bitmap.Config.ARGB_8888);
m_canvas = new Canvas(m_bitmap);
m_path = new Path();
m_bitmapPaint = new Paint(Paint.DITHER_FLAG);
}
catch(Exception ex){}}
public void onerase()
{
m_canvas = null;
}
#Override
protected void onSizeChanged(int p_w, int p_h, int p_oldw, int p_oldh)
{
super.onSizeChanged(p_w, p_h, p_oldw, p_oldh);
}
#Override
protected void onDraw(Canvas p_canvas)
{try{
p_canvas.drawColor(0x00000000);
p_canvas.drawBitmap(m_bitmap, 0, 0, m_bitmapPaint);
p_canvas.drawPath(m_path, m_paint);}
catch(Exception ex){}
}
#SuppressLint("NewApi")
private void touch_start(float p_x, float p_y)
{//try{
m_path.reset();
m_path.moveTo(p_x, p_y);
m_X = p_x;
m_Y = p_y;
//}
//catch(Exception ex){}
}
private void touch_move(float p_x, float p_y)
{try{
float m_dx = Math.abs(p_x - m_X);
float m_dy = Math.abs(p_y - m_Y);
if (m_dx >= TOUCH_TOLERANCE || m_dy >= TOUCH_TOLERANCE)
{
m_path.quadTo(m_X, m_Y, (p_x + m_X) / 2, (p_y + m_Y) / 2);
m_X = p_x;
m_Y = p_y;
m_pathDrawn = true;
}}
catch(Exception ex){}
}
private void touch_up()
{try{
m_path.lineTo(m_X, m_Y);
// commit the path to our offscreen
m_canvas.drawPath(m_path, m_paint);
// kill this so we don't double draw
m_path.reset();
}
catch(Exception ex){}
}
#SuppressLint("NewApi")
#Override
public boolean onTouchEvent(MotionEvent p_event)
{try{
float m_x = p_event.getX();
float m_y = p_event.getY();
switch (p_event.getAction())
{
case MotionEvent.ACTION_DOWN:
touch_start(m_x, m_y);
//touchman(m_x, m_y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(m_x, m_y);
// touchman(m_x, m_y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
//touchman(m_x, m_y);
invalidate();
break;
}
return true;
}
catch(Exception ex){}
return true;
}}
I am following this tutorial: http://danielnadeau.blogspot.ca/2012/01/android-canvas-beginners-tutorial.html
When I look at things in the "Graphical Layout" tab in Eclipse, I see the expected result (a red square in the center of the display), but when I actually run the app, I just get a large black rectangle, and I can't seem to figure out why. I've searched some posts, but have not found an answer that seems to apply to my situation.
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#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;
}
}
activity_mail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.hintonworks.cantstop.CustomView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/my_view" />
</RelativeLayout>
CustomView.java
package com.hintonworks.cantstop;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CustomView extends SurfaceView implements SurfaceHolder.Callback {
public CustomView (Context context) {
super(context);
}
public CustomView (Context context, AttributeSet attrs) {
super(context);
}
#Override
public void onDraw (Canvas canvas) {
int dCenter = 40; //Distance from center in px, NOT in dp
int centerX = (int)(getWidth()/2);
int centerY = (int)(getHeight()/2);
Paint paint = new Paint();
paint.setColor(Color.RED); //The square will draw in the color blue now
Rect rect = new Rect(centerX-dCenter, centerY-dCenter, centerX+dCenter, centerY+dCenter);
canvas.drawRect(rect, paint);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
#Override
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
postInvalidate();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
Thanks for any insight. This is my stab at using the canvas, until now I have been using drawable resources.
I have two ImageViews one of them located on top of another and both inside a frameLayout. As far as I know whatever we add in frameLayout will sit on the left top part of screen and we can set their position by setting a padding for them, So I've set paddings for both of ImageViews.
upperImage = (ImageView) findViewById(R.id.imageView1);
upperImage.setPadding(250,250, 250, 0);
lowerImage = (ImageView) findViewById(R.id.imageView2);
lowerImage.setPadding(250, 361, 250, 0); //250+Height of Image
I want to rotate my ImageViews and I'm almost done with that. the only thing I still haven't done is the location of pivot.
I want to set the pivot point on the center bottom of my ImageViews. I've tried many numbers but I didn't get the answer and didn't even find out how that method works. I mean I don't know what's the number it gets as an argument. Is that pixels or what?(I call the method for animations in onCreate method so I can't use getWidth() and getHeight() methods so I enter the numbers that these methods return myself.)
I have even tried different places to set pivot point in java and even XML layout. But that didn't make any changes.
All I want to know is how to set the pivot point at center bottom of my ImageView using setPivot methods?(They don't seem to be working methods) Can it be the problem with setting the point in onCreate method? If yes how can I make it work in onCreate?
Remember to put the actual graphic in the res folders for your bitmap and change the name in the constructor or whatever when it loads the bitmap
PivotView.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PivotView extends View{
public Bitmap arm;
public Bitmap hand;
public int armrotation = 0;
public int handrotation = 0;
public PivotView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
arm = BitmapFactory.decodeResource(getResources(),R.drawable.arm );
hand = BitmapFactory.decodeResource(getResources(),R.drawable.hand );
}
#Override
protected void onDraw (Canvas canvas){
canvas.rotate((float)armrotation, 0, (float)canvas.getHeight());
canvas.drawBitmap(arm, new Rect(0,0,arm.getWidth(),arm.getHeight()), new Rect(0,
canvas.getHeight()*1/3,canvas.getWidth()*1/5,canvas.getHeight()), null);
Matrix matrix = new Matrix();
matrix.setRectToRect(new RectF(0,0,(float)hand.getWidth(),(float)hand.getHeight()), new RectF(0,
(float)canvas.getHeight()*1/4,(float)canvas.getWidth()*1/5,(float)canvas.getHeight()*4/10), Matrix.ScaleToFit.FILL);
matrix.preRotate((float)handrotation,(float)hand.getWidth()/2,(float)hand.getHeight());
canvas.drawBitmap(hand, matrix, null);
}
public void rotatArm(int rotate){
Log.d("arm", String.valueOf(rotate));
armrotation = rotate;
}
public void rotateHand(int rotate){
Log.d("hand", String.valueOf(rotate));
handrotation = rotate;
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
public SeekBar hand;
public SeekBar arm;
public PivotView robot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hand = (SeekBar)findViewById(R.id.seekhand);
hand.setMax(360);
arm = (SeekBar)findViewById(R.id.seekarm);
arm.setMax(90);
robot = (PivotView)findViewById(R.id.pivot);
hand.setOnSeekBarChangeListener(this);
arm.setOnSeekBarChangeListener(this);
}
#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;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
switch(seekBar.getId()){
case R.id.seekarm:
rotatethearm(progress);
//robot.invalidate();
break;
case R.id.seekhand:
rotatethehand(progress);
//robot.invalidate();
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void rotatethearm(int progress){
robot.rotatArm(progress);
robot.invalidate();
}
public void rotatethehand(int progress){
robot.rotateHand(progress);
robot.invalidate();
}
}
Make your layout like so with custom view, remember to use your package name in place where it says com.example.pivotview in the layout for the custom view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.pivotview.PivotView
android:id="#+id/pivot"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.pivotview.PivotView>
<SeekBar
android:id="#+id/seekarm"
android:layout_alignParentBottom = "true"
android:layout_width = "match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="#+id/seekhand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/seekarm"
/>
</RelativeLayout>
I am trying to draw multiple circles from an array and it is throwing a "force close" when I try to run the code. Any ideas here?
package com.adam.PlaySound;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ButtonPress extends Activity {
/** Called when the activity is first created. */
int numPlayers = 3;
boolean toggle = true;
LinearLayout parent;
Button button;
TextView text;
TextView text2;
DisplayMetrics dm = new DisplayMetrics();
MediaPlayer mediaPlayer[] = new MediaPlayer[numPlayers];
//this array is a placeholder for the loops in a cluster
//essentially this array will be populated through a database
//based on which cluster someone is within
//and the region arrays will be populated by the same database
String soundFiles[] = {"drums_1.mp3", "lead.mp3", "strings.mp3"};
public LoopRegion region[] = new LoopRegion[numPlayers];
float regionX[] = {20, 80, 150};
float regionY[] = {20, 200, 350};
int regionR[] = {50, 80, 100};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setContentView(R.id.view1);
parent = (LinearLayout) findViewById(R.id.parent);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.text);
text2 = (TextView) findViewById(R.id.text2);
parent.setOnTouchListener(Motion);
button.setOnClickListener(on_off);
for (int i = 0; i < numPlayers; i++){
String path = "http://soundclusters.adamlaskowitz.com/uploads/" + soundFiles[i];
mediaPlayer[i] = new MediaPlayer();
//mediaPlayer[i] = MediaPlayer.create(ButtonPress.this, R.raw.drums_2);
mediaPlayer[i] = MediaPlayer.create(ButtonPress.this, Uri.parse(path));
mediaPlayer[i].setLooping(true);
}
for (int i = 0; i < numPlayers; i++){
region[i] = new LoopRegion(this,regionX[i],regionY[i],regionR[i]);
parent.addView(region[i]);
}
getWindowManager().getDefaultDisplay().getMetrics(dm);
}
This is the LoopRegion class.
package com.adam.PlaySound;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
/*This class will create a circle region for
*each loop within a cluster */
public class LoopRegion extends View {
private float x;
private float y;
private int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public LoopRegion(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
public void setCoordinate(float mx, float my){
this.x = mx;
this.y = my;
//this.r = 300;
}
}
And this is the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/parent"
>
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Not Playing"
/>
<TextView android:id="#+id/text2"
android:layout_height="wrap_content"
android:text="Location" android:layout_width="wrap_content"
/>
<Button android:text="Sound On/Off"
android:id="#+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<!-- <View android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</View>-->
</LinearLayout>
Thanks in advance!
My guess is that your LoopRegion views have layout parameters set to FILL_PARENT and the first one pushes the others off the screen. It would help to see the code for how a LoopRegion is created, and perhaps the XML for the parent LinearLayout.