List view first element no clickable - android

I have a list of element in a fragment.
I want to disable the title of list
http://bugs.haploid.fr/file_download.php?file_id=856&type=bug
here are the code the cell of my list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/stations_listView_layout">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/header_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingBottom="5dip"
android:layout_marginTop="29dip"
android:text="A"
style="#style/FragmentTitleSecondary">
</com.infrabel.railtime.views.RobotoMediumTextView>
<View
android:id="#+id/header_bar"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#color/fragment_title_color">
</View>
<RelativeLayout
style="#style/stations_listView_inner_layout"
android:layout_width="fill_parent"
android:layout_height="60dip">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:textSize="#dimen/menu_list_item_primary_text_size"
android:textColor="#color/grey_normal">
</com.infrabel.railtime.views.RobotoMediumTextView>
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="25dip"
android:textSize="#dimen/fragment_title_text_size"
android:textColor="#color/grey_light">
</com.infrabel.railtime.views.RobotoMediumTextView>
</RelativeLayout>
</LinearLayout>
i tried to set android:clickable = false or android:focusable="false" and it doesn't work.

One way to make certain ListView item not clickable is:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// FIRST ITEM - DO NOTHING
} else {
// DO SOMETHING IF YOU WISH...
}
}
});
another way would be to modify your custom adapter:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.item,
listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView v = (TextView) super.getView(position,
convertView, parent);
if(position == 0) {
v.setClickable(false);
// havent tested this thou...
}
}
});

got it , i have in my cell.xml
so the text will be drawn every time when i add cell.
we must declare this in the main xml, than change the text from our adapter:
String firstLetter = station.getTitle().substring(0, 1);
if (position == _alphaIndexer.get(firstLetter)) {
TextView header = (TextView) activity.findViewById(R.id.header_textview);
header.setText(firstLetter);
}

Related

Why is my listview not expanding to accommodate a larger row size?

I have a listview that I want the user to be able to delete items from. If the user holds on a row long enough, the row's checkbox is made visible. However, the listview doesn't grow to accommodate the (now taller) row despite there being plenty of space. I had this issue before when the listview was in a scrollview but that's no longer the case. I understand I can reformat the text as a bandage solution, but I want to understand what is happening.
Here is the listview before and after the first entry is held. You can see the listview's height stays the same and doesn't expand to accommodate the two entries:
Here is the listView:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Here is the listView item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns="https://"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<CheckBox
android:id="#+id/deleteCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="5dp"
android:textColor="#android:color/darker_gray"
android:textSize="18sp" />
</LinearLayout>
Here is how the layout is being inflated in my custom adapter:
public class CustomAdapter extends ArrayAdapter<CustomlistEntry> {
public CustomAdapter(Context context, ArrayList<CustomlistEntry> entries) {
super(context, 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_custom_listview, parent, false);
}
CustomlistEntry entry = getItem(position);
TextView tvNumber = convertView.findViewById(R.id.number);
TextView tvDate = convertView.findViewById(R.id.date);
tvNumber.setText(entry.getNumber());
tvDate.setText(entry.getDateString());
return convertView;
}
}
and finally the adapter being set:
CustomAdapter adapter = new CustomAdapter(App.getApp(), arrayListSorted);
final ListView listView = activity.findViewById(R.id.listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
arg1.findViewById(R.id.deleteCheckBox).setVisibility(View.VISIBLE);
CustomAdapter adapter = (CustomAdapter)listView.getAdapter();
return true;
}
});
listView.setAdapter(adapter);

Setting a List to include 2 TextViews

