0

This question is actually a 3 Part Question. Please suggest the valid solution for my problem.

Application Overview

I'm creating an application having a splash screen and a ListView. On click of the launcher icon, first the splash screen is shown and then after 3-4 seconds later, the splash screen calls the ListView activity and user is left with ListView items on the phone screen. On click of each item, respective textual content will be visible to the user. After reading the content user will click the "Back" button and will be returned to hte ListView items section again. If he/she wants to read any other item content, will be taken to that page on the click of that item. If not willing, then will click "Back" button on phone and application will close.

Part 1 - What I actually want to do

I want to show different textual content (not normal text but styled (bold/italized/containing images and all) on the click of different listView items. I can create the html pages and store them in my resource folder locally but don't know how to call those pages while calling onItemClickListener. I'm not sure if this is a valid approach to do this sort of work or not. Please advice. It's not working so far

Part 2 - What I'm doing instead now

Since I was not able to call the textual content so I wrote the content on some text editor, did all the fancy work on the editor and when the content was ready, I took the screenshot and then call that image instead while calling onItemClickListener. It's working fine and I'm able to see the image on click of my ListView item.

Part 3 - The problem I'm facing

When the image is shown to the user, on click of "Back" button, the application is closed instead of going back to the ListView Section. Apparently the image opening on click of the listview item is in the same activity as is the ListView itself. What I want is that user should be able to go back to the ListView section after he/she finishes reading the textual content.

Below is the code of the ListView Activity: public class MainActivity extends Activity {

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

    ListView listView = (ListView) findViewById(R.id.menulist);
    String[] values = new String[] { "1. First Article Here", "2. Second Article Here", "3. Third Article Here"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.custom_listview, R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id){
            //
            if(0==position)
            {
                setContentView(R.layout.image0); 

             }
            else if (1==position)
            {

            }
        }        

    });

} *The code works fine upto setContentView(R.layout.image0); * But after this I want the application to return back to the ListView section on click of "Back" button but it closes instead. Please guide.

Harry
  • 3
  • 2
  • 9

5 Answers5

0

May be it solves by,

Using layout.image0 in another activity and call by intent when click on list item.

By use this, When you click back button you view your previous activity

Shalini
  • 1,733
  • 4
  • 17
  • 31
  • Hi Shalini, I'm using SplashScreen in another activity. SplashScreen activity is my launcher activity. From there I'm calling MainActivity by intent. MainActivity is marked as Default Activity. – Harry Jul 24 '12 at 08:19
0

When you press the back button, the current activity get closed and you get back to the previous activity on the stack. Given that you are not changing activity by pressing the list element, but only changing the layout, pressing the back button empties the stack and closes your app.

The cleanest way would be to launch the splash screen in another activity.

A couple of other ways to solve it are: - add a stack of fragments

or (the dirtiest way), override the onBackPressed(), intercept it when your app is showing the splash screen and change back the layout to the listview.

I would prefer the other approaches.

fedepaol
  • 6,834
  • 3
  • 27
  • 34
0

If you use setContentView() method, the back button functionality will not work, since no new Activity was created, from which you can go back to the listView. I'd rather take the clicked item's useful information, put it in an Intent (link), and call startActivity() method, to start a new activity, to show content to the user. Then, if the user clicks the back button, the "details" activity will disappear, and the user again sees the listView.

overbet13
  • 1,654
  • 1
  • 20
  • 36
0

You are using setContentView so its overriding the current view with ur new View in OnItem Click...Rather add a View Group to ur activity like a Relative layout...And when u click your Itme add a new layout to ur existing layout and on back remove the layout.

Or u can Use DialogView to dynamically add a view ..see below:

    Dialog dialog = new Dialog(this, android.R.style.Theme_Dialog);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xff000000);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(Your VIew);
    dialog.show();

Or at last you can start Another Intent using your current Activity..

sheetal
  • 3,014
  • 2
  • 31
  • 45
  • The dismiss listener is to handle the event when u want to come out of the listView click view...implement OnDismissListener to ur activity.. – sheetal Jul 24 '12 at 08:24
0

Part 1 and 2

You can create complex and styled layouts using the XML files.

If you really want to use HTML, I advice you to use a WebView. You can store the plain html-string somewhere in your resources and use Webview.loadDataWithBaseUrl() to show it in your WebView.

You can make your choice depending on your functionality. If you have alot of different events to be handled (like clicking on a View), I would opt for the XML files. Because you can assign different listeners to seperate Views. When your functionality doesn't go further as it does now, only clicking a whole view. You can use a WebView as well.

Part 3 The problem is that you are abusing the setContentView-method. setContentView tells Activity which XML file it has to use. In your case, when the Activity is created, it will use R.layout.activity_main.

When you click your item, you tell the Activity.. Don't use the R.layout.activity_main anymore, but use R.layout.image0. The Activity will do this and your image will be shown.

This is not a good way to change layout though and will mess up your working flow. The reason that, when the image is shown, your application closes on Back is that there is only 1 Activity started. (Your stack contains only 1 Activity) You just switched the layout, but no new Activity is started.

Solution

Create 2 Activities:

  1. Create a main Activity showing the your list and set the layout with setContentView(R.layout.activity_main). When your click-event is triggered, you will start the second Activity. Intent i = new Intent(...). You can also add some arguments to the Intent and access them in your second Activity to know which item was clicked and which image to show.
  2. A detail Activity showing your content or in your case an Image. setContentView(R.layout.image0).

Solution 2

Create a popup to show your content. You can use a Dialog for that.

Community
  • 1
  • 1
DroidBender
  • 7,762
  • 4
  • 28
  • 37