Java tutorial
package ch.timjahn.learningapp; import android.app.ActionBar; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.support.v4.view.MotionEventCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; public class ls extends AppCompatActivity { // WONT GET USED, 1 DAY FOR THE CAT private float mLastTouchX; private float mLastTouchY; private float mPosX; private float mPosY; private Circle circle; private ActionBar ab; private int abH; private boolean touch = false; private int mActivePointerId; private ScaleGestureDetector mScaleDetector; // Antworttypen und die Antwort auf die erste Frage private final String TYPE_1 = "Links"; private final String TYPE_2 = "Mitte-Links"; private final String TYPE_3 = "Mitte"; private final String TYPE_4 = "Mitte-Rechts"; private final String TYPE_5 = "Rechts"; private String answer1 = TYPE_5; private String answer2 = TYPE_3; private int activeQ = 1; private String activeA = TYPE_3; @Override protected void onCreate(Bundle savedInstanceState) { //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); super.onCreate(savedInstanceState); //this.circle = new Circle(this, 100, 100, 100); //setContentView(circle); //circle.setCord(100, 100); setContentView(R.layout.activity_ls); SeekBar sb = (SeekBar) findViewById(R.id.seekBar); TextView tv = (TextView) findViewById(R.id.progress); findViewById(R.id.lsZurck).setEnabled(false); sb.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { TextView tv = (TextView) findViewById(R.id.progress); @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { hideSolution(); if (isBetween(progress, 1, 20)) { tv.setText(TYPE_1 + " (" + progress + "|" + seekBar.getMax() + ")"); activeA = TYPE_1; } if (isBetween(progress, 21, 40)) { tv.setText(TYPE_2 + " (" + progress + "|" + seekBar.getMax() + ")"); activeA = TYPE_2; } if (isBetween(progress, 41, 60)) { tv.setText(TYPE_3 + " (" + progress + "|" + seekBar.getMax() + ")"); activeA = TYPE_3; } if (isBetween(progress, 61, 80)) { tv.setText(TYPE_4 + " (" + progress + "|" + seekBar.getMax() + ")"); activeA = TYPE_4; } if (isBetween(progress, 81,100)) { tv.setText(TYPE_5 + " (" + progress + "|" + seekBar.getMax() + ")"); activeA = TYPE_5; } } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} } ); } @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_ls, 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(); return super.onOptionsItemSelected(item); } public void next(View view) { ((Button) findViewById(R.id.lsWeiter)).setEnabled(false); ((Button) findViewById(R.id.lsZurck)).setEnabled(true); ((TextView) findViewById(R.id.fragels)).setText("Wo ungefhr befindet sich die GLP im Parteienkompass?"); hideSolution(); ((SeekBar) findViewById(R.id.seekBar)).setProgress(50); activeQ = 2; } public void back(View view) { ((Button) findViewById(R.id.lsWeiter)).setEnabled(true); ((Button) findViewById(R.id.lsZurck)).setEnabled(false); ((TextView) findViewById(R.id.fragels)).setText("Wo ungefhr befindet sich die SVP im Parteienkompass?"); hideSolution(); ((SeekBar) findViewById(R.id.seekBar)).setProgress(50); activeQ = 1; } public void control(View view) { int progress = ((SeekBar) findViewById(R.id.seekBar)).getProgress(); TextView tv = (TextView) findViewById(R.id.solution); tv.setVisibility(View.VISIBLE); switch (activeQ) { case 1: if (activeA.equals(answer1)) { tv.setText("Richtig (~90|100)."); tv.setTextColor(Color.parseColor("#00CC00")); } else { tv.setText("Falsch."); tv.setTextColor(Color.RED); } break; case 2: if (activeA.equals(answer2)) { tv.setText("Richtig (~45|100)"); tv.setTextColor(Color.parseColor("#00CC00")); } else { tv.setText("Falsch."); tv.setTextColor(Color.RED); } break; } } public static boolean isBetween(int x, int lower, int upper) { return lower <= x && x <= upper; } public void hideSolution() { TextView tv2 = (TextView) findViewById(R.id.solution); tv2.setVisibility(View.GONE); } /* @Override public boolean onTouchEvent(MotionEvent ev) throws IllegalArgumentException { // Because Screenshots throw an Exception try { TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { abH = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_DOWN: { mActivePointerId = MotionEventCompat.getPointerId(ev, 0); // Get Pointer Position final int pointerIndex = MotionEventCompat.getActionIndex(ev); final float mLastTouchX = MotionEventCompat.getX(ev, pointerIndex); final float mLastTouchY = MotionEventCompat.getY(ev, pointerIndex); // Get Circle Position float posX = circle.getXPos(); float posY = circle.getYPos(); // Set Booleans for Collisiondetection abH = actionbarHeight boolean a = posX + 150 >= mLastTouchX; // Rechs drin? boolean b = posX - 150 <= mLastTouchX; // Links drin? boolean c = posY + 150 + (abH) >= mLastTouchY; // Unten drin? boolean d = posY - 150 + (abH) <= mLastTouchY; // Oben drin? *//* Log.e("POS",a + " TouchX " + mLastTouchX); Log.e("POS",b + " PosX " + posX); Log.e("POS",c + " TouchY " + mLastTouchY); Log.e("POS",d + " PosY " + posY); *//* // Collision? if ( a && b && c && d) { this.touch = true; // Log.e("POS", "DONE"); } else { this.touch = false; // Log.e("POS", "FALSCH"); } break; } case MotionEvent.ACTION_UP: { mActivePointerId = MotionEvent.INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = MotionEvent.INVALID_POINTER_ID; break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); // Calculate the distance moved // final float dx = x - mLastTouchX; // final float dy = y - mLastTouchY; // mPosX += dx; // mPosY += dy; //postInvalidate(); // Remember this touch position for the next move event //mLastTouchX = x; //mLastTouchY = y; if (y > abH && this.touch) { circle.setCord(x,y - 200); } break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly. final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } break; } } return true;} catch (IllegalArgumentException iAE) { return true; } // Code from Android Developer // http://creativecommons.org/licenses/by/2.5/legalcode } */ /* protected void onPause () { super.onPause(); circle.terminate(); Log.e("APP", "Pause"); } protected void onStop() { super.onStop(); circle.terminate(); Log.e("APP", "Stop"); } protected void onRestart() { super.onRestart(); circle.restart(); circle.setCord(mPosX, mPosY); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); Log.e("APP", "Restart"); } protected void onResume() { super.onResume(); // circle.restart(); // circle.setCord(mPosX, mPosY); // Log.e("APP", "Resume"); } */ }