Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

9.22.2015

Simplify Android Development Using manifoldJS With Crosswalk

With version 0.3.0 of manifoldJS, you can now choose to build your Android apps with Crosswalk instead of the traditional Android webview. It's quite simple to implement as well.

If you're not familiar, manifoldJS is a new open-source framework that can take a website and create an app for Windows, iOS, Android, Chrome, and Firefox, simplifying the creation of hosted apps across platforms. It debuted at the Microsoft Build 2015 conference in April. manifoldJS runs as a command line tool through Node.js, or you can use the web-based tool.

manifoldJS

In this tutorial, I'll show you the simple steps to get it up and running so that you can try it yourself. First, make sure you have manifoldJS installed and running.

Install Node.js from nodejs.org.

Open your favorite command prompt (Terminal on Mac or Linux) and type:

Now, you simply add the Crosswalk flag to your launch parameters, and watch what happens next:

And…BOOM! You’ve just built a hosted web app with Crosswalk.

Shiftr app on Android phones

With Crosswalk, you can be confident that the newest HTML5 features, such as WebGL, IndexedDB, Web Sockets, and CSS3, are there for your app. For example, here's the output from a WebGL application that uses the default webview and was generated by manifoldJS with the following command:

We are sorry but your browser does not seem to support WebGL

And here is the same application with Crosswalk enabled. It was generated after adding the -c (or –-crosswalk) flag to the previous command:

Or:

Your browser supports WebGL

With Crosswalk, you can be sure that all your users get the intended experience.

Crosswalk is a web runtime environment engineered by the Crosswalk Project. Crosswalk has taken the open-source Chromium and Blink engines, and compiled them into a modern, up-to-date runtime environment. You can think of Crosswalk as a powerful webview. In fact, when the Crosswalk flag is set, we use it in place of the traditional Android webview.

Crosswalk support brings two main advantages. First, it’s an "updated" web runtime environment. That might not be much of an advantage for Android users on a recent version of the Android OS, but for users on older versions of the OS, it's an immense improvement. The Crosswalk webview will give you access to all the latest HTML5 features and performance gains over the traditional webview.

Secondly, Crosswalk provides a consistent runtime environment. With all the different versions of Android in use today, you have that many different versions of the Android webview, so you’re forced to write to the lowest common denominator. Using Crosswalk removes that hindrance. Additionally, the runtime only changes when you update it in your app, not with the OS. We know that many enterprise users rely on this type of consistency for their applications.

I can only think of one reason why you wouldn’t want to use Crosswalk: application size. The average .apk file (an application file for Android) that we produce is just a few megabytes. Adding Crosswalk to the app adds an additional 20MB, close to 60MB once installed on the device. You need to decide whether the resource cost is worth it.

Bundling the runtime with the application is the simplest approach for distribution purposes, but Crosswalk applications can also share a single runtime library (in "shared mode") to lighten the load. A package which enables shared mode is part of the Crosswalk for Android distribution. However, you would have to distribute this shared runtime package yourself. Visit the Crosswalk wiki for more details.

Keep in mind that the nature of a hosted web app is that you make your app updates on your webserver. So in most cases, the cost of the added package size will be felt with the initial download, not with every update like a regular native app.

Advertisement

We’re excited to be supporting the Crosswalk web runtime environment. It’s filling a gap in the Android system that makes development simpler and more reliable. Give it a try with your next manifoldJS app and see what you think. For more information on Crosswalk, visit the Crosswalk Project website. To start building store apps from your website, go to the manifoldJS website and get started.

This article is part of the web development series from Microsoft tech evangelists on practical JavaScript learning, open-source projects, and interoperability best practices, including Microsoft Edge browser and the new EdgeHTML rendering engine

We encourage you to test across browsers and devices including Microsoft Edge—the default browser for Windows 10—with free tools on dev.modern.IE:

In-depth tech learning on Microsoft Edge and the Web Platform from our engineers and evangelists:

More free cross-platform tools and resources for the Web Platform:

1.06.2012

Android ListAdapter example


If you want to use a ListView, you will have to supply it with a ListAdapter to allow it to display any content. A few simple implementations of that adapter are already available in the SDK:
These implementations are perfect for displaying very simple lists. But if your list is just a little more complicated than that, you will need to write your own custom ListAdapter implementation. In most cases it's useful to subclass ArrayAdapter which already takes care of managing a list of objects. Now you only have to tell it how to render each object in the list. Do this by overriding thegetView(int, View, ViewGroup) method of the ArrayAdapter class.
Example of a ListView containing Youtube search results in the form of images and text
The images need to be on-the-fly downloaded from the internet. Let's create a class which represents items in the list:

  1. public class ImageAndText {
  2.     private String imageUrl;
  3.     private String text;
  4.     public ImageAndText(String imageUrl, String text) {
  5.         this.imageUrl = imageUrl;
  6.         this.text = text;
  7.     }
  8.     public String getImageUrl() {
  9.         return imageUrl;
  10.     }
  11.     public String getText() {
  12.         return text;
  13.     }
  14. }

