Unable to set image to android gridview.setEmptyView - android

I have written a grid view that is working fine.
I need an image when gridview is having no item.
I tried to add it like
gridView.setEmptyView((ImageView)findViewById(R.drawable.empty));
empty.png is the image in the res/drawable
but imageView is not getting showed when grid View is empty.
The full code
oncreate
setContentView(R.layout.main_activity);
GridView gridView = (GridView) findViewById(R.id.listhabbit_ma);
gridView.setEmptyView((ImageView)findViewById(R.drawable.empty));
habbitListAdapter = new HabbitListAdapter(AppManager.getHabbits());
gridView.setAdapter(habbitListAdapter);
habbitListAdapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, HabbitDetail.class);
intent.putExtra("position", position);
startActivityForResult(intent, habbit_delete);
main.xml
<GridView
android:id="#+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
I am unable to figure out the error. Should I define a image view at the end of main.xml?
edit1
full layout
<?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:gravity="left"
android:orientation="vertical"
android:background="#color/background">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/user_profile"
android:orientation="horizontal"
android:layout_weight="0.07">
<TextView
android:id="#+id/textView1_ma"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/user_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2_ma"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|right"
android:text="#string/repotation"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<GridView
android:id="#+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<Button
android:id="#+id/add_habbit_button_ma"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/user_profile"
android:onClick="onAddButtonClick"
android:text="Add new Habbit"
android:layout_weight="0"/>
</LinearLayout>

You are not doing it right. Image != ImageView. The proper way, for example, is the following (very simplified):
ImageView iView = new ImageView(mContext);
//set layout params if needed
iView.setImageResource(R.drawable.empty);
((ViewGroup)gridView.getParent()).addView(iView);
gridView.setEmptyView(iView);
You can also declare it at your XML layout as #VipulPatel shown

Following layout and code works fine :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<TextView
android:id="#+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
Code :
GridView gridView = (GridView) findViewById( R.id.listhabbit_ma );
TextView tvNoDataFound = (TextView) findViewById(R.id.empty_list_view);
tvNoDataFound.setVisibility(View.VISIBLE);
gridView.setEmptyView( findViewById( R.id.empty_list_view ) );

Related

GridView with image and text