I have a list adapter that holds a TextView and an ImageView. I can set the image and the text fro each item on the list. The variable for TextView is tvFullName.
Can I have tvFullName to set an item in the list to a text and have tvFullNameOne
also set the item in the list to a text?
Like two variables which each create items in the list with text?
This is my layout.
<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:padding="5dp" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/profile" />
<TextView
android:id="#+id/tvFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingLeft="5dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Adapter........
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.relationship_inflater, null);
Holder holder = new Holder();
//connectOrDisconnectUser();
holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName);
holder.tvFullName.setText(usersInfo.get(position).get(
Relationship.TAG_USERNAME));
imageLoader.DisplayImage(
usersInfo.get(position).get(Relationship.TAG_PROFILE_PICTURE),
holder.ivPhoto);
return view;
}
What I tried made the two items I set as the same ListView therefore had the same onClick value
If you want the click events on the view elements within the list, you can use individual listeners.
For example, with your current code
holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
// holder.ivPhoto.setOnClickListener();
holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName);
// holder.tvFullName.setOnClickListener();
Or you can check which view was clicked in the item listener set on the listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
switch(viewId) {
case R.id.ivImage:
Toast.makeText(MainActivity.this, "Image clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.tvFullName:
Toast.makeText(MainActivity.this, "Full name clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
As for your question, yes, of course you can add whatever extra text you want.

How to get a textView value from ListView adapter onClick button inside same view?

I have a list View which contains imageView working as button and a TextView.
what I want is when I click on the button for example for the third item in the list view I want to get the text that in the textview when I click on the button which is in the same position as the text view..
I'll try with
final TextView txt= (TextView) findViewById(R.id.txt);
String txt2 = txt.getText().toString();
That's work more and less.
my problem it's so strange, I will try to explain with images
When I click on the button irrespective of the position, allways it return the textview which display on the fisrt position on the screen (not on listview, on screen)
for example
On that situation, (red square its screen) if I clicked on the button from the item 1,2 or 3 it return tv_1.
other example (again red square its screen)
Now, if I click on the button from the item 2,3 or 4 it return tv_2
that is, always it displays the top of the list of visible item on screen
My code
public void btn(View view)
{
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
Any suggestions?
UPDATE
The problem its because I need to use final variable to get only that textview, then its report first one item is displayed on screen, but... why? or how to solve?
UPDATE 2 MY XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:layout_marginRight="26dp"
android:id="#+id/lay_fondo">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/cardview_notify"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="#color/primary_green"
android:id="#+id/lay_prioridad"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="580dp"
android:layout_height="match_parent"
android:layout_weight="0.54">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:id="#+id/tv_id"
android:layout_gravity="center_vertical"
android:textColor="#color/black"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="26dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/img_favorito"
android:src="#drawable/star"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_onClick="btn"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
Try to replace the below line
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
with
TextView tv_id = (TextView) ((View) view.getParent()).findViewById(R.id.tv_id);
The above line finds textview with in the given view,
OR
Add an OnItemClickListener() for and list view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
});
Try to set onItemClickListener to your listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView txt= (TextView) view.findViewById(R.id.txt);
String txt2 = txt.getText().toString();
Log.i(LOGTAG, "" + txt2);
}
});
OR
Set click listener for TextView inside your
getView(int position, View convertView, ViewGroup parent)
my suggestions;
tv_id is referencing only one item (R.if.tv_id). for this matter wherever you put your button, it will always call the code handled by tv_id.
I suggest you reference the list view and setonclicklistener on the listView.
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
Why do you want to get the text in so complicated way?
I assume you have your custom ListView adapter (e.g. MyAdapter) and your items (e.g. MyItem) with a getter (e.g. MyItem :: getText(), don't mind C++ style).
Then your approach could be:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Probably this check is not that necessary but better safe than sorry
if (listView.getAdapter() instanceof MyAdapter) {
MyAdapter adapter = (MyAdapter)listView.getAdapter();
String text = adapter.getItem(position).getText();
// Do whatever you need with the text
}
}
});

multiple elements within same drawable

