I am trying to create a navigation draw which displays text and images. My project builds OK but when I open the navigation draw there is no text or images displayed. I have created a layout file for the image and text to be displayed on the navigation draw and I have set the adapter for the navigation draw as well as creating my own Adapter class.
I cant see what is wrong but something obviously is :) Any help is greatly appreciated.
Here are my files
navigation_draw_items.xml
<?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">
<!-- THis file will contain the list of items in the navigation draw -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView3"
android:layout_gravity="left"/>
</LinearLayout>
activity_main.xml
<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 which will be a fragment-->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- <LinearLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
-->
<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"/>
<!-- </LinearLayout> -->
</android.support.v4.widget.DrawerLayout>
AdapterClass.java
public class AdapterClass extends BaseAdapter {
private Context context;
private NavigationItem[] navigationItems;
public AdapterClass(Context context, NavigationItem[] items)
{
this.context = context;
navigationItems = items;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.navigation_draw_items, parent,false);
}
else
{
row = convertView;
}
TextView tvTitle = (TextView) row.findViewById(R.id.textView3);
ImageView tvImage = (ImageView) row.findViewById(R.id.imageView);
tvTitle.setText(navigationItems[position].stringTitle);
tvImage.setImageResource(navigationItems[position].drawableIcon);
return row;
}
}
NavigationItem.java
public class NavigationItem {
public NavigationItem(int stringTitle, int drawableIcon)
{
this.stringTitle = stringTitle;
this.drawableIcon = drawableIcon;
}
public int stringTitle;
public int drawableIcon;
}
MainActivity.java
public class MainActivity extends Activity implements EditListFragment.OnFragmentInteractionListener {
//private String[] items;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
public void onFragmentInteraction(String id)
{
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
// To do
AdapterClass navigationMenuAdapter;
NavigationItem[] navigationMenuItems = {
new NavigationItem(R.string.cat, R.drawable.cat),
new NavigationItem(R.string.dog, R.drawable.dog),
new NavigationItem(R.string.fish, R.drawable.fish),
};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationMenuAdapter = new AdapterClass(this, navigationMenuItems);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(navigationMenuAdapter);
mDrawerList.setOnItemClickListener(new DrawItemClickListener());
// Old code
//mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
//items = getResources().getStringArray(R.array.items_array);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectedItem(position);
}
}
private void selectedItem(int position)
{
switch (position)
{
case 0:
//Fragment addList = ItemList
//Fragment addList = new ItemList();
//Bundle args = new Bundle();
//args.putInt(ItemList.LIST_NUMBER, position);
//addList.setArguments(args);
Fragment addList = ItemList.newInstance();
FragmentManager addListFragmentManager = getFragmentManager();
addListFragmentManager.beginTransaction().replace(R.id.content_frame, addList).commit();
//mDrawerLayout.closeDrawer(mDrawerLayout);
break;
case 1:
Fragment editList = EditListFragment.newInstance();
FragmentManager editListFragmentManager = getFragmentManager();
editListFragmentManager.beginTransaction().replace(R.id.content_frame, editList).commit();
break;
case 2:
Fragment deleteList = EditListFragment.newInstance();
FragmentManager deleteListFragmentManager = getFragmentManager();
deleteListFragmentManager.beginTransaction().replace(R.id.content_frame, deleteList).commit();
break;
}
}
}
Related
I added a navigation drawer in my app and creates a list of fragments Home,Mobiles,laptops in it. When I ran the app the main activity content launched on start. What I need now is when I click on the items in the navigation drawer it opens the fragment layout but the main content is also displayed, how can I hide the main content when a fragment item is clicked.
This is the code
MainActivity.java:
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{
Application myApp;
RSSFeed feed;
ListView lv;
CustomListAdapter adapter;
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
};
private ShareActionProvider shareActionProvider;
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private int currentPosition = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myApp = getApplication();
// Get feed form the file
feed = (RSSFeed) getIntent().getExtras().get("feed");
// Initialize the variables:
lv = (ListView) findViewById(R.id.listView);
lv.setVerticalFadingEdgeEnabled(true);
// Set an Adapter to the ListView
adapter = new CustomListAdapter(this);
lv.setAdapter(adapter);
titles = getResources().getStringArray(R.array.titles);
drawerList = (ListView)findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Populate the ListView
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
//Display the correct fragment.
if (savedInstanceState != null) {
currentPosition = savedInstanceState.getInt("position");
setActionBarTitle(currentPosition);
} else {
selectItem(0);
}
// Set on item click listener to the ListView
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(MainActivity.this,
DetailActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
//Create the ActionBarDrawerToggle
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.string.open_drawer, R.string.close_drawer) {
//Called when a drawer has settled in a completely closed state
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
//Called when a drawer has settled in a completely open state.
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
FragmentManager fragMan = getSupportFragmentManager();
Fragment fragment = fragMan.findFragmentByTag("visible_fragment");
if (fragment instanceof TopFragment) {
currentPosition = 0;
}
if (fragment instanceof MobileFragment) {
currentPosition = 1;
}
if (fragment instanceof LaptopFragment) {
currentPosition = 2;
}
if (fragment instanceof StoresFragment) {
currentPosition = 3;
}
setActionBarTitle(currentPosition);
drawerList.setItemChecked(currentPosition, true);
}
}
);
}
private void selectItem(int position) {
// update the main content by replacing fragments
currentPosition = position;
Fragment fragment;
switch(position) {
case 1:
fragment = new MobileFragment();
break;
case 2:
fragment = new LaptopFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "visible_fragment");
ft.addToBackStack(null);
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
//Set the action bar title
setActionBarTitle(position);
//Close drawer
drawerLayout.closeDrawer(drawerList);
}
#Override
protected void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter(MainActivity activity) {
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
#Override
public int getCount() {
// Set the total list item count
return feed.getItemCount();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.date);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the drawer is open, hide action items related to the content view
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", currentPosition);
}
private void setActionBarTitle(int position) {
String title;
if (position == 0) {
title = getResources().getString(R.string.app_name);
} else {
title = titles[position];
}
getSupportActionBar().setTitle(title);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}
private void setIntent(String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_create_order:
//Code to run when the Create Order item is clicked
Intent intent = new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
//Code to run when the settings item is clicked
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="com.example.ayush.gadgetguru.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</LinearLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<ListView android:id="#+id/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="#ffffff"/>
</android.support.v4.widget.DrawerLayout>
TopFragment.java:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TopFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_top, container, false);
}
}
fragment_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</FrameLayout>
The first child in the layout is the listview for the main Activity UI in Navigation Drawer Layout . And I'm trying to display list-view with fragment and navigation drawer layout but list-view is not display in main Activity UI..Can someone help me how to show list.Thanks in advanced.
Here is my code
<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 first child in the layout is for the main Activity UI-->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Side navigation drawer UI -->
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private ListView mDrawerList ;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
Fragment fragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView) findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
fragment = new YourListFragment();
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}
}
private void addDrawerItems() {
String[] osArray = {"Android", "iOS", "Windows", "OS X", "Linux"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Display listview in fragment
public class YourListFragment extends Fragment {
int DR_CAMERA_REQUEST = 99999;
ListView allPostListView;
MyListAdapter adapter;
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.all_post_list_fragment, container, false);
allPostListView = (ListView)rootView.findViewById(R.id.listview_AllPost);
adapter = new MyListAdapter(getActivity().getBaseContext(),R.layout.all_post_row, bitmapArray);
allPostListView.setAdapter(adapter);
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == DR_CAMERA_REQUEST )
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.e("photo ", " = " + photo);
bitmapArray.add(photo);
//imgProfilePic.setImageBitmap(photo);
}
}
}
Adapter class
class MyListAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
int DR_CAMERA_REQUEST = 99999;
ArrayList<Bitmap> bmp = new ArrayList<Bitmap>();
ProgressBar pBar;int fixedHeight = 220;
public MyListAdapter(Context context, int layoutResourceId , ArrayList<Bitmap> bitmapArray) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.bmp = bitmapArray;
}
#Override
public int getCount() {
return bmp.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View row, ViewGroup parent) {
final Holder holder;
if (row == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
row = vi.inflate(R.layout.all_post_row, parent, false);
holder = new Holder();
holder.horizontalScrollView = (HorizontalScrollView) row.findViewById(R.id.hlist);
holder.lLinearLayout = (LinearLayout) row.findViewById(R.id.innerlay);
holder.imgBtn_Camera = (ImageView) row.findViewById(R.id.imgButton_Camera);
row.setTag(holder);
}
else
{
holder = (Holder) row.getTag();
}
LayoutInflater mInflater;
mInflater = LayoutInflater.from(getContext());
View cur_deal = mInflater.inflate(R.layout.horizontalitem, holder.lLinearLayout, false);
RelativeLayout rLayout = (RelativeLayout) cur_deal.findViewById(R.id.img_layout);
final ImageView imageView = (ImageView) cur_deal.findViewById(R.id.image_AllPost);
pBar = (ProgressBar) cur_deal.findViewById(R.id.pBar_AllPost);
holder.lLinearLayout.removeAllViews();
if(bmp.size() > 0)
{
int index = bmp.size() -1;
rLayout.getLayoutParams().height = fixedHeight;
Bitmap lastbitmap = bmp.get(index);
imageView.setImageBitmap(lastbitmap);
pBar.setVisibility(View.VISIBLE);
holder.lLinearLayout.addView(cur_deal);
}
//OnClickListener for camera button in the List
holder.imgBtn_Camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)context).startActivityForResult(cameraIntent, DR_CAMERA_REQUEST);
Log.e("Camera", " Open");
}
});
return row;
}
final class Holder {
ImageView imgBtn_Camera;
LinearLayout lLinearLayout;
HorizontalScrollView horizontalScrollView;
}
}
Here is my all_post_row.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="match_parent"
android:background="#android:color/white"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="#android:color/white"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="#+id/innerlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#android:color/white"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/buttonslayout"
android:layout_width="match_parent"
android:layout_height="26dp"
android:layout_marginTop="10dp"
android:background="#D8D8D8"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgButton_FoloowUp"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/follow_up_grey" />
<ImageView
android:id="#+id/imgButton_Camera"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/camera_grey" />
<ImageView
android:id="#+id/imgButton_RecordAudio"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/recorder_gray" />
</LinearLayout>
</LinearLayout>
Here is horizontal.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:paddingLeft="1dp">
<ImageView
android:id="#+id/image_AllPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ProgressBar
android:id="#+id/pBar_AllPost"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Change
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
to:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I am working with ViewPager with Circle Page Indicator.
And I am following this link for sample
Here in below I mentioned my class details.
HelpScreen.java
This Activity had one fragment called HelpScreenFragment.java
public class HelpScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_screen);
HelpScreenFragment helpScreenFragment = new HelpScreenFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction().add(R.id.frame_helpscreen_container,
helpScreenFragment);
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.help_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
help_screen.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<FrameLayout
android:id="#+id/frame_helpscreen_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
SlidePagerAdapter.java
This class extends PagerAdapter and used to inflate the
public class SlidePagerAdapter extends PagerAdapter {
ImageView helpScreenImage;
List<Integer> drawableBitmap;
int gallery_grid_Images[] = { R.drawable.sample1, R.drawable.sample2,
R.drawable.sample3, R.drawable.sample4, R.drawable.sample5, };
private Context mContext;
Activity activity;
public SlidePagerAdapter(Activity mContext) {
super();
this.activity = mContext;
}
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewpager_layout, container,
false);
helpScreenImage = (ImageView) view.findViewById(R.id.helpscreen_image);
helpScreenImage.setImageResource(gallery_grid_Images[position]);
((ViewPager) container).addView(helpScreenImage);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
viewpager_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" >
<ImageView
android:id="#+id/helpscreen_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
HelpScreenFragment.java
public class HelpScreenFragment extends Fragment {
ViewPager helpScreenPager;
CirclePageIndicator mCirclePageIndicator;
SlidePagerAdapter mSlidePagerAdapter;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.helpscreen_fragment, container,
false);
initializeWidgets(view);
mSlidePagerAdapter = new SlidePagerAdapter(activity);
helpScreenPager.setAdapter(mSlidePagerAdapter);
mCirclePageIndicator.setViewPager(helpScreenPager);
return view;
}
private void initializeWidgets(View view) {
helpScreenPager = (ViewPager) view.findViewById(R.id.pager);
mCirclePageIndicator = (CirclePageIndicator) view
.findViewById(R.id.page_indicator);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
And in helpscreen_fragment.xml contains only ViewPager and CirclePageIndicator
helpscreen_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.viewpagerdemo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
<ViewFlipper
android:id="#+id/helpscreen_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" /> -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.example.viewpagerdemo.CirclePageIndicator
android:id="#+id/page_indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view_pager"
android:padding="10dip" />
</FrameLayout>
When executing the project I am getting only blank screen.
What I did wrong? What changes have to made.
Could anybody help on this?
[1]: http://androidopentutorials.com/android-image-slideshow-using-viewpager/
[2]: http://i.stack.imgur.com/pSold.jpg
Here
#Override
public int getCount() {
return 0;
}
Currently getCount method of SlidePagerAdapter retuning 0 so no item is visible in ViewPager. return gallery_grid_Images array size from getCount() :
#Override
public int getCount() {
return gallery_grid_Images.length;
}
Good day, I'm trying to build a book shelf with a horizontal list inside a list view but when the app runs, the listview is blank.
What am i missing?
Here's my code (and some code from a tutorial which i was using as a guide)
ListBook.java
public class ListBooks extends ActionBarActivity {
ListView listView;
private VerticalAdapter verListAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_books);
listView = (ListView)findViewById(R.id.listbooks);
BookItem book1 = new BookItem("Fist of Fury");
BookItem book2 = new BookItem("Fist of the fists");
BookItem book3 = new BookItem("Enders game");
BookItem book4 = new BookItem("Lovers");
ArrayList<BookItem> arrayList = new ArrayList<BookItem>();
arrayList.add(book1);
arrayList.add(book2);
ArrayList<BookItem> list = new ArrayList<BookItem>();
list.add(book3);
list.add(book4);
ArrayList<ArrayList<BookItem>> group = new ArrayList<ArrayList<BookItem>>();
group.add(arrayList);
group.add(list);
verListAdapter = new VerticalAdapter(this, R.layout.activity_book_list, group);
listView.setAdapter(verListAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list_books, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class VerticalAdapter extends ArrayAdapter<ArrayList<BookItem>> {
private int resource;
public VerticalAdapter(Context _context, int _ResourceId,
ArrayList<ArrayList<BookItem>> _items) {
super(_context, _ResourceId, _items);
this.resource = _ResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null) {
rowView = LayoutInflater.from(getContext()).inflate(resource,
null);
} else {
rowView = convertView;
}
HorizontalListView hListView = (HorizontalListView) rowView
.findViewById(R.id.sublistview);
HorizontalAdapter horListAdapter = new HorizontalAdapter(
getContext(), R.layout.listitem, getItem(position));
hListView.setAdapter(horListAdapter);
return rowView;
}
}
private class HorizontalAdapter extends ArrayAdapter<BookItem> {
private int resource;
public HorizontalAdapter(Context _context, int _textViewResourceId,
ArrayList<BookItem> _items) {
super(_context, _textViewResourceId, _items);
this.resource = _textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(getContext()).inflate(
this.resource, null);
TextView topText = (TextView) retval.findViewById(R.id.title);
topText.setText(getItem(position).getTitle());
return retval;
}
}
}
Layout files
Listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff">
<ImageView
android:id="#+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:scaleType="centerCrop"
android:src="#drawable/wood"
/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:gravity="center_horizontal"
/>
</LinearLayout>
activity_book_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.azed">
<com.HorizontalListView
android:id="#+id/sublistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"/>
</LinearLayout>
activity_list_books.xml
<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="match_parent"
android:orientation="vertical"
tools:context="com.azed">
<ListView
android:id="#+id/listbooks"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
I have a Custom Base Adapter I am using with my ListView in a DrawerLayout. The ListView inflates ok, but I cannot call the onItemClickListener.
Here is the onCreate portion of the MainAcitivty, where I set the adapter and the onItemClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isPhone = getResources().getString(R.string.screen_type).toString().equals("phone");
mTitle = getResources().getString(R.string.app_name);
mCategoryTitles = getResources().getStringArray(R.array.categories);
mSubtext = getResources().getStringArray(R.array.subtext);
mTitle = mDrawerTitle = getTitle();
// Set the drawer toggle as the DrawerListener
mDrawerList = (ListView) findViewById(R.id.left_drawer);
BaseAdapter adapter = new NavigationListAdapter(this, mCategoryTitles, mSubtext);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
.....
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
//selectItem(position);
System.out.println("This is clicked");
}
}
Here is my Base Adapter Code
public class NavigationListAdapter extends BaseAdapter {
public static final int HDR_POS1 = 0;
public static final int HDR_POS2 = 9;
private static final Integer LIST_HEADER = 0;
private static final Integer LIST_ITEM = 1;
Context mContext;
String[] mCategories;
String[] mSubtext;
private SideBarPositionClickedCommunicator mCallback;
public interface SideBarPositionClickedCommunicator{
public void setListItemPosition(int itemInt);
}
public NavigationListAdapter(Context context, String[] categories, String[] subtext)
{
mContext = context;
mSubtext = subtext;
mCategories = categories;
}
#Override
public int getCount() {
return mCategories.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int positionClicked = position;
String headerText = getHeader(position);
if(headerText != null) {
View item = convertView;
if(convertView == null || convertView.getTag() == LIST_ITEM) {
item = LayoutInflater.from(mContext).inflate(
R.layout.lv_header_layout, parent, false);
item.setTag(LIST_HEADER);
}
TextView headerTextView = (TextView)item.findViewById(R.id.lv_list_hdr);
headerTextView.setText(headerText);
return item;
}
View item = convertView;
if(convertView == null || convertView.getTag() == LIST_HEADER) {
item = LayoutInflater.from(mContext).inflate(
R.layout.lv_layout, parent, false);
item.setTag(LIST_ITEM);
}
TextView header = (TextView)item.findViewById(R.id.lv_item_header);
header.setText(mCategories[position % mCategories.length]);
TextView subtext = (TextView)item.findViewById(R.id.lv_item_subtext);
subtext.setText(mSubtext[position % mCategories.length]);
//Set last divider in a sublist invisible
View divider = item.findViewById(R.id.item_separator);
if(position == HDR_POS2 -1) {
divider.setVisibility(View.INVISIBLE);
}
return item;
}
private String getHeader(int position) {
if(position == HDR_POS1 || position == HDR_POS2) {
return mCategories[position].toUpperCase();
}
return null;
}
#Override
public boolean isEnabled(int position) {
if(position == HDR_POS1 || position == HDR_POS2)
return false;
else
return true;
}
}
Here are my XML Layouts.
Header Layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="#dimen/lvHdrItemHeight"
>
<View
android:id="#+id/item_separator"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="#dimen/lvDividerHeight"
android:background="#color/dark_gray"
android:layout_marginTop="#dimen/lvSectionDividerMarginTop"
/>
<TextView
android:text="This is a text"
android:id="#+id/lv_list_hdr"
android:textColor="#color/dark_gray"
android:gravity="bottom|left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#id/item_separator"
android:layout_alignParentLeft="true"
style="#style/listViewHeaderItem"
/>
</RelativeLayout>
Each line item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".MainActivity"
style="#style/listViewItem"
android:background="#android:drawable/list_selector_background"
>
<View
android:id="#+id/item_separator"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="#dimen/lvDividerHeight"
android:background="#color/lvDividerColor"/>
<TextView
android:id="#+id/lv_item_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/listViewPrimaryDetail"
android:fontFamily="sans-serif-light"
android:ellipsize="marquee"
android:singleLine="true"
android:text="This is a test"
android:layout_alignParentLeft="true"
/>
<TextView
android:text="Of the Emergency Broadcast"
android:id="#+id/lv_item_subtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lv_item_header"
style="#style/listViewSecondaryDetail"
android:layout_above="#id/item_separator"
android:layout_alignParentLeft="true"
android:ellipsize="marquee"
android:singleLine="true"
/>
</RelativeLayout>
Finally, here is the activity_main xml
<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 -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#null"
android:dividerHeight="1dp"
android:background="#EEEEEE"/>
</android.support.v4.widget.DrawerLayout>
Here is the result of the layout inflation:
http://imgur.com/yWPp9ec
Please let me know if you have any advice.
I found the issue. It was in the style portion of my listViewItem.
<style name="listViewItem">
<item name="android:layout_height">#dimen/lvItemHeight</item>
<item name="android:clickable">true</item>
</style>
Changing android:clickable to false fixed the issue.