tags:

views:

557

answers:

3

I am trying to learn how to do stuff in Android, and I'm not sure of the best way to build the interface.

I've been working on porting an iPhone app, which uses navigation controllers and table views for looking at the different sections: basically, someone touches a cell in the table, which drills down to another table. when they touch a cell on that table it drills down to a webview that displays the information.

I want to do something similar for the android app, but I don't know how, or if there is a better way native to Android. I've figured out how to use the webview to my purposes, but moving forward and backward in the table tree is unclear.

A: 

The primary way that the Android ui is created is using xml. I'm not exactly sure what you mean when you say drill down but if you want it to change views its as simple as making one set of xml elements visible and another set not. Check out the developer pages for more help.

http://developer.android.com/guide/topics/ui/index.html

Forgot to mention this is also a very good beginner resource. http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts

GEShafer
Drill down is an Apple term for going to another level of a navigation controller (Book->Chapter->Verse: touch which book you want and a list of chapters displays. touch which chapter you want and you go a deeper level. To go from verse to chapter, you just click the back button on the navigation controller and it remembers where you were before.) I'll have to get used to the XML way of doing it. in xCode you have to have a different view controller for each section, and showing/hiding stuff is more difficult.
AndyD273
A: 

As already pointed out XML way of doing layouts is most preferred.

basically, someone touches a cell in the table, which drills down to another table. when they touch a cell on that table it drills down to a webview that displays the information.

From what I understood from the term drills down, this may be wat you need

http://developer.android.com/guide/topics/ui/ui-events.html

From official docs

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.

primalpop
So in the XML way, the way to do it is to catch the touch event when the user selects a Book, reload the table with the chapters, and when the user selects the chapter hide the table and show the webview to display the text, all in one layout?
AndyD273
You could reuse the table layout for books, for displaying chapters as well. WebView should use a different layout for readability.
primalpop
+2  A: 

So on an iphone when you say drill down I guess you mean when a user touches-up on a list row and it slides a new view on from the right, most of the time it has a nav bar at the top to give the user the option to go back?

The way android handles this is simply by starting a new activity. So you would have your 'Books' ListActivity when a listItem is clicked you would define a new intent that starts your 'Chapters' ListActivity and so on. The nav bar at the top of an iphone is not standard UI in android as most people see the dedicated 'back' key as a way of getting back to the previews screen.

This is how you start an intent in case you haven't seen it before:

Intent chaptersIntent = new Intent(this, Chapters.class);
this.startActivity(chaptersIntent);

This article is worth a quick read through as it explains Activities perfectly

http://d.android.com/guide/topics/fundamentals.html

Also have a look at the android version of TableView - ListView:

http://d.android.com/reference/android/widget/ListView.html

and ListActivity:

http://d.android.com/reference/android/app/ListActivity.html


EDIT:: Sample Code I would do it something like this

public class Books extends ListActivity {

private String[] mBooks = new String[]{ "Book1", "Book2", "Book3", "Book4" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayAdapter<String> booksAdapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, 
            android.R.id.text1, 
            mBooks);

    this.setListAdapter(booksAdapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent mViewChaptersIntent = new Intent(this, Chapters.class);
    mViewChaptersIntent.putExtra("BookName", mBooks[position]);
    startActivity(mViewChaptersIntent);
}

}

So you pass through the id of the book as an extra to the Intent then in your Chapters Activity you get that extra in the onCreate method:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle extras = getIntent().getExtras();
    if(extras != null) {
        String bookId = extras.getString("BookName");
    }
}

Finally make sure all new activities are added to your AndroidManifest.xml file:

<activity android:name=".YourClassName"
  android:label="@string/activity_name"
  >
</activity>

Hope that helps

disretrospect
I think this might be what I am looking for.I'm not totally sure I understand it, but it looks promising.I have the onclick working for the table rows, and I've got it switching views using Activity.setContentView, but not sure how to set it back easily (back button exits the program, I think because my project only has one activity).It looks like from your example that I would have to create another class for each section (Books.class, Chapters.class, etc)However, I get this:The constructor Intent(new View.OnClickListener(){}, Class<Informational>) is undefined
AndyD273
Intent informationalIntent = new Intent(this, Informational.class);startActivity(informationalIntent);
AndyD273
Added some sample code above, hopefully that is clearer
disretrospect
Well, looks promising, and I like the look of the list more than the table.I get an error that I don't know what to do with. it happens on startActivity(mViewChaptersIntent); saying Instrumentation.class: "Source not found", "The JAR of this class file belongs to container 'Android 1.6' which does not allow modifications to source attachments on its entries."I don't know how to debug this very good yet...
AndyD273
I really think this is the way to go...Once I get it to actually start a new activity I should be well on my way, thanks a lot.
AndyD273
Ah, OK. So I figured out that I needed to add the Chapters activity in the manifest. Seems to be working now...
AndyD273
Ah yes. Forgot to mention that in my example. Glad you got it sorted. I will update the example to help others who see it
disretrospect