Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Calling a class with parameters into another one

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android Studio:

Hi everyone. How's it going?
I'm having trouble with calling the Matrix class into the Main Activity.
Basically, when the lock button is pressed, the matrix class should run/be active.
I'm not sure if I should use the resizable() method or create a new intent.

All other parts of the code appears to be working fine.
Please help!
Thanks For Understanding.


MainActivity class:


import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
{

   Button b_enable, b_lock;

   static final int RESULT_ENABLE = 1;

   DevicePolicyManager devicePolicyManager;

   ComponentName componentName;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       b_enable = (Button) findViewById(R.id.b_enable);

       b_lock = (Button) findViewById(R.id.b_lock);

       devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
       componentName = new ComponentName(MainActivity.this, Controller.class);

       boolean active = devicePolicyManager.isAdminActive(componentName);

       if(active)
       {
           b_enable.setText("DISABLE");
           b_lock.setVisibility(View.VISIBLE);


       }
       else
       {
           b_enable.setText("ENABLE");
           b_lock.setVisibility(View.GONE);

       }

       b_enable.setOnClickListener(new View.OnClickListener()
       {
           @Override
           public void onClick(View view)
           {
               boolean active = devicePolicyManager.isAdminActive(componentName);

               if (active)
               {
                  devicePolicyManager.removeActiveAdmin(componentName);
                   b_enable.setText("ENABLE");
                  b_lock.setVisibility(View.GONE);


               }
               else
               {
                   Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                   intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
                   intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Allowing Device Admin Is Required!");
                   startActivityForResult(intent, RESULT_ENABLE);

               }
           }
       });

       b_lock.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view)
           {
               devicePolicyManager.lockNow();
           }
       });



   }


   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
       switch (requestCode)
       {
           case RESULT_ENABLE:

           if (resultCode == Activity.RESULT_OK)
           {
               b_enable.setText("DISABLE");
               b_lock.setVisibility(View.VISIBLE);
           }
           else
           {
               Toast.makeText(this, "FAILED!", Toast.LENGTH_SHORT).show();
           }
           return;
       }
       super.onActivityResult(requestCode, resultCode, data);
   }
}


Matrix class:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.io.Serializable;
import java.util.Random;

public class Matrix extends View
{
   private static final Random random = new Random();
   private int width, height;
   private Canvas canvas;
   private Bitmap canvasBnp;
   private int fontSize = 15;
   private int columnSize;
   private char[] chars = "!@#$%^&*()_-+=|[]{}<>?/.,;:'~`".toCharArray();
   private int [] txtPostByColumn;
   private Paint paintTxt, paintBg, paintBgBmp, paintInitBg;



   public Matrix (Context context, AttributeSet attrs)
   {
       super(context, attrs);
       paintTxt = new Paint();
       paintTxt.setStyle(Paint.Style.FILL);
       paintTxt.setColor(Color.BLUE);
       paintTxt.setTextSize(15);

       paintBg = new Paint();
       paintBg.setStyle(Paint.Style.FILL);
       paintBg.setColor(Color.BLACK);
       paintBg.setAlpha(5);

       paintBgBmp = new Paint();
       paintBgBmp.setColor(Color.BLACK);

       paintInitBg = new Paint();
       paintInitBg.setColor(Color.BLACK);
       paintInitBg.setAlpha(255);
       paintInitBg.setStyle(Paint.Style.FILL);


   }



   @Override
   protected void onSizeChanged (int w, int h, int oldw, int oldh)
   {

       super.onSizeChanged(w, h, oldw, oldh);
       width = w;
       height = h;

       canvasBnp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
       canvas = new Canvas(canvasBnp);
       canvas.drawRect(0, 0, width, height, paintInitBg);
       columnSize = width/fontSize;


       txtPostByColumn = new int[columnSize + 1];


       for ( int y = 0; y < columnSize; y++)
       {
           txtPostByColumn[y] = random.nextInt(height/2) + 1;

       }

   }



   private void drawText()
   {
       for (int i = 0; i < txtPostByColumn.length; i++)
       {
           canvas.drawText("" + chars[random.nextInt(chars.length)], i * fontSize, txtPostByColumn[i] * fontSize, paintTxt);

           if (txtPostByColumn[i] * fontSize > height && Math.random() > 0.975)
           {
               txtPostByColumn[i] = 0;

           }

           txtPostByColumn[i]++;


        }

   }


   private void drawCanvas()
   {
       canvas.drawRect(0, 0, width, height, paintBg);
       drawText();
   }



   @Override

   protected void onDraw(Canvas canvas)
   {
       super.onDraw(canvas);
       canvas.drawBitmap(canvasBnp, 0, 0, paintBgBmp);
       drawCanvas();
       invalidate();
   }






}
 
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is your first post here    You can use code tags to format the code (next time).

I'm having trouble with calling the Matrix class into the Main Activity.
Basically, when the lock button is pressed, the matrix class should run/be active.





In the above code one can construct (call?) the Matrix class using the Matrix's constructor (note the two lines I have added as comments). But, will it work - I don't know.

What does the Matrix (it is extending the android.view.View) class do? What is its purpose?
 
Saloon Keeper
Posts: 6594
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the Matrix view should replace the current view, then you should create another activity, and Matrix should be part of its layout. In that case, using an intent to start that activity would be a proper way to do it.

I don't understand what you mean by the comment about "resizable()" - how is that a way to display a view?
 
Kyle Boomson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's as Tim Moores said. The Matrix should replace the current view.
The problem is I need guidance.
 
Tim Moores
Saloon Keeper
Posts: 6594
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guidance on what, exactly? You already know how to create an activity with its own layout. And if you search for "Android start activity intent" you'll find examples of how to do that.
 
Kyle Boomson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will it look like? (A sample)
because I've trying but it makes my app crash every time.
 
Tim Moores
Saloon Keeper
Posts: 6594
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution to "my code doesn't work or crashes" is almost never "can you show me another way" but "why does it do that".

If there's a crash, then there's a stack trace in the logcat. Post it here in full, together with the code that causes it (the stack trace has the line number where the crash happens).
 
On top of spaghetti all covered in cheese, there was this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic