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>
Related
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 ) );
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
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
}
}
}
});
I am having trouble using ListActivity. If i change class extend from ListActivity to Activity it works fine. But then the trouble is how will i implement the onListItemClick. It muight be some small mistake i might have done but i am not able to trace it.
public class Warehouse extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Inside Warehouse");
setContentView(R.layout.warehous);
LinearLayout linear = (LinearLayout) findViewById(R.id.linearfullview);
drawable = setBitMap(background_image);
linear.setBackgroundDrawable(drawable);
String[] names = new String[] { "Inventory Ageing",
"Inventory Carring Cost", "Inventory Turns" };
LinearLayout linearList = (LinearLayout) findViewById(R.id.linearlist);
drawable = setBitMap(list_image);
linearList.setBackgroundDrawable(drawable);
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.warehouse_list,names));
// ListView lv = getListView(); lv.setTextFilterEnabled(true);
}
public Drawable setBitMap(String imagename) {
Bitmap image = BitmapFactory.decodeStream(getClass()
.getResourceAsStream(("/com/image/" + imagename + ".png")));
drawable = new BitmapDrawable(image);
return drawable;
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/linearfullview" android:layout_height="400sp"
android:layout_alignParentTop="true" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="180sp" android:layout_alignParentBottom="true"
android:layout_marginTop="20sp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/linearlist" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<ListView android:id="#android:id/list" android:layout_width="100dip" android:layout_height="wrap_content"></ListView>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="#+id/linearFirst"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="#+id/firstWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="#+id/linearSecond"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:layout_alignParentRight="true"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="#+id/secondWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="#+id/linearThird"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:layout_alignParentLeft="true"
android:visibility="invisible">
<com.widget.WheelView
android:id="#+id/thirdWheel" android:layout_height="wrap_content"
android:layout_width="200dp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
if you are extending ListActivity then you must take a Listview in main.xml with #+android:id/list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_width="fill_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
Can you use
public class Warehouse extends Activity implements onListItemClick
instead ?
refer http://www.vogella.de/articles/AndroidListView/article.html link.
There is a minor mistake ur making
Best of Luck...
I need a little help.I'm using a ViewFlipper to change views in TabActivity but not really sure how to do it.Here is my code :
MainActivity.class
public class Collections extends Activity implements OnClickListener {
ViewFlipper vFlip;
Button genre, recommended, soon;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collections_layout);
vFlip = (ViewFlipper) findViewById(R.id.tags_flipper);
genre = (Button) findViewById(R.id.genre_btn);
genre.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
recommended = (Button) findViewById(R.id.recom_btn);
recommended.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
soon = (Button) findViewById(R.id.soon_btn);
soon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
}
}
I have three different xml, which I want to load with clicking the buttons.Here is the collection_layout.xml
Collection_layout.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"
android:background="#FFFFFF">
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="fill_parent"
android:layout_height="65px"
android:background="#drawable/pink_bar"
android:tileMode="repeat"
android:orientation="vertical">
<ImageView
android:src="#drawable/stampii_logo"
android:id="#+id/stampii_logo_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="20px"/>
</LinearLayout>
<LinearLayout
android:id="#+id/first_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:background="#d229ce">
<Button
android:id="#+id/genre_btn"
android:text="By Genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
<Button
android:id="#+id/recom_btn"
android:text="Recommended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
<Button
android:id="#+id/soon_btn"
android:text="Expect Soon!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/flip"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<ViewFlipper
android:id="#+id/tags_flipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<include android:id="#+id/genre" layout="#layout/by_genre" />
<include android:id="#+id/recom" layout="#layout/recommended" />
<include android:id="#+id/soon" layout="#layout/expect_soon" />
</ViewFlipper>
</LinearLayout>
</LinearLayout>
So how can i do that?Thanks a lot!
Use ViewFlipper.setDisplayedChild