I am looking to create a mini-drawer menu like in google example:
I have tried to create a layout that always stays there on ParentLeft, and the menu to overflow it when it opens, but it does not look natural. Does anyone know how do I do it?
Update
I have tried another way. To listen to menu sliding, and to catch when it is closed enough, then I can set menu size, and keep icons visible, but text to be gone.
#Override
public void onDrawerSlide(float v, int i) {
Log.d("onDrawerSlide", "v=" + v + " i=" + i);
if (i<previewsI && decreasingCount > 3) {
// check if menu is closed enough
if (i <100 && i > 50) {
// change menu size, and force menu to keep opened
mDrawer.setMenuSize(Utils.dpToPx(70, getApplicationContext()));
mDrawer.openMenu();
// TODO: hide menu items title, and let only icons to be visible
}
}
else if (i < previewsI)
// make sure the menu is closing
decreasingCount++;
previewsI = i;
}
It works, but not as smooth as I wish. Now I'd have to mess with smoothly opening it again.
Anyway, I don't think this is an elegant solution. I am sure there must be a better one out there.
I know this is a very old question, and i'm not sure if you are willing to use a library. But the MaterialDrawer library would offer a MiniDrawer implementation including the transformation to a normal drawer.
As shown in the screenshot the MiniDrawer also supports badges, and it also comes with an AccountSwitcher. Also with everything else.
The MiniDrawer uses the Crossfader library which allows the crossfade effect. The sample application of the Crossfader library also shows how to implement this with the MaterialDrawer
Here's the code which creates the shown sample (You can also find it over at the repository on GitHub):
DrawerBuilder builder = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withInnerShadow(true)
.addDrawerItems(
//.. add some items ..
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
//some actions inside the listener
return miniResult.onItemClick(drawerItem);
}
})
.withSavedInstance(savedInstanceState);
// build the main drawer
result = builder.buildView();
// build the miniDrawer
miniResult = new MiniDrawer().withDrawer(result).withInnerShadow(true).withAccountHeader(headerResult);
//define the width of the normal drawer, and the minidrawer
int first = (int) UIUtils.convertDpToPixel(300, this);
int second = (int) UIUtils.convertDpToPixel(72, this);
//create the Crossfader used to hook the MiniDrawer and the normal drawer together. This also handles the crossfade effect.
crossFader = new Crossfader()
.withContent(findViewById(R.id.crossfade_content))
.withFirst(result.getSlider(), first)
.withSecond(miniResult.build(this), second)
.withSavedInstance(savedInstanceState)
.build();
// inform the MiniDrawer about the crossfader.
miniResult.withCrossFader(new CrossfadeWrapper(crossFader));
I figured out a way to implement the mini navigation drawer using the SlidingPaneLayout.
Create a layout resource file and set SlidingPaneLayout as your parent view. SlidingPaneLayout requires two child views: a master view and a detail view. The master view will contain a list of all our menu options and the detail view will contain the content.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Master fragment-->
<fragment
android:name="com.ng.anthony.mininavigationdrawer.MasterFragment"
android:layout_width="220dp"
android:layout_height="match_parent"
android:id="#+id/fragment_master">
</fragment>
<!--Detail layout -->
<FrameLayout
android:layout_width="1000dp"
android:layout_height="match_parent"
android:layout_marginLeft="56dp">
</FrameLayout>
</android.support.v4.widget.SlidingPaneLayout>
Create a master fragment class. Inside your master fragment you should have a list view with all your menu options.
public class MasterFragment extends ListFragment {
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_master, container);
setListAdapter(new MenuListAdapter(R.layout.row_menu_action_item, getActivity(), MenuActionItem.values()));
return view;
}
}
Add the master fragment layout to your layout resources folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:divider="#null">
</ListView>
</LinearLayout>
The master fragment contains a list view and uses an enumeration of menu options to populate the list.
public enum MenuActionItem {
ITEM1,
ITEM2,
ITEM3,
ITEM4,
ITEM5
}
The master fragment also contains a custom array adapter that displays the list of menu options. The custom array adapter inflates a row layout for each menu option.
import android.app.Activity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Anthony on 16-01-25.
*/
public class MenuListAdapter extends ArrayAdapter<MenuActionItem> {
int resource;
Activity activity;
public MenuListAdapter(int resource, Activity activity, MenuActionItem[] items) {
super(activity, resource, items);
this.resource = resource;
this.activity = activity;
}
public View getView (int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null) {
rowView = activity.getLayoutInflater().inflate(resource, null);
MenuItemViewHolder viewHolder = new MenuItemViewHolder();
viewHolder.menuItemImageView = (ImageView)rowView.findViewById(R.id.menu_item_image_view);
viewHolder.menuItemTextView = (TextView)rowView.findViewById(R.id.menu_item_text_view);
rowView.setTag(viewHolder);
}
MenuItemViewHolder holder = (MenuItemViewHolder)rowView.getTag();
if(position == MenuActionItem.ITEM1.ordinal()) {
holder.menuItemImageView.setImageDrawable(activity.getDrawable(R.drawable.ic_payment_white_24dp));
holder.menuItemTextView.setText(activity.getResources().getString(R.string.item1));
}
else if(position == MenuActionItem.ITEM2.ordinal()) {
holder.menuItemImageView.setImageDrawable(activity.getDrawable(R.drawable.ic_pets_white_24dp));
holder.menuItemTextView.setText(activity.getResources().getString(R.string.item2));
}
else if(position == MenuActionItem.ITEM3.ordinal()) {
holder.menuItemImageView.setImageDrawable(activity.getDrawable(R.drawable.ic_receipt_white_24dp));
holder.menuItemTextView.setText(activity.getResources().getString(R.string.item3));
}
else if(position == MenuActionItem.ITEM4.ordinal()) {
holder.menuItemImageView.setImageDrawable(activity.getDrawable(R.drawable.ic_shopping_cart_white_24dp));
holder.menuItemTextView.setText(activity.getResources().getString(R.string.item4));
}
else if(position == MenuActionItem.ITEM5.ordinal()) {
holder.menuItemImageView.setImageDrawable(activity.getDrawable(R.drawable.ic_work_white_24dp));
holder.menuItemTextView.setText(activity.getResources().getString(R.string.item5));
}
return rowView;
}
private static class MenuItemViewHolder {
public ImageView menuItemImageView;
public TextView menuItemTextView;
}
}
Add the row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:gravity="center_vertical">
<ImageView
android:id="#+id/menu_item_image_view"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="16dp"/>
<TextView
android:id="#+id/menu_item_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#android:color/white"/>
</LinearLayout>
In the end you should see something like this
You can download the sample project here: https://github.com/nganthony/MiniNavigationDrawer
Related
I am new to Android TV development. I don't know which type of fragment to use. I want two achieve layout similar to above screenshot as jiocinema. Somehow I have achieved it using a two xml fragments inside activity layout. Second fragments loads screenshot after hitting an API so it loads after some time. As can be seen in the above screenshot I want the layout in two parts..top one with details and some buttons and the bottom one is a list of screenshots of that movie.
In my case the problem is, bottom list part takes the focus on loading this particular screen after that on pressing up button or any button it never loses focus and never goes on the top part.
Note: below fragment loads asynchronously, as it hits an api for screenshot urls
May be I haven't used proper fragments for this particular layout. Can someone point me to the code or help me out in deciding what to use for this kind of layout. As it can be achieved but navigation is the main thing.
code
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/photo_label_box">
<fragment
android:id="#+id/detail_layout"
android:layout_width="match_parent"
android:name="com.DetailsActivityGame$Detalfragment"
android:layout_height="200dp"></fragment>
<fragment
android:id="#+id/row_layout"
android:layout_width="match_parent"
android:layout_below="#+id/detail_layout"
android:name="com.DetailsActivityGame$SampleFragmentC"
android:layout_height="wrap_content"></fragment>
</RelativeLayout>
Thanks
Try to use the RowSupportFragment in V4 Support Fragment for desired output.
Divide layout into two parts layout with buttons, description and below scrolling layout(Represent by RowSupportFragment)
//----------------------detail_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/leader_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_layout"
//your own layout design for buttons and description
</RelativeLayout>
<fragment
android:name="FragmentScreenshots"
android:id="#+id/screenshot_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//----------------Detailfragment--------------------
public static class Detailfragment extends Fragment {
public Detailfragment(){ }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.detail_layout, container, false);
//————————your own implementation—————————————————————————
return view;
}
public static class FragmentScreenshots extends RowsSupportFragment {
private ArrayObjectAdapter mRowsAdapter = null;
public FragmentScreenshots() {
mRowsAdapter = new ArrayObjectAdapter(new ShadowRowPresenterSelector());
setAdapter(mRowsAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//———————Provide data accordinally———————————
List<ScreenshotItem> list;
// Add a Related items row
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(
new ScreenshotCardPresenter(getActivity()));
for (ScreenshotItem s:list)
{
listRowAdapter.add(s);
}
HeaderItem header = new HeaderItem("Screenshots");
mRowsAdapter.add(new ListRow(header, listRowAdapter));
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(new OnItemViewClickedListener() {
#Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof ScreenshotItem) {
}
else{
}
}
});
setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
#Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
}
});
}
#Override
public void setExpand(boolean expand) {
super.setExpand(true);
}
#Override
public void setOnItemViewClickedListener(BaseOnItemViewClickedListener listener) {
super.setOnItemViewClickedListener(listener);
}
#Override
public void setOnItemViewSelectedListener(BaseOnItemViewSelectedListener listener) {
super.setOnItemViewSelectedListener(listener);
}
}}
You have to use BrowseFragment for your purpose. It is composed of a RowsFragment and a HeadersFragment.
A BrowseFragment renders the elements of its ObjectAdapter as a set of rows in a vertical list. The elements in this adapter must be subclasses of Row.
This tutorial can help you to get started.
There's very little literature on this topic, and google's documents don't account for the possibility of customization (listviewanimation) of the fragment's list using ListFragment extension. Therefore, I'm going to ask this question, and then answer it as best as possible, because I also want 50 reputation points so I can finally thank great explainers on this website through comments.
For the purpose of this comment, I will have components from the listviewanimation lib laced in:
https://github.com/nhaarman/ListViewAnimations
Answer:
We will need to set up 4 components to have a proper fragment with a listview component
The Activity Creating the fragment through the activity's fragment manager.
The Fragment class which will be pretty basic fragment stuff, it will have the listview, and it will link that listview with an arrayadapter.
The Adapter class which for our purposes will only handle strings.
WITHIN THE ADAPTER CLASS the final fourth component will be a viewholder class which will allow the rows within the list to be created faster, since each row's individual components will be wrapped up in a class that will allow for quicker object instantiation.
Ok so, first will be the code for the activity, this code can be called by a button click or some other event. When the event happens, the fragment manager will be created, and that fragment manager will create a transaction which is a fancy way of saying, the manager will communicate between the activity and the newly formed fragment to get everything set up properly.
Here's the code that should be placed in your activity where the event occurs:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GenericFragment fragment = new GenericFragment();
fragmentTransaction.add(R.id.pager, fragment);
//Replace R.id.pager with the view that you want your fragment to go in.
fragmentTransaction.commit();
That's it! Not so bad, is it? Now let's move on to the GenericFragment class, which you can create a different name of course. I won't be posting all the code for this, but I'll step through everything you need for a fragment class that has a listview:
Have your Fragment class extend Fragment
Have an empty constructor for this class (google requires it... -__- )
Create a newInstance method which will handle the passing of data from the activity to the fragment when a 'new instance' of the fragment is created from the activity:
I'll help you with this one:
public static GenericFragment newInstance(String StuffYouWantGetsPassedFromActivityToFragment) {
GenericFragment GenericFragment = new GenericFragment();
Bundle args = new Bundle();
GenericFragment.setArguments(args);
return GenericFragment;
}
Again not so bad, right? We're still not done, we still need to override onCreateView and onCreate, then we'll be done with this simple step!
Ok for onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.generic_fragment_layout, container, false);
addGoalButton = (Button) view.findViewById(R.id.btn_newRow); //Created for testing purposes
lv = (ListView) view.findViewById(R.id.GenericListView);
addGoalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { //Created for testing purposes
genericAdapter.add("Goal");
genericAdapter.notifyDataSetChanged();
}
});
lv.setAdapter(genericAdapter);
return view;
}
That above code may seem like a monstrosity, and you're right! The high level overview is that you're getting the layout file that you want the fragment to look like. From that layout file, you're getting the listview and creating a variable to hold it in. Then you're calling that listView's 'setAdapter' method to add the next step, the adapter class. For testing purposes, I added that button, so that you can mentally extend this tutorial l8er. (delete all button code if you'd like just a list)
Ok, one last step in the fragment class: Overriding OnCreate!
The OnCreate method is where you want to instantiate all your private variables like the genericAdapter variable or anything that you'd like to use over the multiple parts of the Fragment class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> exampleItemList = new ArrayList<String>();
exampleItemList.add("item1");
exampleItemList.add("item2");
exampleItemList.add("item3");
exampleItemList.add("item4");
exampleItemList.add("item5");
exampleItemList.add("item6");
exampleItemList.add("item7");
exampleItemList.add("item8");
exampleItemList.add("item9");
exampleItemList.add("item10");
exampleItemList.add("item11");
exampleItemList.add("item12");
genericAdapter = new genericAdapter(getActivity(), 0, exampleItemList);
setHasOptionsMenu(true); // Allows the fragment to change the menu buttons
}
I added the example items to an arrayList to make this tutorial a bit more transparent about where data is coming from, and where it's going.
That's it! You're Fragment is done! It's almost over, I promise.
Let's knock these last two steps out together, creating a GenericAdapter class that extends ArrayAdapter and has a private inner ViewHolder class to wrap all the layout components in:
public class GenericAdapter extends ArrayAdapter<String>
LayoutInflater layoutInflater;
//Used to get the correct LayoutInflater to inflate each row item
public GenericAdapter(Context context, int resource, List<String> objects) {
super(context, 0, objects);
layoutInflater = layoutInflater.from(context);
}
/**
* #param position The position in the list to get the data for that row item.
* #param convertView The view for the row item that will be shown in the list.
* #param parent Having this object allows you to use the LayoutInflater for the parent.
* #return
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final GenericViewHolder GenericViewHolder;
final String item = getItem(position);
if(convertView == null){
LinearLayout rootView = (LinearLayout) layoutInflater.inflate(R.layout.item_row, parent, false);
genericViewHolder = genericViewHolder.create(rootView);
rootView.setTag(genericViewHolder);
}
else{
genericViewHolder = (genericViewHolder) convertView.getTag();
}
genericViewHolder.textView.setText(item);
return genericViewHolder.rootView;
}
/**
* ViewHolder's allow for a single object to maintain a Goal row item, so that the row item
* doesn't have to create each individual component (textview layout etc.) each time the
* row object is created/recreated. Allows for fast scrolling with little latency.
*/
private static class GenericViewHolder {
public final LinearLayout rootView;
public final GripView gripView;
public final TextView textView;
private GoalViewHolder(LinearLayout rootView, GripView gripView, TextView textView) {
this.rootView = rootView;
this.gripView = gripView;
this.textView = textView;
}
public static GoalViewHolder create(LinearLayout rootView){
TextView textView = (TextView)rootView.findViewById(R.id.list_row_draganddrop_textview);
GripView gripView = (GripView)rootView.findViewById(R.id.list_row_draganddrop_touchview);
return new GenericViewHolder(rootView, gripView, textView);
}
}
}
That was again, a monstrosity, let's look at the high level overview, we created an adapter class, and a viewholder class for the adapter class to use. In the adapter's constructor we got a layoutinflater to help with inflating each row's item. Then, we created the getView method which get's called thousands of times in your app, because it handles making the each row appear when it's viewable by the user. The getView method sees if the view to be converted into a row is null or not. If it is, it will create a new data entry (a viewholder), but if it's not null, then that viewholder has already been created, so we get whatever was inside the viewholder already, so that we don't have to create a new row item.
phew! I don't expect you to understand any of that, but congrats if you do.
Ok so that's it. You should be set, and when your activity's event get's called, the fragment will show up in whatever view is containing the fragment. I'll post my xml files in my answer so that I can get those delicious upvotes (or not, I may be completely incorrect, but this worked for me!)
enjoy life, don't give up!
The activity xml, most of it is irrelevant to you the reader, but the container view for the fragment is pager:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Navigation Drawer Still Under Construction-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
<!--Navigation Drawer Still Under Construction-->
<!--Customizations on README at: https://github.com/astuetz/PagerSlidingTabStrip-->
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="48dip"
app:pstsShouldExpand="true"
app:pstsIndicatorHeight="5dip"
app:pstsDividerPadding="0dip"
app:pstsDividerColor="#ff6d00"
app:pstsUnderlineColor="#ff5722"
app:pstsIndicatorColor="#ff5722"/>
<!--To scale the viewpager vertically, android:layout_above="#+id/[viewname]" -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabs"
tools:context=".MainActivity" />
</RelativeLayout>
The xml layout for the fragment:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Item"
android:id="#+id/btn_newItem"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
android:id="#+id/GenericListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_newGoal" />
The specific row item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UseCompoundDrawables">
<com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView
android:id="#+id/list_row_draganddrop_touchview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:color="#android:color/darker_gray"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp" />
<TextView
android:id="#+id/list_row_draganddrop_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:minHeight="48dp"
android:textColor="?android:attr/textColorSecondary"
android:textSize="20sp"
tools:ignore="UnusedAttribute" />
</LinearLayout>
The layout portion of the 2nd code snippet got cut off and SO wasn't agreeing with my ctrl K'ing, but the long and short of it, is that it doesn't matter, because the listview is there, so it doesn't matter whether you put it in a linear layout or a relative layout.
Good luck bro's happy coding
I have used a ListFragment with an ArrayAdapter to show a list of items. You can find the code below.
public class SelectRepiceFragment extends ListFragment {
private BuyRecipeActivity mActivity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = mActivity.getResources();
int recipeNum = res.getInteger(R.integer.recipe_number);
String[] titles = res.getStringArray(R.array.titles);
ArrayList<RecipeItem> recipeItems = new ArrayList<RecipeItem>(recipeNum);
for (int i = 0; i < recipeNum; i++) {
RecipeItem item = new RecipeItem();
item.title = titles[i];
recipeItems.add(item);
}
RecipeItemAdapter recipeItemAdapter = new RecipeItemAdapter(mActivity, recipeItems);
setListAdapter(recipeItemAdapter);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = (BuyRecipeActivity) activity;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
mActivity.onRecipeSelected(position);
}
}
The size of recipeItems is 3 (I check it with debugger). So the getView function in ArrayAdapter should be called 3 times to generate the ListFragment. Here I have copied the ArrayAdapter.
public class RecipeItemAdapter extends ArrayAdapter<RecipeItem> {
private final Activity mActivity;
static class ViewHolder {
public MyTextView title;
public ImageView image;
}
public RecipeItemAdapter(Activity context, ArrayList<RecipeItem> recipeItems) {
super(context, R.layout.recipe_list, recipeItems);
this.mActivity = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.recipe_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (MyTextView) rowView.findViewById(R.id.recipeItemTitle);
viewHolder.image = (ImageView) rowView.findViewById(R.id.recipeItemImage);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
RecipeItem recipeItem = getItem(position);
holder.title.setText(recipeItem.title);
holder.image.setImageResource(R.drawable.ic_launcher);
return rowView;
}
}
The problem is that getView is actually called three times, while every time the position parameter is equal to 0. So After running the app, I just see a single row in my list instead of 3 rows.
UPDATE: I found out that I just see the first item, but if I scroll it up, then it shows the rest of items in that single row! Consequently I guessed there should be a mistake in the xml file. So I add xml files bellow.
This is my main activity xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/fragmentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
</ScrollView>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#dcdcdc"
android:choiceMode="singleChoice"
android:divider="#dcdcdc"
android:dividerHeight="0.5dp" />
</android.support.v4.widget.DrawerLayout>
And this is my recipe_item xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/recipeItemTitle"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#drawable/design_price_view"
android:gravity="center" />
<ImageView
android:id="#+id/recipeItemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/app_icon" />
</LinearLayout>
UPDATE 2: I removed the DrawerLayout from my main activity xml and now I see the list. Just confusing.
You may try removing the ScrollView, take the FrameLayout out, put
layout_height="0dp"
layout_weight="1"
to the FrameLayout. You will need to manually fix the height of your navigation drawer to predefined pixel.
The above setup will make your FrameLayout & your ListView inside it take all the available space left from the navigational drawer.
The problem is, if you place a ListView inside a ScrollView - there is a rendering problem. I have not worked with ListView inside FrameLayout so cant really comment on that.
If you absolutely need to keep the ScrollView, you would need to re-render your ListView so that it spans the height it needs. Check this thread
I'm having two issues with my Android application. I'll start with the relevant code, which I'm 99% certain is locked within my MainActivity class:
package com.simplerssreader;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
private ListView list;
private TextFragment text;
private RssFragment rss;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Initializing AddNote fragment
if (getFragmentManager().findFragmentByTag("RSS") == null)
{
rss = new RssFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, rss, "RSS").commit();
}
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
if (getFragmentManager().findFragmentByTag("RSS") == null)
{
rss = new RssFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, rss, "RSS").commit();
}
if (getFragmentManager().findFragmentByTag("TEXT") == null)
{
text = new TextFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.text, text, "TEXT").commit();
}
}
//View view = getLayoutInflater().inflate(R.layout.fragment_layout, null);
list = (ListView) this.findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
System.out.println("YES!");
/*RssAdapter adapter = (RssAdapter) parent.getAdapter();
RssItem item = (RssItem) adapter.getItem(position);
TextFragment fragment = new TextFragment();
fragment.setLink(item.getLink());
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
xact.replace(R.id.text, fragment, "TEXT");*/
}
});
if (savedInstanceState != null) {
text = (TextFragment) getSupportFragmentManager().findFragmentByTag("TEXT");
rss = (RssFragment) getSupportFragmentManager().findFragmentByTag("RSS");
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("fragment_added", true);
}
}
(Note that I'm not concerned with the code within the onItemSelected at the moment; I'll deal with that later, when I actually have working code.)
So my first problem is with this line:
list = (ListView) this.findViewById(R.id.listView);
What I'm hoping to attain here is the ListView found in the current display (in portrait or landscape mode, the list view will be displayed). I also attempted another method in the commented out line above that, but it also failed. The error here is a NullPointerException.
My second issue is that whenever I go to portrait mode from landscape mode, the application crashes. I can go from portrait to landscape, and boot in either mode, but going back to portrait kills the application. I've made the proper checks for orientation in onCreate so I'm not sure of where I'm going wrong. The error here states that there's no TextView to find in the current view- but that should only be called when I'm in landscape mode, right?
Anyways, any help would be greatly appreciated!
Here are my layouts:
fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
main.xml (portrait)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/fragment_container"
android:layout_height="fill_parent"
/>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
main.xml (landscape)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"
android:baselineAligned="false">
<FrameLayout android:id="#+id/fragment_container" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
<FrameLayout android:id="#+id/text" android:layout_weight="2"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
First, try moving the code where you get the list view to the fragment.
Also, in
if (savedInstanceState != null) {
text = (TextFragment) getSupportFragmentManager().findFragmentByTag("TEXT");
rss = (RssFragment) getSupportFragmentManager().findFragmentByTag("RSS");
}
you overwrite the text even if there was no TEXT fragment before, in case there's a savedInstance
Finally, you have duplicate ids:
<FrameLayout android:id="#+id/text" android:layout_weight="2"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
and
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Firstly, I don't see your ListView set to an Adapter in your activity. Here is just a simple example, where customtextview is a simple TextView and items a string array:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.customtextview, items);
listView.setAdapter(adapter);
Then, you have two id with "text" (R.layout.text_view and FrameLayout in R.layout.main landscape). If you use both in one of your Fragment, this will crash. Change one of them..
And, because you have the FrameLayout "text" only in landscape mode, you can do your onCreate method as below (check if this FrameLayout is present or not):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// in both layout, you add this Fragment in the same FrameLayout
// check the savedInstanceState, if isn't null = orientation has changed
if (savedInstanceState == null)
rss = new RssFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, rss, "RSS").commit();
} else {
rss = (RssFragment) getSupportFragmentManager().findFragmentByTag("RSS");
}
/*
** check if FrameLayout text is null (true = landscape)
*/
if(findViewById(R.id.text) != null) {
// check the savedInstanceState
if (savedInstanceState == null) {
text = new TextFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.text, text, "TEXT").commit();
} else {
text = (TextFragment) getSupportFragmentManager().findFragmentByTag("TEXT");
}
}
/*
** else the id isn't exist (false = portrait) // do nothing
*/
}
Then, in your RSS Fragment, set your ListView:
private ListView list;
String[] items = new String[] {
"One",
"Two",
"Three",
"And so on.."
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
list = (ListView) v.findViewById(R.id.listView);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
list.setAdapter(adapt);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("YES!");
/* some stuff */
}
});
return v;
}
To save a fragment state on a activity, you can use setRetainInstance(true). There is a great post on it, and a tutorial. For more information about what happens with fragment and orientation changed, you can read this answer.
Let me know if this is helpful.
I searched a bit on the web, and I found these tutorials which might help you a lot:
great: Create Android Dynamic view with Fragments
great: User Interface Design: Working With Fragments
not bad: Fragment example tutorial using WebView
All works with WebView and they mostly use findFragmentById and not tag. Maybe it's the clue..
But you can add directly on the xml the appropriate fragments, as:
- Portrait Mode :
o layout 1 : listview (ex fragment RSS)
o layout 2 : fragment WebView
- Landscape Mode :
o layout 1 :
LinearLayout (id container) : listview (ex fragment RSS) + framelayout
And on your FragmentActivity, you host your list and check the orientation, like the following:
ListView list = (ListView) findViewById(...);
list.setAdapter(...);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
if(container != null) {
// your framelayout is present so set your fragment WebView
// inside it with a fragment transaction and also
// use the item clicked to send info to this fragment
} else {
// call a new activity on item clicked to display the WebView
}
This should work..
I hope these added informations will help you to achieve that.
I have a custom dialog class that extends Dialog. Inside this I have a Tab Layout with 2 tabs. In each tab I have a list view. Everything works but I can't get scroll bars to show up.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TabHost01"
android:layout_width="300dp"
android:layout_height="300dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
<ListView
android:id="#+id/ListView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
</FrameLayout>
</LinearLayout>
</TabHost>
and here is part of my code that sets this up:
// get this window's layout parameters so we can change the position
WindowManager.LayoutParams params = getWindow().getAttributes();
// change the position. 0,0 is center
params.x = 0;
params.y = 250;
this.getWindow().setAttributes(params);
// no title on this dialog
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.myLayout);
// instantiate our list views for each tab
ListView listView01 = (ListView)findViewById(R.id.ListView01);
ListView listView02 = (ListView)findViewById(R.id.ListView02);
// instantiate and set our custom list view adapters
listViewAdapter01 = new ListViewAdapter01(context);
listView01.setAdapter(listViewAdapter01);
listViewAdapter02 = new ListViewAdapter02(context);
listView02.setAdapter(listViewAdapter02);
// get our tabHost from the xml
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
// create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator("List 1");
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator("List 01");
tabs.addTab(tab2);
OK here is the complete working code for a custom dialog class that contains a tabbed layout which contains a listView. The first tab has a listView with rows being a textView and an imageView with the imageView being right aligned. The second tab has a listView with rows being a single textView. The scroll bars are set to a high fade duration to make them always show. The dialog window itself is set to a static size to prevent the dialog from resizing when switching tabs. The dialog window is also positioned lower on the screen, not in the center. The listViews use custom adapters and the second tab's listView is registered for a context menu.
I have renamed everything to be more generic and ont contain names of our product, so I may have made some typos when renaming but I think everything is right. Tried to comment the code as best I could. Hope this helps some people.
The customDialog's XML (custom_dialog_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="300dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarFadeDuration="1000000"/>
<ListView
android:id="#+id/listView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarFadeDuration="1000000"/>
</FrameLayout>
</LinearLayout>
</TabHost>
Tab 1 listView row XML (list_view_01_row.xml). This is a textView, left aligned and an imageView, right aligned. The textView has been set to a larger height in order to force the listView rows to be higher. The listView has also been set to a specific width, this pushes the imageView to the right in order to right align it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TableLayout
android:id="#+id/list_view_01_row_table_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow
android:id="#+id/list_view_01_row_table_row"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/list_view_01_row_text_view"
android:textSize="18sp"
android:textColor="#ffffff"
android:gravity="center_vertical"
android:layout_width="200dip"
android:layout_height="75dip" />
<ImageView
android:id="#+id/list_view_01_row_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
Tab 2 listView row XML (list_view_02_row.xml). Same as tab 1 but with a single textView, no imageView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TableLayout
android:id="#+id/list_view_02_row_table_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow
android:id="#+id/list_view_02_row_table_row"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/list_view_02_row_text_view"
android:textSize="18sp"
android:textColor="#ffffff"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="75dip" />
</TableRow>
</TableLayout>
</LinearLayout>
And finally the custom Dialog class.
import android.app.Dialog;
import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
/**
* This is a custom dialog class that will hold a tab view with 2 tabs.
* Tab 1 will be a list view. Tab 2 will be a list view.
*
*/
public class CustomDialog extends Dialog
{
/**
* Our custom list view adapter for tab 1 listView (listView01).
*/
ListView01Adapter listView01Adapter = null;
/**
* Our custom list view adapter for tab2 listView (listView02).
*/
ListView02Adapter listView02Adapter = null;
/**
* Default constructor.
*
* #param context
*/
public CustomDialog(Context context)
{
super(context);
// get this window's layout parameters so we can change the position
WindowManager.LayoutParams params = getWindow().getAttributes();
// change the position. 0,0 is center
params.x = 0;
params.y = 250;
this.getWindow().setAttributes(params);
// no title on this dialog
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog_layout);
// instantiate our list views for each tab
ListView listView01 = (ListView)findViewById(R.id.listView01);
ListView listView02 = (ListView)findViewById(R.id.listView02);
// register a context menu for all our listView02 items
registerForContextMenu(listView02);
// instantiate and set our custom list view adapters
listView01Adapter = new ListView01Adapter(context);
listView01.setAdapter(listView01Adapter);
listView02Adapter = new ListView02Adapter(context);
listView02.setAdapter(listView02Adapter);
// bind a click listener to the listView01 list
listView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parentView, View childView, int position, long id)
{
// will dismiss the dialog
dismiss();
}
});
// bind a click listener to the listView02 list
listView02.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parentView, View childView, int position, long id)
{
// will dismiss the dialog
dismiss();
}
});
// get our tabHost from the xml
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
// create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator("List 1");
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator("List 2");
tabs.addTab(tab2);
}
/**
* A custom list adapter for the listView01
*/
private class ListView01Adapter extends BaseAdapter
{
public ListView01Adapter(Context context)
{
}
/**
* This is used to return how many rows are in the list view
*/
public int getCount()
{
// add code here to determine how many results we have, hard coded for now
return 10;
}
/**
* Should return whatever object represents one row in the
* list.
*/
public Object getItem(int position)
{
return position;
}
/**
* Used to return the id of any custom data object.
*/
public long getItemId(int position)
{
return position;
}
/**
* This is used to define each row in the list view.
*/
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
// our custom holder will represent the view on each row. See class below.
ListView01Holder holder = null;
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
// inflate our row from xml
row = inflater.inflate(R.layout.list_view_01_row, parent, false);
// instantiate our holder
holder = new ListView01Holder(row);
// set our holder to the row
row.setTag(holder);
}
else
{
holder = (ListView01Holder)row.getTag();
}
return row;
}
// our custom holder
class ListView01Holder
{
// text view
private TextView text = null;
// image view
private ImageView image = null;
ListView01Holder(View row)
{
// get out text view from xml
text = (TextView)row.findViewById(R.id.image);
// add code here to set the text
text.setText("");
// get our image view from xml
image = (ImageView)row.findViewById(R.id.list_view_01_row_image_view);
// add code here to determine which image to load, hard coded for now
rating.setImageResource(R.drawable.image);
}
}
}
/**
* A custom list adapter for listView02
*/
private class ListView02Adapter extends BaseAdapter
{
public ListView02Adapter(Context context)
{
}
/**
* This is used to return how many rows are in the list view
*/
public int getCount()
{
// add code here to determine how many results we have, hard coded for now
return 5;
}
/**
* Should return whatever object represents one row in the
* list.
*/
public Object getItem(int position)
{
return position;
}
/**
* Used to return the id of any custom data object.
*/
public long getItemId(int position)
{
return position;
}
/**
* This is used to define each row in the list view.
*/
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ListView02Holder holder = null;
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
row=inflater.inflate(R.layout.list_view_02_row, parent, false);
holder = new ListView02Holder(row);
row.setTag(holder);
}
else
{
holder = (ListView02Holder)row.getTag();
}
return row;
}
class ListView02Holder
{
private TextView text = null;
ListView02Holder(View row)
{
text = (TextView)row.findViewById(R.id.list_view_02_row_text_view);
text.setText("");
}
}
}
/**
* This is called when a long press occurs on our listView02 items.
*/
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Delete");
}
/**
* This is called when an item in our context menu is clicked.
*/
public boolean onContextItemSelected(MenuItem item)
{
if(item.getTitle() == "Delete")
{
}
else
{
return false;
}
return true;
}
}
Try to set all of the android:layout_width attributes within your layout xml to "fill_parent".