Java tutorial
package com.xalops.spotifystreamer.fragments; /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.os.Bundle; import android.support.annotation.IdRes; import android.support.v4.app.ListFragment; import android.text.InputType; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; import com.xalops.spotifystreamer.R; /** * Static library support version of the framework's {@link android.app.ListFragment}. * Used to write apps that run on platforms prior to Android 3.0. When running * on Android 3.0 or above, this implementation is still used; it does not try * to switch to the framework's implementation. See the framework SDK * documentation for a class overview. */ public class SearchListFragment extends ListFragment { @IdRes static final int INTERNAL_EMPTY_ID = 0x00ff0001; @IdRes static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002; @IdRes static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003; static final int INTERNAL_SEARCH_FIELD_ID = R.id.searchField; EditText mSearchView; private TextView.OnEditorActionListener mOnEditorActionListener; private ListView.OnItemClickListener mOnItemClickListener; public SearchListFragment() { } /** * Provide default implementation to return a simple list view. Subclasses * can override to replace with their own layout. If doing so, the * returned view hierarchy <em>must</em> have a ListView whose id * is {@link android.R.id#list android.R.id.list} and can optionally * have a sibling view id {@link android.R.id#empty android.R.id.empty} * that is to be shown when the list is empty. * * <p>If you are overriding this method with your own custom content, * consider including the standard layout {@link android.R.layout#list_content} * in your layout file, so that you continue to retain all of the standard * behavior of ListFragment. In particular, this is currently the only * way to have the built-in indeterminant progress state be shown. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final Context context = getActivity(); FrameLayout root = new FrameLayout(context); //QUICK Inflate from XML file //inflater.inflate(R.layout.search_list_detail, root); // ------------------------------------------------------------------ LinearLayout pframe = new LinearLayout(context); pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID); pframe.setOrientation(LinearLayout.VERTICAL); pframe.setVisibility(View.GONE); pframe.setGravity(Gravity.CENTER); ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge); pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); // ------------------------------------------------------------------ RelativeLayout rlayout = new RelativeLayout(context); EditText et = new EditText(getActivity()); et.setId(INTERNAL_SEARCH_FIELD_ID); et.setSingleLine(true); et.setInputType(InputType.TYPE_CLASS_TEXT); et.setImeOptions(EditorInfo.IME_ACTION_DONE); RelativeLayout.LayoutParams etRLayoutParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); etRLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); etRLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); FrameLayout lframe = new FrameLayout(context); lframe.setId(INTERNAL_LIST_CONTAINER_ID); RelativeLayout.LayoutParams lframeRLayoutParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); lframeRLayoutParams.addRule(RelativeLayout.BELOW, INTERNAL_SEARCH_FIELD_ID); TextView tv = new TextView(getActivity()); tv.setId(INTERNAL_EMPTY_ID); tv.setGravity(Gravity.CENTER); lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ListView lv = new ListView(getActivity()); lv.setId(android.R.id.list); lv.setDrawSelectorOnTop(false); lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); rlayout.addView(et, etRLayoutParams); rlayout.addView(lframe, lframeRLayoutParams); root.addView(rlayout, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); // ------------------------------------------------------------------ root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return root; } /** * Detach from list view. */ @Override public void onDestroyView() { mSearchView = null; super.onDestroyView(); } /** * Attach to list view once the view hierarchy has been created. */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ensureSearch(); } public EditText getSearchView() { return mSearchView; } public void setSearchView(EditText searchView) { mSearchView = searchView; } public void setOnEditorActionListener(TextView.OnEditorActionListener listener) { mSearchView.setOnEditorActionListener(listener); } public void setOnItemClickListener(ListView.OnItemClickListener listener) { getListView().setOnItemClickListener(listener); } private void ensureSearch() { if (mSearchView != null) { return; } View root = getView(); if (root == null) { throw new IllegalStateException("Content view not yet created"); } if (root instanceof EditText) { mSearchView = (EditText) root; } else { View rawSearchView = root.findViewById(INTERNAL_SEARCH_FIELD_ID); if (!(rawSearchView instanceof EditText)) { if (rawSearchView == null) { throw new RuntimeException("Your content must have a ListView whose id attribute is " + "'android.R.id.searchField'"); } throw new RuntimeException("Content has view with id attribute 'android.R.id.searchField' " + "that is not a ListView class"); } mSearchView = (EditText) rawSearchView; } mSearchView.setOnEditorActionListener(mOnEditorActionListener); } public boolean getListShown() { if (super.getListView().getVisibility() == View.VISIBLE) { return true; } return false; } }