I'm trying to add several items within a single Drawable which would be used in a GridView:
- text aligned at the center of the drawable
- image at the background
- a programmatically variable number (0-3) of images (stars) ALIGNED horizontally at the bottom of the drawable.
How could I accomplish this please?
At the moment I'm trying to use a custom ImageView and set the background image using setImageResource. An alternative would be to use a TextView, however I still have no clue about the stars at the bottom.
Any help would be appreciated!
George.
You don't mean Drawable, you mean to say a View. You simply have to find a decent tutorial about making custom views for AdapterView (ListView, GridView, ...).
One such tutorial to get you started is http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
Quick dump of the code:
main.xml
<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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}

Android ListView not detecting selection

I'm stumped. This one IS simple and I'm missing something simple but I'm just not getting anywhere.
I have a ListView that contains pretty simple items - TextView's that contain text of the form "N. name", where N is the position in the list and name is the name of the associated objects. I've created a ListAdapter that presents that information into the ListView.
The presentation stuff seems to work fine. It's the part where when I tap on a row that's not working. I just don't get a notification. If I set a onClickListener on the item (when it's generated in the Adapter) I do get a notification, but it's convoluted to make that work the way I'd like it to. I've tried a bunch of stuff - changing descendent focusability, enabling, disabling, setting itemsCanFocus to false, ... - but no luck on any of them.
I've included relevant code for configuring the view and the elements in the ListView.
In particular, what I'm trying to do is get that OnItemClickLister.onItemClick() to get invoked when I tap on a row. Any thoughts on what I'm doing wrong?
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = FZMModel.getInstance(this);
setContentView(R.layout.activity_deploy);
TextView viewTitle = (TextView) findViewById(R.id.deploy_header);
if (model.getProfiles().size() == 0) {
viewTitle.setText(R.string.deploy_header_create);
} else {
viewTitle.setText(R.string.deploy_header_choose);
}
profileListView = (ListView) findViewById(R.id.profileListView);
profileListAdapter = new ProfileListAdapter(this, model);
profileListView.setAdapter(profileListAdapter);
profileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.d(TAG, String.format("Item %d selected (with id=0x%x)", position, id));
}
});
}
Adapter:
... relevant code from getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Profile profile = model.getProfileAtIndex(position);
final int rowNum = position;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.profile_list_layout, parent, false);
}
TextView profileIndexView;
TextView profileNameView;
profileIndexView = (TextView) convertView.findViewById(R.id.profileIndexLabel);
profileNameView = (TextView) convertView.findViewById(R.id.profileNameLabel);
profileIndexView.setText(String.format("%d.", position));
profileNameView.setText(profile.getName());
// convertView.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// Toast.makeText(context, String.format("Did select row #%d (index=%d)",rowNum, profile.getPosition()), Toast.LENGTH_SHORT).show();
// }
// });
return convertView;
}
activity layout:
<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"
tools:context="com.nsn.flexizonemobile.FZMConcreteActionDeploy"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/deploy_header_choose"
android:id="#+id/deploy_header"
android:layout_margin="10dp"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileListView"
android:layout_gravity="left|top"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:choiceMode="none"
android:layout_below="#+id/deploy_header"
android:divider="#android:color/darker_gray"
android:dividerHeight="2dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants" />
</RelativeLayout>
profile_list_layout layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="#dimen/profile_index_width"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="A. "
android:id="#+id/profileIndexLabel"
android:gravity="center_vertical|right"
android:layout_centerVertical="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="24sp"
android:textIsSelectable="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile Name"
android:id="#+id/profileNameLabel"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/profileIndexLabel"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textSize="24sp"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</RelativeLayout>
in the textview of your inflator layout just add:
android:focusable="false"
android:focusableInTouchMode="false"
and in parent layout of your inflator i.e in relative layout of inflator add:
android:descendantFocusability="blocksDescendants"
remove :
android:descendantFocusability="beforeDescendants"
from your listview. and set height of your listview to match_parent will solve your issue.
remove following attributes from your ListView:
android:choiceMode="none"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
also
android:layout_width="wrap_content"
android:layout_height="wrap_content"
makes no sense in this context (especially height)

Categories

Resources