Search code examples
javaandroidandroid-fragmentsandroid-intentandroid-implicit-intent

Android multi-fragment layout in landscape mode


Although I got my code to work, I now have no idea what it's actually doing. My app is an RSS reader, and the main content is in a Fragment containing a ListView with NewsStory objects. When a list item is clicked, it opens an Intent with the website linked from the RSS.

Now the problem is, I don't understand the Intent here, it's not the way I've ever used Intents before.

Also, I have to make it so that when I change the orientation, the original profile Fragment takes up the left half of the screen and the linked webpage takes up the right half of the screen. I've tinkered around with it, to no avail. I did a bit of research on orientation changes, but I feel like doing things with Fragments always changes how everything works. Anyway, here's the Fragment code. Any thoughts would be greatly appreciated.

public class HeadlineFragment extends Fragment {

    EditText input;
    Button search;
    ListView headlines;
    NewsDataSource ds;


    public HeadlineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_headline,container,false);

        input = (EditText)v.findViewById(R.id.txtInput);
        search = (Button)v.findViewById(R.id.btnSearch);
        headlines = (ListView)v.findViewById(R.id.listView);

        try {
            ds = new NewsDataSource();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        headlines.setAdapter(new NewsDataSourceAdapter(this.getActivity(), ds));

        headlines.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent,View view, int position, long id)
            {
                String url = NewsDataSource.stories[position].getLink();
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        return v;
    }
    /*
    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);


    }
    */
}

Solution

  • Firstly, regarding that Intent, its an implicit Intent. When you set the action as ACTION_VIEW and add a URI / URL as an extra, the OS gets the message that you want to open an app that can navigate to that URI / URL.

    Secondly, for showing a two-pane layout in landscape mode, you'll have to show that RSS content in a Fragment instead of an Activity as you are currently doing, and you'll have to display those Fragments side-by-side in an Activity in landscape mode. See the Retrieving a List of Contacts example for a really good explanation of how to display a multi-pane master detail layout in portrait mode.

    References:

    1. Intents and Intent Filters.

    2. Planning for Multiple Touchscreen Sizes.