I have an List Item with ImageView's, TextView's. Issue is List Item as whole is not clickable, it's only when I click on any of the ImageView it's clickable. I have set android:descendantFocusability="blocksDescendants" for root and below for each child
android:focusable="false"
android:focusableInTouchMode="false"
Still only ImageViews are clickable. Below is my list item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
android:id="#+id/itemdisplaylist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:minHeight="#dimen/loclist_item_minumum_height"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/listitem_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="#+id/listitem_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#color/loclistitem_text"
android:textIsSelectable="true" >
</TextView>
<TextView
android:id="#+id/listitem_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="true" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/listitem_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#color/loclistitem_text"
android:textIsSelectable="true" />
<ImageView
android:id="#+id/listitem_prop1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="#+id/listitem_prop2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="#+id/listitem_prop3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/listitem_img3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:contentDescription="#null"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
</LinearLayout>
I am not sure where I am going wrong but struggling for last 6 hours.
Custom Adapter Code(Partial Coe):
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_listitem, parent,false);
holder.locName = (TextView) convertView
.findViewById(R.id.listitem_name);
holder.locDistance = (TextView) convertView
.findViewById(R.id.listitem_text2);
holder.locRating = (ImageView) convertView
.findViewById(R.id.listitem_img3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(this);
holder.locName.setText(data.get(position).getLocationName());
return convertView;
Please advise.
Solution
Finally I was able to do this by using Relative Layout instead of Linear Layout in my list item layout.
Add the click listener when you inflate the view only. Try this:
convertView = inflater.inflate(R.layout.list_listitem, null);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
convertView.setTag(holder);
Try using this on your Activity class:
listView.setItemsCanFocus(true);
or
listView.setItemsCanFocus(false);
which is useful for you.
I know its late but may be someone finds it helpful. In order to make the whole item as clickable instead of different items in a row use this
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Related
I have created a custom ListView with 2 ImageView and a TextView.
In the Adapter, I'm doing setText in TextView, but it is not visible in the app.
Here is my List:
<?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:background="#ffffff">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#289FA5"/>
<RelativeLayout
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular products"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp">
<LinearLayout
android:id="#+id/pop_pro_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catagories"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<ListView
android:id="#+id/cata_men"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
Here is my Custom List:
<LinearLayout
android:id="#+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">
<ImageView
android:layout_gravity="left"
android:id="#+id/imageView2"
android:layout_height="130dp"
android:layout_marginLeft="7dp"
android:layout_width="120dp"
android:src="#drawable/clothing_men"/>
<TextView
android:id="#+id/cata_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clothing"
android:layout_marginTop="35dp"
android:textStyle="bold"
android:textSize="27sp"
android:padding="10dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward_arrow"
android:layout_marginTop="42dp"/>
And Here is the getView method of my Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);
TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);
sub_catagory.setText(catagory[position]);
pic.setImageResource(image[position]);
Log.d("Strings========", sub_catagory.getText().toString());
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
Please Help as i am unable to find what is wrong in this.
Remove your layout_weight attribute from your TextView. If you remove sub_catagory.setText(catagory[position]); does "Clothing" appear when you run your app?
I got the answer now. I don't know how my textColor was set to White, because i didn't mentioned it. So, change the color to something else and it worked.And it wasted my whole day.
Sorry to disturb you all.
I have spent so much time searching for solutions and tried different ways but couldn't find where it goes wrong.
I am developing an app where I have an ImageButton as child element of a listView (I have done this using Custom Adapters). Upon executing OnClick of the ImageButton, nothing happens.
My fragment_tweets.xml looks as follows:
<?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.support.v4.widget.SwipeRefreshLayout android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvTweets" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
item_tweet.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
>
<ImageButton
android:layout_width="#dimen/profile_image_view_length"
android:layout_height="#dimen/profile_image_view_length"
android:background="#android:color/transparent"
android:id="#+id/ivProfilePicture"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvUserName"
android:textColor="#color/black"
android:layout_alignTop="#+id/ivProfilePicture"
android:layout_toRightOf="#+id/ivProfilePicture"
android:layout_marginBottom="#dimen/margin_10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="false"/>
<com.codepath.apps.twitterclient.helpers.LinkifiedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBody"
android:textColor="#color/black"
android:layout_toRightOf="#+id/ivProfilePicture"
android:autoLink="all"
android:layout_marginTop="#dimen/margin_30dp"
android:layout_marginBottom="#dimen/margin_10dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="false"/>
/>
<ImageView
android:layout_width="#dimen/margin_10dp"
android:layout_height="#dimen/margin_10dp"
android:id="#+id/ivClock"
android:layout_toLeftOf="#+id/tvTimeofPost"
android:src="#mipmap/ic_clock"
android:layout_marginTop="#dimen/margin_15dp"
android:layout_marginBottom="#dimen/margin_10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvTimeofPost"
android:textColor="#color/gray"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="#dimen/margin_10dp"
android:layout_marginRight="#dimen/margin_5dp"
android:layout_marginLeft="#dimen/margin_5dp"
android:layout_marginBottom="#dimen/margin_10dp"
android:textIsSelectable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ivPostImage"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_below="#+id/tvBody"
android:layout_alignLeft="#+id/tvBody"
android:layout_alignStart="#+id/tvBody"
android:layout_alignRight="#+id/tvTimeofPost"
android:layout_alignEnd="#id/tvTimeofPost"
android:layout_marginBottom="#dimen/margin_10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ivPostImage"
android:layout_alignLeft="#+id/ivPostImage"
android:layout_alignStart="#+id/ivPostImage"
android:layout_alignRight="#+id/ivPostImage"
android:layout_alignEnd="#id/ivPostImage"
android:weightSum="3">
<TextView android:id="#+id/tvReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/gray" android:layout_weight="1"
android:drawableLeft="#drawable/reply_small" android:layout_below="#+id/ivPostImage"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/tvRetweetCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#color/gray"
android:drawableLeft="#drawable/retweet_small"
android:layout_weight="1"
android:drawablePadding=".5dp"
android:layout_centerVertical="true"
android:textSize="#dimen/text_size_13sp" android:layout_below="#+id/ivPostImage"
android:layout_alignRight="#+id/tvReply"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/tvFavoriteCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textColor="#color/gray"
android:layout_weight="1"
android:drawableLeft="#drawable/favorite_small"
android:drawablePadding=".5dp"
android:textSize="#dimen/text_size_13sp"
android:layout_below="#+id/ivPostImage"
android:layout_alignRight="#+id/tvRetweetCount"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
</RelativeLayout>
getView() of Adapter looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Tweet tweet = getItem(position);
final ViewHolder viewHolder;
// If it's a retweeted tweet, display the original
if (tweet.getRetweeted() != null) {
tweetToDisplay = tweet.getRetweetedStatus();
} else {
tweetToDisplay = tweet;
}
if(convertView==null){
viewHolder = new ViewHolder();
LayoutInflater inflator = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.item_tweet, null);
convertView.setClickable(true);
convertView.setFocusable(true);
viewHolder.ivProfilePicture = (ImageButton) convertView.findViewById(R.id.ivProfilePicture);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.ivProfilePicture.setImageResource(0); // clear out the old image
// For Rounded image in profile picture
Transformation transformation = new RoundedTransformationBuilder()
.borderColor(Color.BLACK)
.borderWidthDp(1)
.cornerRadiusDp(30)
.oval(false)
.build();
Picasso.with(getContext()).load(tweet.getUser().getProfileImageUrl())
.transform(transformation)
.placeholder(R.mipmap.ic_launcher)
.into(viewHolder.ivProfilePicture);
Log.v("Tweets Adapter","Before onclick of profile pic");
viewHolder.ivProfilePicture.setTag(tweet.getUser().getScreenName());
viewHolder.ivProfilePicture.setFocusable(false);
viewHolder.ivProfilePicture.setFocusableInTouchMode(false);
viewHolder.ivProfilePicture.setClickable(true);
// View listener for showing the profile of user
viewHolder.ivProfilePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("Adapter ", "inside onclick method");
// Launch the profile activity directly from the fragment
listener.profileClicked(tweet.getUser());
}
});
return convertView;
}
I have already tried various solutions posted.. Any help would be great.. Thanks in advance..
In your else statement use this:
viewHolder = (ViewHolder) convertView.getTag();
convertView.setClickable(true);
convertView.setFocusable(true);
Your convertview is focusable and clickable when is drawing but not when is redrawing.
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 want to have a single "Textview" and 2 "Edittext" in a single row.I have tried the following code:
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/vegtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:id="#+id/vegquantity"
android:hint="Qty"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
/>
<EditText
android:id="#+id/vegprice"
android:layout_width="100dp"
android:layout_height="100dp"
android:hint="price"
android:layout_weight="1"
/>
</LinearLayout>
Getview in adapter:
#Override
public View getView(int position, View convertview, ViewGroup parent)
{
VeglistHolder holder;
// TODO Auto-generated method stub
if(convertview == null)
{
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview = inflater.inflate(R.layout.veglistrow, null);
holder = new VeglistHolder();
holder.text = (TextView) convertview
.findViewById(R.id.vegtext);
holder.Qtyedit = (EditText) convertview
.findViewById(R.id.vegquantity);
holder.priceedit = (EditText) convertview
.findViewById(R.id.vegprice);
convertview.setTag(holder);
}
else
{
holder = (VeglistHolder) convertview.getTag();
}
holder.text.setText(vegitems[position]);
return convertview;
}
But the Edittext is not there in the row. Also some blank space is coming below the "TExtview" .I searched for "Lots of" post regarding this but i cant solve this.Please help me to solve this..Thanks in advance.
Try to use weightSum in main layout for equal partition as below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
android:orientation="vertical" >
Use a Realative layout. Modify the attributes to suit your requirement
<?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:id="#+id/textView1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp"
android:text="TextView" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp" //set to whatever you desire
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="60dp"// //set to whatever you desire
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="70dp"
android:ems="10"
android:inputType="textPersonName" />
</RelativeLayout>
Well your problem is that you set the width and the height of the TextView to fill parent
I am not able to align a text below imageview in a listview. below is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:gravity="center"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textColor="#color/white"/>
</RelativeLayout>
and ListView is
<ListView android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="280sp"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/imgView"
android:scrollbars="none"
android:divider="#null"
android:gravity="center" >
</ListView>
java code to dynamically add text and image from array
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.gridview_layout, null);
holder = new ViewHolder();
holder.imageName = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageName.setText(name[position]);
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
not getting how to center text below imageview. please help me to solve this problem.
Simply use the layout_centerHorizontal attribute in your TextView:
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="#color/white" />
In textview change
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
You should use Linear layout to set orientation attribute. Set orientation to vertical for Linear layout and set imageview attribute android:layout_gravity="center"
Here is the complete code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/home" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cardiology"
android:layout_gravity="center"
android:layout_marginTop="5px"
android:textSize="20sp" >
</TextView>
</LinearLayout>
hope it will help