I am trying to use both xml view and coded view at the same time in the Android. In this example I am trying to make a button in xml while using coded views to create more dynamic parts in this example moving ball. Ball is moved by tilting the phone. I can either see button which is in xml or the ball which is in java.
I want to know how to put together those two views together so I can move a ball and at the same time see the button or anything else in xml.
Here is my setup Java:
public class MainActivity extends Activity implements SensorEventListener { private SensorManager sensorManager; private Sensor accelerometer; private long lastUpdate; AnimatedView animatedView = null; ShapeDrawable mDrawable = new ShapeDrawable(); public static int x; public static int y; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accelerometer = sensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); lastUpdate = System.currentTimeMillis(); animatedView = new AnimatedView(this); setContentView(animatedView); Display mdisp= getWindowManager().getDefaultDisplay(); Point mdispSize = new Point(); mdisp.getSize(mdispSize); int maxX = mdispSize.x; int maxY = mdispSize.y; x = maxX/2; y = maxY/2; } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.layout.activity_main, menu); return true; } @Override public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub Display mdisp= getWindowManager().getDefaultDisplay(); Point mdispSize = new Point(); mdisp.getSize(mdispSize); int maxX = mdispSize.x; int maxY = mdispSize.y; if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { if(x>maxX){ x=maxX-25; } else if(y>maxY){ y=maxY-25; } else{ x -= (int) event.values[0]; y += (int) event.values[1]; } } } public class AnimatedView extends ImageView { static final int width = 25; static final int height = 25; public AnimatedView(Context context) { super(context); // TODO Auto-generated constructor stub mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(Color.BLUE);//0xffffAC23 mDrawable.setBounds(x, y, x + width, y + height); } public AnimatedView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(Color.BLUE);//0xffffAC23 mDrawable.setBounds(x, y, x + width, y + height); } public AnimatedView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(Color.BLUE);//0xffffAC23 mDrawable.setBounds(x, y, x + width, y + height); } @Override protected void onDraw(Canvas canvas) { mDrawable.setBounds(x, y, x + width, y + height); mDrawable.draw(canvas); invalidate(); } } public void onClick(View v){ Button button = (Button) v; ((Button) v).setText("kliknut"); } xml: (activity_main)
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" tools:context="bepo.proba.MainActivity" > <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout> xml (content_main)
<?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="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="bepo.proba.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <bepo.proba.MainActivity.AnimatedView android:id="@+id/AnimatedView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:clickable="true" android:onClick="onClick" /> </RelativeLayout>
No comments:
Post a Comment