I have a grid view that have 8 images
Now on a particular image view i want to set a text value that will be changed dynamically
Main XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/blue_bar"
android:orientation="horizontal" >
</LinearLayout>
<GridView
android:id="#+id/gridView1"
android:layout_below="#+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
**Inner XML Layout **
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Code Main Activity
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, DASHBOARD_LINKS));
Adapter Class
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] dashBoardValues;
public ImageAdapter(Context context, String[] dashBoardValues) {
this.context = context;
this.dashBoardValues = dashBoardValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from dashboard_inner.xml
gridView = inflater.inflate(R.layout.dashboard_inner, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.ic);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return dashBoardValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
So how could i do this in a grid view .Please help me .
As others have already suggested you can use a Relative Layout or FrameLayout
<?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/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="20dp"
android:text="TextView" />
</RelativeLayout>
Snap
Check this answer kcoppock which will give you an idea of how to use framelayout
Placing/Overlapping(z-index) a view above another view in android
Also it is better to use a ViewHolder Pattern
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Here is actual working code
public class MainActivity extends Activity {
private static final String TAG = "AAA";
private static Handler mHandler;
private LayoutInflater layoutInflater;
private GridView mGridView;
private int click = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layoutInflater = LayoutInflater.from(getApplicationContext());
click = 0;
setContentView(R.layout.my_grid_view);
mGridView = (GridView)findViewById(R.id.my_grid_view);
mGridView.setAdapter(new MyAdapter());
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterViews, View pView, int position, long id) {
click++;
TextView textView = (TextView)pView.findViewById(R.id.my_text_view);
if(textView!=null){
textView.setText("Changed : Position = " + position + " : Click No = " + click);
}else{
Log.d(TAG, "Fish : textView = " + textView);
}
}
});
}
private class MyAdapter extends BaseAdapter{
#Override
public int getCount() {
return 200;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){ //This is memory efficient, but after certain no of time u will get similar view repetition,
//to over come that overwrite getViewTypeCount() & provide proper way of persistent view & data management mechanism.
convertView = layoutInflater.inflate(R.layout.child_one, null);
}
return convertView;
}
}}
And XMLs
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
And
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/my_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/flower_1" />
<TextView
android:id="#+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#android:color/darker_gray"
android:text="Default Text" />
</FrameLayout>
instead of image view pass framelayout reference. it will work.
framelayout is view group which sub class of view only.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView android:scaleType="fitXY"
android:src="#drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image_view"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_gravity="top|right"
android:background="#android:color/darker_gray"
android:layout_height="wrap_content"
android:id="#+id/badge_view"
android:text="10"/>
</FrameLayout>
snap shot
Get this image and add to your project drawble. new http://www.flickr.com/photos/113556524#N05/11793569304/
This is the screen shot nw http://www.flickr.com/photos/113556524#N05/11793569304/
This your xml code to use :
<?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:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
</LinearLayout>
In your activity java file use this :
package com.example.dashboard;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2,b4,b3,b5,b6,b7,b8;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.neww);
b1 =(Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b5 = (Button) findViewById(R.id.button7);
b6 = (Button) findViewById(R.id.button8);
/* Wat ever you want validation , anything do here */
// if you need to set text to button
b1.setText("Hi pooja");
// blah blah blah - wat ever you want to any button text
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Got it ;)
Use FrameLayout as child of GridView. Within FrameLayout first put ImageView & on top of it put a TextView as mentioned by "sush".
Then you can change Visibility & content of TextView on demand, through program.
It is not working for you because probably for all TextViews of you have put same "android:id" put different id for different TextView. It is bound to work. I've tested.
http://i.stack.imgur.com/HUY4o.png Check the sample image...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/cover"
android:layout_width="148dp"
android:layout_height="148dp"
android:src="#drawable/empty_photo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cover"
android:background="#70000000"
android:padding="6dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
---> Use this layout and inflate it in your adapter.You will get the text at bottom of image.
Related
I have got a custom listview. I am trying to add items to the custom listview by clicking on the button. When I click the button, I want the items to show one under the other but it shows a big space between items. Data read from the database are listed in the listview at the same time.it works like chat applications. Thanks for your help
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
tools:context=".Mainactivity">
<ListView
android:id="#+id/listid"
android:layout_width="match_parent"
android:divider="#drawable/dessen"
android:layout_above="#+id/lineerid"
android:dividerHeight="3dp"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:id="#+id/lineerid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="0dp"
android:layout_marginBottom="3dp"
android:background="#91f1f1f1"
android:orientation="horizontal"
android:paddingBottom="2dp">
<EditText
android:id="#+id/mesajid"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/gonderid"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/sendMessageButton"
android:layout_weight="0.72"
android:ems="10"
android:maxHeight="80dp" />
<Button
android:id="#+id/gonderid"
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:background="#drawable/buttonsekil"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Gönder" />
</LinearLayout>
</RelativeLayout>
custom _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="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nickid"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/tarihid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/formesajid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:textColorLink="#9C27B0"
android:textSize="16sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</LinearLayout>
custom adapter
public class customadapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<Model> modelList;
public customadapter(Activity activity,List<Model> modelList) {
layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.modelList=modelList;
}
#Override
public int getCount() {
return modelList.size();
}
#Override
public Object getItem(int position) {
return modelList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
view=layoutInflater.inflate(R.layout.custom_layout,null);
TextView nick =view.findViewById(R.id.nickid);
TextView tarih=view.findViewById(R.id.tarihid);
TextView mesaj=view.findViewById(R.id.formesajid);
Model model=modelList.get(position);
nick.setText(model.getNickname());
tarih.setText(model.getTarih());
mesaj.setText(model.getMesaj());
return view;
}
}
<LinearLayout
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
>
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/backicon_round"/>
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/color_green"
android:text="Error"
android:textSize="19dp"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:layout_marginBottom="25dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3">
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now, I set layout for title on the top. Then, a Imageview, a textview and a listview follow below this layout, and only 1 textview is inside listview.
listview can vary in size.
The problem is, if the size of the listview is very big, I can only scroll the screen assigned to listview.
But, I want scroll the entire screen.
To solve this problem, I added scrollview outside of the first linearlayout.
However, It didn't work.(the imageview disappeared)
What can I do?
you can make your listview addheaderview,and put your imageview and textview into headerview
yourlayout.xml
<?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:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp">
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Error"
android:textColor="#000000"
android:textSize="19dp"
/>
</RelativeLayout>
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
lv_header.xml
<?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="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="20dp" />
Activity.class
public class MainActivity extends AppCompatActivity {
private ListView list_question_detail_expComments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_question_detail_expComments = (ListView) findViewById(R.id.list_question_detail_expComments);
View lvHeaderView = View.inflate(this,R.layout.lv_header,null);
list_question_detail_expComments.addHeaderView(lvHeaderView);
list_question_detail_expComments.setAdapter(new LvAdapter());
}
private class LvAdapter extends BaseAdapter{
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView tv = new TextView(MainActivity.this);
tv.setText("test"+i);
return tv;
}
}
}
In the below code how to scroll not just the ListView, but all of the Views with it? If I am adding full page scrollview then listview not showing full page.
But top menu should be constant. Inside search button I want to scroll full 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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#027fdb"
android:id="#+id/linearlayout1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="Contact -"
android:id="#+id/textView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_mylead_listcount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_alignRight="#+id/textView"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/lin_symbols"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_marginRight="20dp">
<LinearLayout
android:id="#+id/lin_addmechines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right">
<ImageView
android:id="#+id/img_addmachines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_machine"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lin_addmechines"
android:layout_gravity="center_vertical"
android:gravity="right"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/img_addfilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/filter"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/search"
android:hint=" Search"
android:paddingLeft="5dp"
android:imeOptions="actionSearch"
android:inputType="text"
android:id="#+id/textSearch"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin_contactPersons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:id="#+id/img_contactprs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/contactprs"
/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<ListView
android:id="#+id/users"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Use this function to full view list on scroll view.
ListView listViewObject;
// after binding list view.
getListViewSize(listViewObject);// pass your listview object here.
public void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
Hope! it is helpful to you...
You should copy the layouts, you want to scroll with the listview in a different layout XML and put it to the top of the listview as a different first item. You can do it with a custom ListAdapter:
//WARNING: I did not try this code, handle it like pseudocode
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<Item> mData = new ArrayList<>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Item item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size() + 1;
}
#Override
public Item getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(position==0){
//initialize your custom view
}else{
//initialize a normal list item
}
return convertView;
}
}
Check this ListAdapter example, if you still have questions!
I want to be able to click on a button inside an item of a ListView. It should have a different effect from clicking the whole item. I realize there are several questions asked on stackoverflow, but none of the suggestions works for me.
The ListView is inside a Fragment.
Layout of the fragment:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EventFragment" >
<ListView
android:id="#+id/event_list"
android:background="#C0FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</RelativeLayout>
Layout of each list item:
<?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="wrap_content"
android:orientation="vertical"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My list adapter is:
public class EventAdapter extends ArrayAdapter<Event> {
static class ViewHolder {
TextView separator;
LinearLayout relativeLayout;
TextView eventHeader;
TextView eventDescription;
ImageView blueDot;
Button button1;
Button button2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.event_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.event_list_element);
viewHolder.blueDot = (ImageView) convertView.findViewById(R.id.event_list_element_icon);
viewHolder.eventHeader = (TextView) convertView.findViewById(R.id.event_list_element_firstLine);
viewHolder.eventDescription = (TextView) convertView.findViewById(R.id.event_list_element_secondLine);
viewHolder.button1 = (Button) convertView.findViewById(R.id.event_list_element_button1);
viewHolder.button2 = (Button) convertView.findViewById(R.id.event_list_element_button2);
viewHolder.separator = (TextView) convertView.findViewById(R.id.event_list_separator);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
final Event item = getItem(position);
if (item != null) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(_context, "boo!", Toast.LENGTH_SHORT).show();
}
};
viewHolder.button1.setOnClickListener(listener);
}
return convertView;
}
}
The problem is that the two buttons are not clickable. What I tried to far:
ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
I also tried setting on the button:
android:focusable="true"
android:clickable="true"
I also experimented with android:descendantFocusability.
None of my tries made the buttons clickable.
Insert the attribute android:descendantFocusability="blocksDescendants" in the Parent Layout declaration of each list item.
The xml should be as follows:
<?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="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I found the solution! I wanted to update the list of events periodically (right now it's a thread running every x milliseconds, later I want to switch that to only update the event list when there is a change). Anyway, the code was (inside my main activity):
private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_EVENT)) {
Bundle bundle = intent.getExtras();
Event event = (Event) bundle.get("event");
showEvent(event);
}
}
};
private void showEvent(final Event event){
final Context context = this;
runOnUiThread(new Runnable() {
public void run() {
final ListView listview = (ListView) findViewById(R.id.event_list);
EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
listview.setAdapter(adapter);
}
});
}
Setting the adapter each time is certainly not the right approach. Once I changed that to only set the adapter once, it worked like suggested using android:descendantFocusability="blocksDescendants"
I have a list view inside a ViewSwitcher that works fine in android version 2.2 but it does not show in my device which has android 4.1.2 version.
I also debugg it and dont see any errors, and also checked to see if the list I pass to adapter is not null, and saw that it was not empty.
I have another list view which is located in a TabHost that is being shown correctly.
Regards
Edited:
Here is my code:
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewswitcher"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:layout_gravity="center|top"
>
<TextView
android:id="#+id/dateLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/lblChooseaDate" />
<DatePicker
android:id="#+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center" />
<Button
android:id="#+id/btnChooseDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/startdayexpensebtn" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
<TextView
android:id="#+id/descLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/lbldaydescription" />
<EditText
android:id="#+id/dayDesctiptionText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:singleLine="false"
android:minLines="1"
android:maxLines="3"
android:inputType="textMultiLine" />
<Button
android:id="#+id/addnewItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:enabled="false"
android:text="#string/btnAddnewItem" />
<LinearLayout
android:id="#+id/listcontaioner"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scrollbars="vertical"
>
<ListView
android:id="#+id/android:list"
android:divider="#bababa"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/btnlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:paddingRight="10dp"
android:paddingLeft="10dp" >
<Button
android:id="#+id/cancelbtn"
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btncancel" />
<Button
android:id="#+id/savebtn"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btnAdd" />
</LinearLayout>
</LinearLayout>
</ViewSwitcher>
also for each row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp" >
<ImageView
android:id="#+id/expenselisitem_imgaetype"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="35dp"
android:src="#drawable/fail" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/expenselisitem_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/expenselisitem_category"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenselisitem_desc"
android:paddingBottom="6dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/expenselisitem_amount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/expenselisitem_category"
android:layout_alignBottom="#+id/expenselisitem_category"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:paddingBottom="6dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
and here is the adapter:
public class CustomAddExpenseItemListAdapter extends BaseAdapter {
private ArrayList<ExpenseItem> _list=new ArrayList<ExpenseItem>();
private final Activity _context;
private static LayoutInflater inflater=null;
public CustomAddExpenseItemListAdapter(Activity activity,ArrayList<ExpenseItem> data){
_list = data;
_context=activity;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return _list.size();
}
public Object getItem(int position) {
return position;
}
static class ViewHolder {
protected TextView _cexpenseDesctiption;
protected TextView _cAmount;
protected ImageView _cType;
protected TextView _cCategory;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=null;
if(convertView==null)
{
inflater = _context.getLayoutInflater();
vi = inflater.inflate(R.layout.expense_item_list_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder._cexpenseDesctiption= (TextView) vi.findViewById(R.id.expenselisitem_desc);
viewHolder._cAmount = (TextView) vi.findViewById(R.id.expenselisitem_amount);
viewHolder._cType=(ImageView)vi.findViewById(R.id.expenselisitem_imgaetype);
viewHolder._cCategory=(TextView)vi.findViewById(R.id.expenselisitem_category);
vi.setTag(viewHolder);
}
else
{
vi = convertView;
}
ViewHolder holder = (ViewHolder) vi.getTag();
ExpenseItem item;
item = _list.get(position);
holder._cexpenseDesctiption.setText(item.get_description());
holder._cAmount.setText(String.valueOf(item.get_amount()));
if(item.get_type()==0)
{
holder._cCategory.setText(item.get_category().get_title());
holder._cType.setImageResource(R.drawable.downarrow);
}
else
{
holder._cCategory.setText("-");
holder._cType.setImageResource(R.drawable.uparrow);
}
return vi;
}
}
Usually this is solved by wrapping the ListView inside a LineaLayout or RelativeLayout but depending on the problem you've countered the solution might be something entirely different.
Here is a working example of a ViewSwitcher that switches between a ListView and TextView.
From the following XML text you can see that ListView has been wrapped in a RelativeLayout together with a Button that enables the switch between the views(layouts).
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/viewswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Show Next" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/btn_next"></ListView>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:text="HEllO WORLD">
</TextView>
<Button
android:id="#+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Previous" />
</LinearLayout>
</ViewSwitcher>
</RelativeLayout>
And here is the Java code that delivers the magic:
public class MainActivity extends Activity {
ViewSwitcher mViewSwitcher;
ListView listview;
String[] values = new String[] { "Hello1", "Hello2", "Hello3",
"Hello4", "Hello5", "Hello6", "Hello7", "Hello8",
"Hello9", "Hello10", "Hello11", "Hello12", "Hello13", "Hello14",
"Hello15"};
Button shownext;
Button showprevious;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewSwitcher = (ViewSwitcher)findViewById(R.id.viewswitcher);
listview = (ListView) findViewById(R.id.listview);
shownext = (Button)findViewById(R.id.btn_next);
showprevious = (Button)findViewById(R.id.btn_previous);
shownext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mViewSwitcher.showNext();
}
});
showprevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mViewSwitcher.showPrevious();
}
});
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This has been tested successfully on Android 4.1.2.
Hope that helps!