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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Android Studio (Starting a new activity/class with the click of a button)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
"Hey guys, I don't know what happened to my last post but my question is: How do I get the "Lock button" to start the "Matrix class"?"

Please let me know if I have to add something to the Android Manifest class.


[Main Activity code]


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 v)
           {
               boolean active = devicePolicyManager.isAdminActive(componentName);

               if (active)
               {

                   b_enable.setVisibility(View.GONE);
                   devicePolicyManager.lockNow();

                   launchMatrix();

               }
               else

                   {


               }

           }


       });



   }


   public void launchMatrix()
   {
       Intent intent = new Intent(this, Matrix.class);

       startActivity(intent);

   }



   @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);
   }


}


[/Main Activity code]









[Main Activity xml code]

<?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"
   tools:context=".MainActivity">




   <Button
       android:id="@+id/b_enable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="21dp"
       android:text="ENABLE" />

   <Button
       android:id="@+id/b_lock"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="144dp"
       android:text="LOCK"

       />




</RelativeLayout>



[/Main Activity xml code]











[Matrix xml code]

<?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"
   tools:context=".MainActivity">

   <avantgardestudios.noexcuses.Matrix
       android:layout_width="match_parent"
       android:layout_height="match_parent" />


</RelativeLayout>


[/Matrix xml code]








[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.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 = 40;
   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(fontSize);

       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();
   }


[/Matrix class]



 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
How are you trying to debug the code? Try adding some print statements to show the execution flow to see if the listener is called and if the launch method is executed.

What is shown in the logcat when the code is executed?

One possible problem is that the Matrix class is not an Activity.
To show the Matix class, try using setContentView

Note: Posted code is easier to read if it is wrapped in code tags.  Select the code and use the Code button to add code tags.  For example:
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What wrong with your code ?
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Kyle Boomson wrote:"Hey guys, I don't know what happened to my last post but my question is: How do I get the "Lock button" to start the "Matrix class"?"

Your previous posting had one or more issues as such you were sent a Purple Mooseage to with how you could fix your posting.
The site should have sent an email to you regarding the Purple Mooseage and you can see your Purple Mooseages by looking at the top of the page and clicking on "My Purple Mooseages".
 
Saloon Keeper
Posts: 7592
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The previous discussion is at https://coderanch.com/t/694734/mobile/Calling-class-parameters. You were asked to add proper code tags around the code to make it easier to read, but since you didn't react to that the thread was closed after a few days. (Code that's hard to read tends to attract fewer responses, so we try to avoid it.) I've reopened it now, but the code tag issue is still unresolved. Let's close this identical discussion and continue in the original topic.
    Bookmark Topic Watch Topic
  • New Topic