Now, let's create an implementation of a ListAdapter that is able to display a list of these ImageAndTexts.

  1. public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
  2.     public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
  3.         super(activity, 0, imageAndTexts);
  4.     }
  5.     @Override
  6.     public View getView(int position, View convertView, ViewGroup parent) {
  7.         Activity activity = (Activity) getContext();
  8.         LayoutInflater inflater = activity.getLayoutInflater();
  9.         // Inflate the views from XML
  10.         View rowView = inflater.inflate(R.layout.image_and_text_row, null);
  11.         ImageAndText imageAndText = getItem(position);
  12.         // Load the image and set it on the ImageView
  13.         ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
  14.         imageView.setImageDrawable(loadImageFromUrl(imageAndText.getImageUrl()));
  15.         // Set the text on the TextView
  16.         TextView textView = (TextView) rowView.findViewById(R.id.text);
  17.         textView.setText(imageAndText.getText());
  18.         return rowView;
  19.     }
  20.     public static Drawable loadImageFromUrl(String url) {
  21.         InputStream inputStream;
  22.         try {
  23.             inputStream = new URL(url).openStream();
  24.         } catch (IOException e) {
  25.             throw new RuntimeException(e);
  26.         }
  27.         return Drawable.createFromStream(inputStream, "src");
  28.     }
  29. }

The views are inflated from an XML file called "image_and_text_row.xml":

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.              android:orientation="horizontal"
  4.              android:layout_width="fill_parent"
  5.              android:layout_height="wrap_content">
  6.         <ImageView android:id="@+id/image"
  7.                   android:layout_width="wrap_content"
  8.                   android:layout_height="wrap_content"
  9.                   android:src="@drawable/default_image"/>
  10.         <TextView android:id="@+id/text"
  11.                  android:layout_width="wrap_content"
  12.                  android:layout_height="wrap_content"/>
  13. </LinearLayout>

This ListAdapter implementation renders the Image And Texts in the ListView like you would expect. The only thing is that this only works for a very small list which doesn't require scrolling to see all items. If the list of ImageAndTexts gets bigger you will notice that scrolling isn't as smooth as it should be (in fact, it's far off!).

1.03.2012

Using ViewFlipper in Android App

Suppose you want to display a news bar in your activity. this news bar displays a single news item at a time then flips and shows next item and so on, then your choice would be Android's ViewFlipper.

ViewFlipper inherits from frame layout, so it displays a single view at a time.
consider this layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="@string/hello"/>
  11.     <Button
  12.    android:layout_width="wrap_content"
  13.    android:layout_height="wrap_content"
  14.    android:text="Flip"
  15.    android:id="@+id/btn"
  16.    android:onClick="ClickHandler"/>
  17.     <ViewFlipper
  18.    android:layout_width="fill_parent"
  19.    android:layout_height="fill_parent"
  20.    android:id="@+id/flip">
  21.     <TextView
  22.    android:layout_width="fill_parent"
  23.    android:layout_height="wrap_content"
  24.    android:text="Item1"/>
  25.     <TextView
  26.    android:layout_width="fill_parent"
  27.    android:layout_height="wrap_content"
  28.    android:text="Item2"/>
  29.     <TextView
  30.    android:layout_width="fill_parent"
  31.    android:layout_height="wrap_content"
  32.    android:text="Item3"/>
  33.     </ViewFlipper>
  34. </LinearLayout>


Just a ViewFlipper container that contains three text views

Now we want to flip the views when the button is clicked.

  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6. }
  7. public void ClickHandler(View v)
  8. {
  9.     flip.showNext();
  10. }


If we want to flip in reverese direction we could use flip.showPrevious() instead. If you want to flip to a specific view do the following: flip.setDisplayedChild(indexOfView) replace indexOfView with the integer representing the index of the view you want to switch to.

We can add animations to the child views when they appear or disappear:
  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6.     //when a view is displayed
  7.     flip.setInAnimation(this,android.R.anim.fade_in);
  8.     //when a view disappears
  9.     flip.setOutAnimation(this, android.R.anim.fade_out);
  10. }

We can also set the ViewFlipper to flip views automatically when the button is clicked:

  1. public void ClickHandler(View v)
  2. {
  3.     //specify flipping interval
  4.     flip.setFlipInterval(1000);
  5.     flip.startFlipping();
  6. }

We can stop the flipping by calling flip.stopFlipping(); method or we can set the flipper to flip autommatically when the activity starts by changing our code.

  1. public void onCreate(Bundle savedInstanceState) {
  2.     super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.     btn=(Button)findViewById(R.id.btn);
  5.     flip=(ViewFlipper)findViewById(R.id.flip);
  6.     flip.setInAnimation(this,android.R.anim.fade_in);
  7.     flip.setOutAnimation(this, android.R.anim.fade_out);
  8.     flip.setFlipInterval(1000);
  9.     flip.setAutoStart(true);    
  10. }

More about this control you can read by this url: http://developer.android.com/reference/android/widget/ViewFlipper.html