Tutorial details

Android UI: Taking a look at iosched open source app | App Code for Sale | Preview

Android UI: Taking a look at iosched open source app | Android Tutorial

Look at the source code of two open source apps for Android: iosched and ubuntu one for android

Overview PAGE TOP

So, yes, open source is right, great and helps everyone to learn how to do right things (or even to just learn more about some framework). This week I needed to look at the source code of two open source apps for Android: iosched and ubuntu one for android. They’re both pretty great and they deserve attention and take a closer look to their source code. Now, I’m going to talk you about a nice implementation I found on IOSched app. The SinglePane pattern. I really didn’t find it very useful first time, but when you start using it, you find it really cool! It provides you a simple way to define Activities that just have one fragment for its content. Simple, uh?

01  /**
02   * Copyright 2012 Google Inc.
03   *
04   * Licensed under the Apache License, Version 2.0 (the "License");
05   * you may not use this file except in compliance with the License.
06   * You may obtain a copy of the License at
07   *
08   *      http://www.apache.org/licenses/LICENSE-2.0
09   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.android.apps.iosched.ui;
18  
19  import com.google.android.apps.iosched.R;
20  
21  import android.content.Intent;
22  import android.os.Bundle;
23  import android.support.v4.app.Fragment;
24  
25  /**
26   * A {@link BaseActivity} that simply contains a single fragment. The intent used to invoke this
27   * activity is forwarded to the fragment as arguments during fragment instantiation. Derived
28   * activities should only need to implement {@link SimpleSinglePaneActivity#onCreatePane()}.
29   */
30  public abstract class SimpleSinglePaneActivity extends BaseActivity {
31      private Fragment mFragment;
32  
33      @Override
34      protected void onCreate(Bundle savedInstanceState) {
35          super.onCreate(savedInstanceState);
36          setContentView(R.layout.activity_singlepane_empty);
37  
38          if (getIntent().hasExtra(Intent.EXTRA_TITLE)) {
39              setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));
40          }
41  
42          final String customTitle = getIntent().getStringExtra(Intent.EXTRA_TITLE);
43          setTitle(customTitle != null ? customTitle : getTitle());
44  
45          if (savedInstanceState == null) {
46              mFragment = onCreatePane();
47              mFragment.setArguments(intentToFragmentArguments(getIntent()));
48              getSupportFragmentManager().beginTransaction()
49                      .add(R.id.root_container, mFragment, "single_pane")
50                      .commit();
51          } else {
52              mFragment = getSupportFragmentManager().findFragmentByTag("single_pane");
53          }
54      }
55  
56      /**
57       * Called in <code>onCreate</code> when the fragment constituting this activity is needed.
58       * The returned fragment's arguments will be set to the intent used to invoke this activity.
59       */
60      protected abstract Fragment onCreatePane();
61  
62      public Fragment getFragment() {
63          return mFragment;
64      }
65  }

Although this time, it extends from BaseActivity, you could extend from Activity, SherlockFragmentActivity, RoboSherlockFragmentActivity or whatever fits your project. Just take into account that It must be able to use fragments.

As you can see it’s pretty simple, you just have to extend this class and override onCreatePane() on your child activity.

01  public class ExampleOneFragmentActivity extends SinglePaneActivity {
02  
03      @Override
04      protected void onCreate(Bundle savedInstanceState) {
05          super.onCreate(savedInstanceState);
06      }
07  
08      @Override
09      protected Fragment onCreatePane() {
10          return new ExampleOneFragment();
11      }
12  
13  }

and this would be the fragment:

01  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
02      android:layout_width="match_parent"
03      android:layout_height="match_parent">
04  
05      <LinearLayout android:orientation="vertical"
06          android:layout_width="match_parent"
07          android:layout_height="wrap_content"
08          android:paddingLeft="@dimen/content_padding_normal"
09          android:paddingRight="@dimen/content_padding_normal"
10          android:paddingTop="@dimen/content_padding_normal"
11          android:paddingBottom="@dimen/content_padding_normal">
12  
13          <TextView android:id="@+id/vendor_name"
14              android:layout_width="match_parent"
15              android:layout_height="wrap_content"
16              style="@style/TextHeader" />
17  
18          <TextView android:id="@+id/vendor_url"
19              android:layout_width="match_parent"
20              android:layout_height="wrap_content"
21              android:autoLink="web"
22              android:paddingBottom="@dimen/element_spacing_normal"
23              style="@style/TextBody" />
24  
25          <com.google.android.apps.iosched.ui.widget.BezelImageView android:id="@+id/vendor_logo"
26              android:scaleType="centerCrop"
27              android:layout_width="@dimen/vendor_image_size"
28              android:layout_height="@dimen/vendor_image_size"
29              android:layout_marginTop="@dimen/element_spacing_normal"
30              android:src="@drawable/sandbox_logo_empty"/>
31  
32          <TextView android:id="@+id/vendor_desc"
33              android:layout_width="match_parent"
34              android:layout_height="wrap_content"
35              android:layout_marginTop="@dimen/element_spacing_normal"
36              android:paddingTop="@dimen/element_spacing_normal"
37              style="@style/TextBody" />
38      </LinearLayout>
39  </ScrollView>

*That one is picked from iosched

Don’t forget that this simple pattern also provides passing extra to set a new title! Or you can use this two methods from BaseActivity to pass arguments from activities to fragments.

01  /**
02       * Converts an intent into a {@link Bundle} suitable for use as fragment arguments.
03       */
04      public static Bundle intentToFragmentArguments(Intent intent) {
05          Bundle arguments = new Bundle();
06          if (intent == null) {
07              return arguments;
08          }
09  
10          final Uri data = intent.getData();
11          if (data != null) {
12              arguments.putParcelable("_uri", data);
13          }
14  
15          final Bundle extras = intent.getExtras();
16          if (extras != null) {
17              arguments.putAll(intent.getExtras());
18          }
19  
20          return arguments;
21      }
22  
23      /**
24       * Converts a fragment arguments bundle into an intent.
25       */
26      public static Intent fragmentArgumentsToIntent(Bundle arguments) {
27          Intent intent = new Intent();
28          if (arguments == null) {
29              return intent;
30          }
31  
32          final Uri data = arguments.getParcelable("_uri");
33          if (data != null) {
34              intent.setData(data);
35          }
36  
37          intent.putExtras(arguments);
38          intent.removeExtra("_uri");
39          return intent;
40      }

Reference PAGE TOP

http://www.javacodegeeks.com/2013/01/android-ui-taking-a-look-at-iosched-open-source-app.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.