I'm very new in android and need a little help with this task.
I have gridview witch pull images and text from database and display them. Currently is displayed like image and on the right of the image is text. I want to make text under the image. Can you help me with this task. This is the gridview.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2" >
</GridView>
</LinearLayout>
And this is how I display image and text in it table_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Name"
/>
</LinearLayout>
</LinearLayout>
And if need getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.table_column, null);
}
// ColPhoto
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setPadding(10, 10, 10, 10);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText("" + MyArr.get(position).get("name").toString());
return convertView;
}
u can use relative layout and set align textview to imageview.
try this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ColName"
android:layout_alignRight="#+id/ColName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ColPhoto"
android:gravity="center"
android:text="Name name" />
</RelativeLayout>
I don't know why add a single element LinearLayout in another LinearLayout.
if grid item need to be put in correct position, I'd like use RelativeLayout instead.
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ColPhoto"
android:text="Name"
/>
</RelativeLayout>
`
Try to change your orientation to "vertical".
EDIT:
Like, Tr4X said you don't need second LinearLayout. You can put both in one.
<?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:background="#drawable/bckimageviw" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>

How to add buttons to grid view?- Android

I am developing a simple android application and i want to add two buttons to grid view. one button should be in the top of the grid view and other one be in bottom. But the i had a problem grid view not display properly. please help me to correct this ,
this is my xml file,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3b3b3b" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:text="Back"/>
<GridView android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
<Button android:id="#+id/btnCapturePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:layout_gravity="center"
android:layout_marginBottom="10dp"/>
</LinearLayout>
this is the java file,
public class HorsePictureAlbum extends Activity{
Button btnCapturePicture;
Button btnBack;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_album);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent camara = new Intent(getApplicationContext(), Camera.class);
startActivity(camara);
}
});
}
}
Hi Check the below code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:text="Back" />
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<Button
android:id="#+id/btnCapturePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:text="Camera" />
</LinearLayout>
</LinearLayout>
<?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" >
<Button
android:id="#+id/up_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Up button" />
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:layout_below="#+id/up_button"
android:layout_above="#+id/down_button">
</GridView>
<Button
android:id="#+id/down_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Down button"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

CursorAdapter onitemClick of listview

Hi I am developing an android app with CursorAdapter for loading the listview. onItemClick of the listview item, I am trying to start a dialogue activity.
adapter = new ConversationAdapter(MainActivity.this, cursor);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
Cursor cur = (Cursor) adapter.getItem(position);
cur.moveToPosition(position);
id = cur.getString(cur.getColumnIndexOrThrow("_id"));
Intent intent = new Intent(MainActivity.this, DialogueActivity.class);
Bundle bundle = new Bundle();
bundle.putString ("id", id);
intent.putExtras(bundle);
startActivity(intent);
}
});
I am using only the startActivity without a finish(). The problem is the listview which is to be displayed at back disappears and there is just a black screen. Not sure what I need to do so that listview is displayed in background of DialogueActivity.
onItemClick of the listview item is displayed as below
main_activity.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=".EaseSms" >
<LinearLayout
android:id="#+id/linearlayoutlistSMS"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".88"
android:orientation="vertical"
android:paddingBottom="3dp" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#000000"
android:dividerHeight="5dp" >
</ListView>
<TextView
android:id="#+id/Nosms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No SMS to display"
android:textColor="#FFFFFF"
android:textSize="28dp"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutCreateMessage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".12"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/createmsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/newcreatesms"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
and dialogue_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayoutdeleteall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:background="#drawable/cal_sms_bg"
android:gravity="center|left"
android:orientation="vertical"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/newdeleteconversation"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
please Help. Thanks!
Try this codes..
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
before setContentView(R.layout.activity); in DialogueActivity.class

Grid item not clickable

I have used one gridview calendar in my application.The gridview calendar is working correctly and displayed the element what i want.My problem is when i click the grid item this will not be clickable.Thanks
My Gridview:
<LinearLayout
android:id="#+id/gridlayoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="#+id/gridviewid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:horizontalSpacing="-1px"
android:numColumns="7"
android:stretchMode="columnWidth"
android:textDirection="anyRtl" />
</LinearLayout>
My Grid Rows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#drawable/gridcal"
android:orientation="vertical">
<TextView
android:id="#+id/griddate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:textColor="#color/text_color"
android:textSize="14sp"
android:textStyle="bold" >
<!-- android:layout_height="200dp" -->
</TextView>
<TextView
android:id="#+id/gridstatus"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
My Activity:
gridview = (GridView) view.findViewById(R.id.gridviewid);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//some codes
}
}
}
});

android gridview fit the size in to screen

The gridview of my application has an image and a text below it for each grid.
I need to make sure that the complete list of grid fit in to the screen (no overflow at bottom).
Here is how i have defined my gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:background="#drawable/home_bg"
/>
and each grid item is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="0dip"
android:gravity="center_horizontal"
android:layout_below="#+id/grid_item_image"
android:layout_alignParentBottom="true"
>
</TextView>
</LinearLayout>
I have tried several ways to set the grid view layout param, but i am not getting the right output. Please help me here. the gridview adapter code is
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
convertView.setLayoutParams(new GridView.LayoutParams(h-50, h-20));
//holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY); ......
Try this:-
grid_item.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">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center">
<ImageView android:id="#+id/imgIcon" android:src="#drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:text="TextView" android:id="#+id/txtDesc" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
gridview.xml
<?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"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/mobGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
try
android:layout_width="fill_parent"
android:layout_height="fill_parent"
for your GridView instead of wrap_content!

Categories

Resources