My ListView gets filles by this
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/projectImage"
android:maxHeight="70dp"
android:maxWidth="70dp"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/projectTitle"
android:textStyle="bold"
android:focusable="false"
android:clickable="false"
android:textSize="15dp"
android:text="projecttitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/projectTopic"
android:focusable="false"
android:clickable="false"
android:textSize="14sp"
android:text="projectTopic"
android:layout_marginTop="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/projectdeadline"
android:textSize="14sp"
android:text="projectdeadline"
android:layout_marginTop="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="projectTaskCount"
android:id="#+id/projectTaskCount"
android:layout_marginTop="3dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
It contains one ImageView and three TextViews.
If I click on the ImageView I want to start my Activity Example1, else open next Activity WiFi.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent(this, WiFi.class);
in.putExtra("projectFileNamesMap", projectFileNamesMap.get(position));
in.putExtra("position", position);
in.putExtra("sessionId", sessionId);
startActivity(in);
}
How can I solve my Problem?
Summarized:
If I click on ImageView open Example1 Activity.
If elsewhere clicked, then open WiFi Activity.
Hope everybody can understand this.
Kind Regards!
use image.setOnclickListener and view.setOnItemCLickListener inside getView method to perform click on image view and whole view.
First set your image:
android:clickable="true" //setOnClickListener makes a view clickable if it doesn't have that as a default but use it anyway.
then:
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Log.v(TAG, " click");
}
});
pls tell me if this works
Write OnClickListener for ImageView when creating a new Instance for each ImageView elements for the listView in the Adapter's getView method
Also make sure before you addView's to the Linearlayout you set appropriate LayoutParams.
public View getView(final int position, View convertView, ViewGroup parent) {
TextView txtOne, txtTwo, txtThree;
ImageView iv;
LinearLayout layout;
if(convertView == null){
layout = new LinearLayout(context);
txtOne = new TextView(context);
txtTwo = new TextView(context);
txtThree = new TextView(context);
iv = new ImageView(context);
} else{
layout = (LinearLayout) convertView;
txtOne = (TextView) layout.getChildAt(0);
txtTwo = (TextView) layout.getChildAt(0);
txtThree = (TextView) layout.getChildAt(0);
iv = (ImageView) layout.getChildAt(0);
}
layout.addView(txtOne);
layout.addView(txtTwo);
layout.addView(txtThree);
layout.addView(iv);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your handling here
}
});
}
Related
I have a custom list view which I recently upgraded to use View Holder. Right now the project works fine but the images in the two Image Views don't show on the screen.
This two black icons appear "transparent" but work if I click them.
This is the layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgInfo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_weight="0.04"
android:visibility="visible"
app:srcCompat="#drawable/details64" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imgInfo"
android:layout_toLeftOf="#+id/imgEdit"
android:layout_toRightOf="#+id/imgInfo"
android:layout_weight="0.24"
android:orientation="vertical">
<TextView
android:id="#+id/txtChamado"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="25sp" />
<TextView
android:id="#+id/txtSolicitante"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
<TextView
android:id="#+id/txtItemDeCatalogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:id="#+id/imgEdit"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_weight="0.03"
android:contentDescription=""
app:srcCompat="#drawable/pencil64" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
This is the code snippet:
#Override
public View getView(final int position, View view, final ViewGroup parent) {
View convertView;
ViewHolder holder;
if (view == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.customized_list_view, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
convertView = view;
holder = (ViewHolder) convertView.getTag();
}
// Object declaration
final Sette setteData = getItem(position);
//region Put the Tasks values to the textviews
// Get me two strings, one with the older value and one with the newest
String oldChamado = holder.txtChamado.getText().toString();
String newChamado = setteData.getChamado();
// Do not repeat the string in the textview
if (!(oldChamado.equals(newChamado))) {
holder.txtChamado.setText(newChamado);
}
//region Click Listener for the button that leads to Approvals Activity
if (!aBoolean) {
holder.imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamadoClicked = setteData.getChamado();
solicitanteClicked = setteData.getSolicitante();
itemDeCatalogoClicked = setteData.getItemDeCatalogo();
id = setteData.getId();
Intent intent = new Intent(context.getApplicationContext(), ApprovalsActivity.class);
intent.putExtra("Id", id);
intent.putExtra("chamadoClicked", chamadoClicked);
intent.putExtra("solicitanteClicked", solicitanteClicked);
intent.putExtra("itemDeCatalogoClicked", itemDeCatalogoClicked);
context.startActivity(intent);
}
});
}
//endregion
return convertView;
}// END METHOD
ViewHolder (which I got from an internet example):
public class ViewHolder {
TextView txtChamado;
TextView txtSolicitante;
TextView txtItemDeCatalogo;
ImageView imgInfo, imgEdit;
Typeface bebasKai;
Typeface london;
public ViewHolder(View view){
bebasKai = Typeface.createFromAsset(context.getAssets(), "fonts/bebaskai.otf");
london = Typeface.createFromAsset(context.getAssets(), "fonts/london.ttf");
txtChamado = (TextView) view.findViewById(R.id.txtChamado);
txtChamado.setTypeface(bebasKai);
txtSolicitante = (TextView) view.findViewById(R.id.txtSolicitante);
txtSolicitante.setTypeface(london);
txtItemDeCatalogo = (TextView) view.findViewById(R.id.txtItemDeCatalogo);
txtItemDeCatalogo.setTypeface(london);
imgEdit = (ImageView) view.findViewById(R.id.imgEdit);
imgInfo = (ImageView) view.findViewById(R.id.imgInfo);
}
}
I tried clean, rebuild, download the project from BitBucket again, reinstall the app from scratch on my phone, invalidade cache/restart and check the code.
Am I missing something obvious here?
**EDIT:**Thank you SaNtoRiaN, I knew it was something simple that I was missing.
Change app:srcCompat attribute in your ImageView to android:src to view your images properly. To know more about the difference, check this answer.
I'am trying to click a button on my list view to update that row first of all I tried to click on the item in the list view to show toast.
I did setOnItemClickListener of the ListView
public class Medlistview extends Activity{
DB db = new DB(this);
ImageButton updateimagebutton = (ImageButton)findViewById(R.id.editbuttonimage);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medlistview);
populatemedlistview();
}
public void populatemedlistview(){
Cursor cursor = db.getMEDINF();
String[] medinfo = new String[]{
DB.col1,DB.col2,DB.col3,DB.col4,DB.rowID
};
int[] mappingmed = new int[]{
R.id.MEDid,R.id.MedName,R.id.Medpurpose,R.id.medNotaplet,R.id.ROWID
};
SimpleCursorAdapter medcursoradapter = new SimpleCursorAdapter(this,R.layout.medlistviewxml,cursor,medinfo,mappingmed);
ListView medlistview = (ListView)findViewById(R.id.listViewmed);
medlistview.setAdapter(medcursoradapter);
medlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Medlistview.this,"clicked",Toast.LENGTH_LONG).show();
}
});
}
}
I made sure that my list view is clickable and focusable but still not working
One more thing I already define image button on layout item not on ListVew layout every time I run the app, is crashing and showing that null reference message in the log-cat do I need to define the button also on the ListView layout.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
My xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="282dp"
android:id="#+id/listViewmed"
android:layout_weight="0.40"
android:background="#c7c0c0"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
Here is my list item xml layout file.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/MEDid"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/MedName"
android:layout_below="#+id/MEDid"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/Medpurpose"
android:layout_below="#+id/MedName"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/medNotaplet"
android:layout_below="#+id/Medpurpose"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editbuttonimage"
android:layout_above="#+id/Medpurpose"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/editicon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/ROWID"
android:layout_alignBottom="#+id/medNotaplet"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Thanks in Advance.
Implement View.OnClickListener() to your ImageButton in getView() method in your adapter, not in Medlistview activity. And use callback or eventBus to work with click action in your activity.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.recycler_view_item, parent, false);
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button was clicked
}
});
return convertView;
}
Set
android:focusable="false"
android:focusableInTouchMode="false"
for all UIs controls in your custom layout file.
use android:descendantFocusability="blocksDescendants" inside listview,
check out this answere: can't click on listview row with imagebutton
Set the parent layout of the item
android:descendantFocusability="blocksDescendants"
and set
android:clickable="false"
android:focusable="false">
on all your widgets.
This is my GridView ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<!-- gridview adapter layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/gridview_background_selector" >
<!-- profile picture layout -->
<RelativeLayout
android:id="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<!-- profile picture image view -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_profile_picture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/facebook_blank_profile" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/background_black_transparent" >
<!-- user name text view -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="#color/orange" >
<HorizontalScrollView
android:id="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<!-- event name -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_event_name"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textAppearance="#android:style/TextAppearance.Small"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal" >
<!-- event icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_gravity="right"
android:layout_margin="2dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_action_event" />
<!-- event start time -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/black"
android:textSize="12sp"
app:isBold="true" />
</LinearLayout>
</RelativeLayout>
<!-- attending event -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_attending_mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="#color/background_black_transparent"
android:contentDescription="#string/app_name"
android:padding="30dp"
android:src="#drawable/v"
android:visibility="gone" />
</RelativeLayout>
and i'm setting an onItemClickedListener:
private void setGridView()
{
//create a new event list
picoEventsList = new ArrayList<PicoEvent>();
//create new gridview arrayadapter
arrayAdapter = new EventDetailsArrayAdapter(getActivity(), picoEventsList);
//set grid's view empty view
View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_events_empty_view, null);
gridView.setEmptyView(emptyView);
gridView.setOnItemClickListener(this);
gridView.setAdapter(arrayAdapter);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//get clicked event
PicoEvent event = arrayAdapter.getItem(arg2);
//create new event details fragment
EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();
//add the event to fragment's arguments
eventDetailsFragment.setArguments(PicoEventCreator.createBundle(event));
//show the event details fragment
getMainActivity().showFragment(null, eventDetailsFragment, true, true);
}
but the onItemClick() method doesn't get fired..
I've checked and non of my views in my layout has clickable="true" setted...
Any ideas ?
Hey guyz finally got a solution...
what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.
So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)
so what i m trying to say is:
public View getView(int position, View convertView, ViewGroup parent) {
//Here row is a view and we can set OnClickListener on this
final View row;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
//Here we inflate the layout to view (linear in my case)
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
//Now get the id or whatever needed
row.setId(position);
// Now set the onClickListener
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Clicked" + row.getId() + "!!",
Toast.LENGTH_SHORT).show();
}
});
return row;
}
Ok i found what the problem was
Apparently GridView and HorzonitalScrollView are not such good friends...
probably because the HorzonitalScrollView has onClick funcionality..
The solution i found is to put:
android:descendantFocusability="blocksDescendants"
in my ArrayAdapter root view.
In my xml I have:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#FFFFFF" android:id="#+id/listItemMainRelativeLayout">
<RelativeLayout android:id="#+id/myFavourtiesPlaylistTextRelativeLayout"
android:layout_centerVertical="true" android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:layout_marginBottom="1dp"..... android:textColor="#000002"
android:layout_width="wrap_content"></TextView>
<TextView android:visibility="visible" .... android:id="#+id/myFavouritesPlaylistDescription"
android:textColor="#000006"></TextView>
</RelativeLayout>
<RelativeLayout android:id="#+id/myFavourtiesPlaylistButtonRelativeLayout"
android:layout_alignParentRight="true" android:layout_centerVertical="true"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:visibility="gone">
<ImageButton android:id="#+id/playListPlayButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:visibility="visible" android:background="#drawable/play_button"
android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:focusable="false" android:focusableInTouchMode="false"/>
</RelativeLayout>
</RelativeLayout>
My Java Code is:
public OnItemClickListener listOnItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
if (previousSelectedListItem != -1) {
Toast.makeText(getActivity(),
"Hidding Position:" + previousSelectedListItem,
Toast.LENGTH_LONG).show();
RelativeLayout mainRelativeLayout = (RelativeLayout) v;
RelativeLayout buttonsRelativeLayout = (RelativeLayout) mainRelativeLayout
.findViewById(R.id.myFavourtiesPlaylistButtonRelativeLayout);
buttonsRelativeLayout.setVisibility(View.GONE);
}
Toast.makeText(getActivity(), "ShowingPosition:" + position,
Toast.LENGTH_LONG).show();
RelativeLayout mainRelativeLayout = (RelativeLayout) v;
RelativeLayout buttonsRelativeLayout = (RelativeLayout) mainRelativeLayout
.findViewById(R.id.myFavourtiesPlaylistButtonRelativeLayout);
buttonsRelativeLayout.setVisibility(View.VISIBLE);
previousSelectedListItem = position;
}
};
Expected Behavior: I want to make myFavourtiesPlaylistButtonRelativeLayout in the item visible once it is clicked.
What is Happening: myFavourtiesPlaylistButtonRelativeLayout of clicked item remain hidden, but a few other random items become visible instead.
I believe having a RelativeLayout inside of a ListView is the problem. I have wasted 6h of my day today on a simmilar issues.
ListView element not clickable
Try reading this thread, it should provide the fix.
http://code.google.com/p/android/issues/detail?id=3414
I have a custom list view which contains two text views and on button.
I have customized the button style.
In the list view each row is not fit to show my button properly.
I want to change the list item's height.
How can i do that..?
This is my list view.
<ListView android:layout_width="fill_parent"
android:scrollbars="none" android:footerDividersEnabled="false"
android:minHeight="50dp"
android:listSelector="#00000000" android:dividerHeight="0px"
android:divider="#00000000" android:layout_height="fill_parent"
android:layout_marginLeft="5dp" android:layout_marginTop="5dp"
android:layout_marginRight="5dp" android:id="#+id/list_view"
android:cacheColorHint="#00000000" android:background="#00000000" />
my custom view xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="Subtitle 2" android:id="#+id/text1"
android:layout_marginLeft="5dp"
android:singleLine="true"/>
<TextView android:layout_width="60dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="$ 00.00" android:id="#+id/text2"
android:singleLine="true"/>
<Button android:layout_width="80dp" android:layout_height="25dp"
android:background="#drawable/type10_subbg" android:text="Add"
android:textSize="20dp" android:textStyle="bold" android:textColor="#153E7E"
android:id="#+id/add" />
</LinearLayout>
Here is my getView.
#SuppressWarnings("unchecked")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
pos = position;
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.type10_sub, null);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.add = (Button) convertView.findViewById(R.id.add);
holder.add.setTag(String.valueOf(position));
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
int tag = Integer.valueOf((String)b.getTag());
System.out.println("Added at "+tag);
System.out.println("Data = "+((HashMap<String,Object>)list.get(tag)).get("text1").toString());
System.out.println("Price = "+((HashMap<String,Object>)list.get(tag)).get("text2").toString()); }
}); holder.text1.setText(((HashMap<String,Object>)list.get(position)).get("text1").toString());
holder.text2.setText(((HashMap<String,Object>)list.get(position)).get("text2").toString());
convertView.setTag(holder);
return convertView;
}
Thanks in advance....!
convertedview.setlayoutparams(new Listview.setlayoutparams(width, height));
First put LinearLayout instead of ListView
and then edit that to ListView otherwise it will not work. This is the trick.
set minHeight in R.layout.type10_sub elements
Its very easy to hardcode in a new height for the custom view simply specify the layout_height for the highest level item, in this case the LinearLayout. Below I've shown how to set it to 100dp
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="100dp">
....
</LinearLayout>
In linear layout give android:layout_height="100dp" instead of wrap_content
use:
if(convertView == null){
convertView = inflater.inflate(R.layout.type10_sub, parent);
}