Home > Blockchain >  open MainActivity using another activity java class
open MainActivity using another activity java class

Time:09-02

I have made a simple canvas. but I want to make a new activity java class and open this activity using that activity java class. I have not used any layout files to make this class. I want to know how can i make new activity before starting this and I won't open this java activity class using that. I have some idea using intent we can open them. but I haven't any clear idea.

public class MainActivity extends AppCompatActivity {

    ArrayList<Double> values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

    public class MyView extends View{
        public MyView(Context context){
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            
            //Build Canvas
            super.onDraw(canvas);
            //Give the size
            int x = getWidth();
            int y = getHeight();
            int radius = 500;

            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            canvas.drawPaint(paint);

            // Add Values to ArrayList
            values = new ArrayList<>();
            values.add(20.1);
            values.add(21.9);
            values.add(21.3);
            values.add(21.4);
            values.add(20.5);
            values.add(21.6);
            values.add(21.7);

            //Generate Mean
            double sum = 0.0;
            for (double n : values){
                sum = n;
            }

            try {
                double mean = sum/values.size();

                //Set Colors on Values
                if (mean <=50.0){
                    paint.setColor(Color.parseColor("#001a1a"));
                    canvas.drawCircle(x/2,y/2,radius,paint);
                    //Status show Toast
                    Toast toast = Toast.makeText(MainActivity.this, "Wrong Input", Toast.LENGTH_LONG);
                    toast.getView().setBackgroundColor(Color.parseColor("#001a1a"));
                    toast.show();
                    Toast toast2 = Toast.makeText(MainActivity.this, "Please check about Values", Toast.LENGTH_LONG);
                    toast2.getView().setBackgroundColor(Color.parseColor("#001a1a"));
                    toast2.show();
                }
                else if (mean <=80.0){
                    paint.setColor(Color.parseColor("#87CEEB"));
                    canvas.drawCircle(x/2,y/2,radius,paint);
                    //Status show Toast
                    Toast toast = Toast.makeText(MainActivity.this, "Hart rate is Low", Toast.LENGTH_LONG);
                    toast.getView().setBackgroundColor(Color.parseColor("#87CEEB"));
                    toast.show();
                }
                else if(mean <=100.0){
                    paint.setColor(Color.parseColor("#B2BEB5"));
                    canvas.drawCircle(x/2,y/2,radius,paint);
                    //Status show Toast
                    Toast toast = Toast.makeText(MainActivity.this, "Hart rate is Normal", Toast.LENGTH_LONG);
                    toast.getView().setBackgroundColor(Color.parseColor("#B2BEB5"));
                    toast.show();
                }
                else if(mean<=140.0){
                    paint.setColor(Color.parseColor("#CD5C5C"));
                    canvas.drawCircle(x/2,y/2,radius,paint);
                    //Status show Toast
                    Toast toast = Toast.makeText(MainActivity.this, "Hart rate is Very High", Toast.LENGTH_LONG);
                    toast.getView().setBackgroundColor(Color.parseColor("#CD5C5C"));
                    toast.show();

                }else{
                    paint.setColor(Color.parseColor("#001a1a"));
                    canvas.drawCircle(x/2,y/2,radius,paint);

                    Toast toast = Toast.makeText(MainActivity.this, "Wrong Input", Toast.LENGTH_LONG);
                    toast.getView().setBackgroundColor(Color.parseColor("#001a1a"));
                    toast.show();
                    Toast toast2 = Toast.makeText(MainActivity.this, "Please check about Values", Toast.LENGTH_LONG);
                    toast2.getView().setBackgroundColor(Color.parseColor("#001a1a"));
                    toast2.show();

                }
            }catch (Exception e){
                Toast.makeText(MainActivity.this, "Please check Values", Toast.LENGTH_SHORT).show();
            }

        }
    }
}

CodePudding user response:

You can create MainActivity2, and in AndroidManifest, change

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

from MainActivity to MainActivity2.

And in MainActivity2, using Intent to navigate to MainActivity

  • Related