title: How To Print Spaces Or Left And Right Align Content On One Line From Android To Receipt Printer Over Bluetooth?
body: <p><img src="https://i.stack.imgur.com/DO3GY.jpg" alt="Spacing issue on the receipt"></p>
  
  <pre><code>for (i=0;i&lt;items.length;i++){
          String line;
          padding = 50-items[i].length();
          int words = items[i].split("\\s+").length;
          Log.i("info","words: " + words);
          if (words==2) padding += words-1;
          else if (words==3) padding += words-2;
          else if (words==4) padding += words-2;
          //line = String.format("%55s%s", items[i], prices[i]);
          Log.i("info","padding:" + padding);
          //for (int x=0;x&lt;padding;x++) line +="-";
          line = String.format("%1$-" + padding + "s", items[i]);
          tempContent += line + "P" + prices[i] + "\n";
          total +=  Double.parseDouble(prices[i]);
      }
      tempContent += "-----------\n";
      tempContent += String.format("%1$-" + 28 + "s", "Total");
      tempContent += Double.toString(total);
      final String orderContent = tempContent;
  // then sending to the print function
  print.sendData("Table " + tableNumber + "\n\n" + orderContent);
  </code></pre>
  
  <p>As you can see I tried various poor attempts to solve this issue. For the actual Bluetooth code I used this guide - <a href="https://www.androidcode.ninja/android-bluetooth-tutorial/" rel="nofollow noreferrer">https://www.androidcode.ninja/android-bluetooth-tutorial/</a>.</p>
  
  <p>The printing works fine I just have an issue with spacing as described in the title.</p>
  
  <p>Here is my question again - How To Print Spaces Or Left/Right Align Content On One Line From Android To Receipt Printer Over Bluetooth?</p>
  
  <p>It's not a very clear question but to make it clear I want to be able to left align the item then right align the price, or use some method of space generation that will do this for me that could, for example, take into consideration the length of the item string as I have tried to do.</p>
  
  <p>Ignore the Double for prices, I plan to replace it with two Integer values as I know using floats for pricing is inaccurate.</p>
  
  <p>Thanks for your help.</p>