1

I am new to Android, so please dont be very harsh,

I am writing an application that simply displays a triangle on the android emulator screen, I have written a Triangle class and tested the picture in eclipse console! and it displayed perfectly however when I launch it in the emulator my triangle is off!

console android emulator

I have tried using various layouts, but nothing seems to solve the problem. Please help

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

Activity class

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

    TextView text = (TextView) this.findViewById(R.id.text1);

    Triangle triangle = null;
    try {
        triangle = new Triangle (20);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    text.setText(triangle.toString());
   // text.setText("HERE SHOULD BE A TRIANGLE");


}
Jenny
  • 445
  • 2
  • 6
  • 23
  • Is triangle a custom class? May want to post that as well. – Nathaniel D. Waggoner Feb 11 '14 at 21:49
  • I think your problem is that there is a difference between character spacing in the console and the application. If you have to use text aim for the emulartor over the console. This isn't a great solution though because different devices have different pixel density and will look different. – cbrulak Feb 11 '14 at 21:50
  • 3
    OK, here's a hint: take your triangle and copy the text - exactly as it is - into a Word document (or OpenOffice.org). Then change the font to "Courier"; then to "Arial". After a couple of flips you will see the difference between a proportional font and a fixed-width font. – fdreger Feb 11 '14 at 21:50
  • @fdreger better answer than mine. Will the display density of different devices change the widths? – cbrulak Feb 11 '14 at 21:52
  • dear cbrulak and fdreger I completely understand that the density can be a problem, however this has to work somehow, I use exactly one star "*" for a star and exactly one space " " for a space in the triange. – Jenny Feb 11 '14 at 21:53
  • @Jenny: please, read my answer again if you didn't understand it (or best - just try it). The density is NOT the problem. – fdreger Feb 11 '14 at 21:55
  • Nathaniel, triangle is a custom class, user provides the height for the triangle and then the class itself calculated and outputs it in 2D array. the class was tested and there is absolutely no problem whatsoever – Jenny Feb 11 '14 at 21:56
  • @Jenny: The problem is neither your class nor the density. You will see the problem yourself easily if you just trust me and try to see the difference between Courier (being a monospaced font) and Arial (being a proportional font). – fdreger Feb 11 '14 at 22:03
  • fdreger, you r amazing!! – Jenny Feb 11 '14 at 22:04
  • @Jenny: I get that a lot ;-) – fdreger Feb 11 '14 at 22:39

1 Answers1

2

The solutions is that there can be monospaced font and proportional font. In my case it had to be changed to monospased. Solution in code below

 <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="monospace"
    android:text="@string/hello_world" 
    />
Jenny
  • 445
  • 2
  • 6
  • 23