Tag Archives: Android

Open external Android app from within Android app

You can open any external Android application from within your Android application by pressing a button (or any other event). All you need to know is the namespace of the application you want to launch.

For this functionality use following code snippet:

public class MainActivity extends Activity {

private Button openAppButton;

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

openAppButton = (Button) findViewById(R.id.openApp);
openAppButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent i = new Intent();
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("launching.app.namespace");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}

});
}

}

In this code replace „launching.app.namespace“ with the namespace of the app you want to launch. If you don’t know the namespace of the app you can go to Google Play and search for the app. Inside the URL the namespace is mentioned as the „id“. For example, Google Maps has following URL:

 https://play.google.com/store/apps/details?id=com.google.android.apps.maps

As you can see, the value of id is „com.google.android.apps.maps“ – this is the app’s namespace.

 

Wrapping a MGWT Webapp with PhoneGap into a Native Android App

Here is an example how to make a simple MGWT Webapp and wrap it with PhoneGap into a native Android application. With MGWT to you can create fantastic mobile web applications with the look and feel of native mobile Android or iOS apps.

  • Install Eclipse IDE
  • Install GWT plugin into Eclipse
  • Download the MGWT jar file
  • Download the PhoneGap (Cordova) jar file
  • Create a new GWT project („HelloWorld“) with Eclipse (the official „Getting started“ documentation for GWT can be found here).
  • Add following jar files into the /libs folder of the newly created GWT project:
    mgwt.jar
    gwtphonegap.jar
  • Add these lines of code into the HelloWorld.gwt.xml file:
    <!-- Other module inherits -->
    <inherits name="com.googlecode.mgwt.MGWT"/>
    <inherits name='com.googlecode.gwtphonegap.PhoneGap' /><set-property name="user.agent" value="safari" />
    <set-configuration-property name="mgwt.css" value="pretty" />
    
  • Develop your GWT app as you wish using the MGWT library (the MGWT „Getting started“ documentation to be found here). Here is a simple and easy example:
    public class HelloWorld implements EntryPoint {public void onModuleLoad() {
    // set viewport and other settings for mobile
    MGWT.applySettings(MGWTSettings.getAppSetting());// build animation helper and attach it
    AnimationHelper animationHelper = new AnimationHelper();
    RootPanel.get().add(animationHelper);// build some UI
    LayoutPanel layoutPanel = new LayoutPanel();
    Button button = new Button("Hello World! Woohoo!");
    layoutPanel.add(button);// animate
    animationHelper.goTo(layoutPanel, Animation.SLIDE);}}
    
  • Create new native Android project with Eclipse. With this we are going to wrap the MGWT project into a native app.
  • Add the PhoneGap (or Cordova) jar file into the newly created Android project’s /libs folder.
  • Create subfolders /assets/www in that project. Make sure to add the PhoneGap.js (JavaScript) file into the /assets/www folder!
  • Create a simple index.html file in the /assets/www folder. Here a simple example of the html file:
    <head>
    <title>Cordova</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
    </head>
    <body>
    <h1>Hello World</h1>
    <h2>PhoneGap works!</h2>
    </body>
    </html>
    
  • Add following permissions to the Android project’s manifest file:
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    
  • Change the MainActivity class of the Android project that it looks like this:
    public class MainActivity extends DroidGap {@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    super.loadUrl("file:///android_asset/www/index.html");
    }@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }}
    
  • Build the Android project and see if the index.html file is being displayed after launching the app.
  • Compile (Google ->GWT Compile) the MGWT project. The compiled code can be found inside of the /war folder of the MGWT project.
  • Copy all the content of the /war folder of the MGWT project into the /assets/www folder in the Android project.
  • In the Android project in the MainActivity class change this line of code:
    super.loadUrl("file:///android_asset/www/index.html");
    

    to this

    super.loadUrl("file:///android_asset/www/HelloWorld.html");
    

    (or however the main html file of the MGWT project is named)

  • Run the Android project again. Now you should see the content of the MGWT app displayed acting like a native Android application. If that looks okay you can export the project to an APK file! Done!

Unity 3D: Debugging with the Unity logfile when testing an APK file on an actual device

Normally, if you test your Unity game on your Android phone you are able to see the Unity debug logfile in the Android logcat with Eclipse.

Debug.Log("info");
Debug.LogWarning("warning");
Debug.LogError("error");

However, in case this is not working you can output the game’s logfile to a flat file. Do as follows:

  • Connect to the device using ADB.
  • Use the logcat command:
    adb logcat -d > mylogfile.txt

That will create a text file (mylogfile.txt) which should include any statements you logged (like „information“ from Debug.Log).

Android debugging: Force app to be installed on SD card

If you are working on a big application which uses a lot of memory you might get the error „No space left on device“ when trying to debug. With a Android Debug Bridge (ADB) command you can force the application to be installed on the SD card directly.

You have following options to chose when it comes to where the app shall be installed by default:

The setInstallLocation command changes the default install location
0 [auto]: Let system decide the best location
1 [internal]: Install on internal device storage
2 [external]: Install on external media

Do as described:

  1. Connect your phone to your computer via USB. Make sure it is in debug mode.
  2. Open Windows CMD Command (cmd.exe)
  3. Type following command if you want the default location to be your SD card:
    adb shell pm setInstallLocation 2
That’s about it. From now on when testing your app it will automatically be installed on the SD card instead of the internal memory.

How to convert an Activity class to use with FragmentPagerAdapter

This article shows how you have to alter your Activity classes to make them usable as Fragments for a horizontal slider.

You can find a tutorial how to create a smooth horizontal slider here:
Simple tutorial for a “smooth horizontal view slider” with Android

If you already have Activity classes and you want to combine them into one horizontal slider (FragmentPagerAdapter) you have to make some alterations in order to make it work. Here is how:

Let’s say you have following Activity classes in your project and want to combine them with a FragmentPagerAdapter:

  • MainArticle
  • AuthorInformation
  • CommentSection

First of all you change extends Activity to extends Fragment in those classes.

Before:

public class MainArticle extends Activity {...}

After:

public class MainArticle extends Fragment {...}

As with any Activity class there is the mandatory onCreate(…) method. Change this method to public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {…}.

Before:

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

After:

private LinearLayout ll;
private FragmentActivity fa;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.outer_layout, container, false);
...

return ll;
}

The difference now is that we have to use the FragmentActivity object „fa“ and get the Activity from its parent class. Otherwise we won’t be able to access the Layout XML’s elements. You now also have to return a View object which should be your most outer element in the Layout XML file. In this example’s case it would be the LinearLayout called R.layout.outer_layout.

If you used an Intent object to pass variables to that view you will have to change the line of code from:

intent = getIntent();

to

fa = super.getActivity();
intent = fa.getIntent();

All the elements you are accessing from the XML Layout file can still be accessed. However, you have to alter the lines of code from

TextView myTextView = (TextView) findViewById(R.id.myTextView);

to

TextView myTextView = (TextView) ll.findViewById(R.id.myTextView);

Also if you have any Toast messages in that Activity you have to change the line of code to the following:

Toast.makeText(fa, message, Toast.LENGTH_LONG).show();

If you have any links which forward the user to another Activity you have to change the source parameter to „fa“. This will look like this:

Intent explicitIntent = new Intent(fa, AuthorInformation.class);
startActivity(explicitIntent);

Once you’ve done all that you can add those newly converted Fragment classes to the FragmentPagerAdapter class. If you have any questions feel free to use the comment section below.

Simple tutorial for a „smooth horizontal view slider“ with Android

Screenshot of a horizontal sliderSurely you have seen some Android apps which have this cool feature to scroll through different Views smoothly with a horizontal swipe. If you have some Views and you want them to be „slide-able“ you can do this quite easily actually.

Because this feature is not in Android 2.x you will need to use the compatibility pack from Android. It is a library with classes from Android 3. It is called „android-support-v13.jar“. You can download this JAR file from developer.android.com or just download the sample project from this site which also contains this JAR file.

Create a new project and add android-support-v13.jar to its build path. First we need a FragmentPagerAdapter. Create a class according to the code below:

MyPagerAdapter.java

public class MyPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;

/**
* @param fm
* @param fragments
*/
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

/*
* (non-Javadoc)
*
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}

Next you have to create a class which handles the different Fragments (i.e. the separate „Views“). This class is going to be your Activity class.

ViewPagerFragmentActivity.java

public class ViewPagerFragmentActivity extends FragmentActivity {

private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_layout);

// initialize the pager
this.initialisePaging();
}

/**
* Initialize the fragments to be paged
*/
private void initialisePaging() {

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
}

viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/awesomepager">

</android.support.v4.view.ViewPager>

Eclipse will mark some errors. This is because the Fragment classes have not created yet. But this is what we are going to do right now. Create as many classes as you wish to have in your slider. In my example we use the classes Fragment0, Fragment1 and Fragment2. If you want to use any other classes make sure to include them in the code in the ViewPagerFragmentActivity.java class.

Fragment0.java, Fragment1.java, Fragment2.java

public class Fragment0 extends Fragment {
/**
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return (LinearLayout) inflater.inflate(R.layout.fragment0_layout, container, false);
}
}

As for the XML Layouts you can design them however you like.

Lastly add the classes ViewPagerFragmentActivity, Fragment0, Fragment1 and Fragment2 to the manifest file.

That’s all. The app should work already allowing you to slide through the Fragment classes smoothly with a horizontal swipe.

If you’re lazy you can as well just download the whole demo project here:
HorizontalSmoothSliderExample

If you already have a FragmentPagerAdapter and want to use your already existing Activity classes als Fragments you can use my other tutorial here:
How to convert an Activity class to use with FragmentPagerAdapter