Categories
discuss

Layout problem with Android: rotated TextView

I’m building an Android app on which I have to represent data in a Table-like format.
So I’m using TableLayout. The problem is that I have to draw a rotated string like in the following raw example:

alt text

How can I create my layout to be able to show the ‘2011’ rotated?

Answer

Extend the TextView class and override the onDraw method.

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate(90, xPivot, yPivot);
     super.onDraw(canvas);
     canvas.restore();

} 
Categories
discuss

How do I check in Firefox and Chrome if a plugin is installed

I’m using an embedded plugin in my web app to enable scanning (Dynamsoft’s webscanning plugin). I’m serving an xpi to firefox users and a crx to chrome users. When viewing the page without the plugin installed, Firefox will try to download the plugin and the user will need to click “manual install” to install it. Chrome will make no attempt to donwload the plugin (this is by design, according to http://code.google.com/p/chromium/issues/detail?id=15745)

I would like to use javascript to detect wether the plugin is installed or not (preferrably before trying to load it through the embed tag). If no plugin is installed, I want to provide an explanation and a link to the correct plugin to the user. Both browsers install their respective plugins without any problem if the user clicks at a download link instead of just trying to load through the embed tag.

So, does anybody know a way detect wether a plugin is installed using javascript?

Answer

Have you had a look at window.navigator.plugins?

..and the equivalent reference for webkit plugins

The Apple reference for the WebKit DOM appears to have disappeared. navigator.plugins does work for me in Safari & Chrome. See a jsfiddle here: http://jsfiddle.net/2EaKD/

Categories
discuss

Using enums in Java across multiple classes

I have the following class:

public class Card 
{
    public enum Suit 
    {
        SPACES, HEARTS, DIAMONDS, CLUBS
    };

    public Card(Suit nsuit, int nrank)
    {
        suit = nsuit;
        rank = nrank;
    }

    private Suit suit;
    private int rank;
}

I want to instantiate it in another class, but that class doesn’t understand the Suit enum. Where should I put the enum to make it publicly visible?

Answer

Put Suit in its own class:

Suit.java:

public enum Suit 
{
    SPACES, HEARTS, DIAMONDS, CLUBS
}
Categories
discuss

Android memory types (RAM v Internal Memory)

On a separate thread I demonstrated my ignorance of memory types by asking about the best way to copy a file into “internal memory” and was advised that this was not a good idea and that it would be better to read the file into “RAM”. I am now trying to understand how these two types of memory are related and how they can be used.

I understand that “internal memory is perceived to be the flash that is used to store APKs, ROM images, etc.”

The specification for my HTC Hero says that there is “ROM: 512MB, RAM: 288MB” with no indication of how the RAM is subdivided.

On the phone the “Settings”->”SD & phone storage” page simply refers to “SD card” and “Internal phone storage” and for the latter just shows one amount for “Available space”.

The Android Reference for Data Storage mentions “Internal Storage”. This is a place where “you can save files”. The page also mentions SQLite database files which I think can be stored in the “Internal storage”. However, the page doesn’t seem to offer any explanation of the sub-division of RAM between Internal Storage and another (presumably more dynamic) type.

Is there some kind of dynamic division of the total RAM (288MB on the Hero) into two sub-divisions which are usable in different ways? If so, where in the Android documentation is this explained?

Answer

I understand that “internal memory is perceived to be the flash that is used to store APKs, ROM images, etc.”

The specification for my HTC Hero says that there is “ROM: 512MB, RAM: 288MB” with no indication of how the RAM is subdivided.

The RAM is not “subdivided” on any sort of permanent basis. RAM is used by running processes on an as-needed basis. The biggest thing for SDK apps to note is that there is an upper bound as to how much RAM a process can consume — on your Hero, it should be 16MB.

On the phone the “Settings”->”SD & phone storage” page simply refers to “SD card” and “Internal phone storage” and for the latter just shows one amount for “Available space”.

The “Internal phone storage” refers to the portion of on-board flash space set aside for APK files, databases, preferences, and other local files.

The page also mentions SQLite database files which I think can be stored in the “Internal storage”.

Correct.

However, the page doesn’t seem to offer any explanation of the sub-division of RAM between Internal Storage and another (presumably more dynamic) type.

RAM has nothing to do with internal storage.

Is there some kind of dynamic division of the total RAM (288MB on the Hero) into two sub-divisions which are usable in different ways?

No.


Let’s step back a pace.

RAM is RAM is RAM. RAM on your phone is not significantly different than RAM on your desktop, your notebook, your netbook, etc. This construct has remained relatively stable in its use for the past few decades, particularly once we escaped from the mainframe. Running programs (OS and end-user apps) consume RAM, but only while they are running. And RAM gets wiped on a reboot, on phones as much as on your desktop.

Now, your average desktop, notebook, or netbook also has a hard drive for more permanent storage than RAM offers. There is no absolute term for the equivalent in Android — I tend to use “on-board flash storage” for it, others call it “internal storage”, though as you will see, the latter term is fraught with confusion. Just like a hard drive, on-board flash storage is used for the OS (a.k.a., firmware in Android), end-user programs (a.k.a., APK files in Android), and end-user data (a.k.a., databases, shared preferences, and other files stored where getFilesDir() is located).

The on-board flash storage is divided into a few partitions. If you’re a Windows user, partitions is basically saying “your hard drive is broken into C:, E:, and F: drives”. In Android, one partition is dedicated to firmware, and another partition (the “data partition”) is dedicated to the end-user programs and data. (NOTE: the preceding description is not entirely accurate, but the discrepancies are well beyond the scope of this answer).

So, with all that in mind:

  • Your 512MB of ROM is the on-board flash storage, hearkening back to the old “flash ROM” term
  • Your “Internal phone storage” in settings it the available space in the data partition for end-user apps and data (one of the reasons why I don’t use this term to refer to the on-board flash storage as a whole)
  • Your “SD card” is more generically referred to in Android as external storage, which on many devices is some form of SD card, though it could actually be just another partition of the on-board flash storage designated as serving in the role of external storage
Categories
discuss

Is there any way to add a MouseListener to a Graphic object?

Is there any way to add a MouseListener to a Graphic object.
I have this simple GUI that draw an oval.
What I want is handling the event when the user clicks on the oval

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class Gui2 extends JFrame {
 JFrame frame = new JFrame();
 MyDrawPanel drawpanel = new MyDrawPanel();

 public static void main(String[] args) {
  Gui2 gui = new Gui2();
  gui.go();
 }

 public void go() {

  frame.getContentPane().add(drawpanel);
  // frame.addMouseListener(this);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 300);
  frame.setVisible(true);

 }

}

class MyDrawPanel extends JComponent implements MouseListener {

 public void paintComponent(Graphics g) {

  int red = (int) (Math.random() * 255);
  int green = (int) (Math.random() * 255);
  int blue = (int) (Math.random() * 255);
  Color startrandomColor = new Color(red, green, blue);

  red = (int) (Math.random() * 255);
  green = (int) (Math.random() * 255);
  blue = (int) (Math.random() * 255);
  Color endrandomColor = new Color(red, green, blue);

  Graphics2D g2d = (Graphics2D) g;
  this.addMouseListener(this);
  GradientPaint gradient = new GradientPaint(70, 70, startrandomColor,
    150, 150, endrandomColor);

  g2d.setPaint(gradient);
  g2d.fillOval(70, 70, 100, 100);

 }

 @Override
 public void mouseClicked(MouseEvent e) {
  if ((e.getButton() == 1)
    && (e.getX() >= 70 && e.getX() <= 170 && e.getY() >= 70 && e
      .getY() <= 170)) {
   this.repaint();
   // JOptionPane.showMessageDialog(null,e.getX()+ "n" + e.getY());
  }

 }

 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub

 }

}

This Works Except it fires when the click is within a virtual box around the oval.
Could anyone help me to have it fire when the click is EXACTLY on the oval.
Thanks in advance.

Answer

The simplest way I can think of to do this is to avoid fillOval and use the geometry in the java.awt.geom package. So you can declare an Ellipse since it appears that the oval that you are drawing is statically sized.

 class MyDrawPanel extends JComponent implements MouseListener {
       Ellipse2D oval = new Ellipse2D.Double(70, 70, 100, 100);

       ....

 }

Then in the paintComponent you use the fill(Shape) method to draw this.

  public void paintComponent(Graphics g) {
      ....
      Graphics2D g2d = (Graphics2D) g;
      ....
      g2d.fill(oval);

  }

Then in the mouse event you can detect if the click is in the oval or not like this:

 public void mouseClicked(MouseEvent e) {
   if ((e.getButton() == 1) && oval.contains(e.getX(), e.getY()) ) {
      repaint();
    // JOptionPane.showMessageDialog(null,e.getX()+ "n" + e.getY());
   }
 }
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..