Listview items text is not showing complete - android

I'm trying to create an activity for Admin to approve some Posts(by user) to publish, so I'm using Listview where all posts will be checked/unchecked. Now the issue is that some posts have large text and this big text is not showing properly in listview row.
I'm new to software development I searched all solutions, one is to use custom listview but I'm not getting it.
here are my files:
approvepost.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"
android:background="#android:color/transparent"
tools:context=".ApprovePost"
android:layout_marginTop="25dp">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="20dp"
android:paddingBottom="10dp"
/>
</RelativeLayout>
This is the main class...
ApprovePost.java
package com.ahmed.signup;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class ApprovePost extends AppCompatActivity {
public static final String TAG = "ListView";
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.approvepost);
listView = (ListView)findViewById(R.id.listView);
// CHOICE_MODE_NONE: (Default)
// (listView.setItemChecked(..) doest not work with CHOICE_MODE_NONE).
// CHOICE_MODE_SINGLE:
// CHOICE_MODE_MULTIPLE:
// CHOICE_MODE_MULTIPLE_MODAL:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemClick: " +position);
CheckedTextView v = (CheckedTextView) view;
boolean currentCheck = v.isChecked();
ApprovePostDto user = (ApprovePostDto) listView.getItemAtPosition(position);
user.setActive(!currentCheck);
}
});
this.initListViewData();
}
private void initListViewData() {
ApprovePostDto user1 = new ApprovePostDto("Ali", "I would like to set up a meeting to give an overview of Foxit and demo the features important to your workflows.");
ApprovePostDto user2 = new ApprovePostDto("Haider", "See the data in your Google Account and choose what activity is saved to personalize your Google experience");
ApprovePostDto user3 = new ApprovePostDto("Usman", "Your account storage is shared across Google services, like Gmail and Photos");
ApprovePostDto[] users = new ApprovePostDto[]{user1,user2, user3};
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_checked , users);
listView.setAdapter(arrayAdapter);
ProgressDialog progress;
progress = new ProgressDialog(this);
progress.setTitle("Please Wait!!");
progress.setMessage("Wait!!");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
for(int i=0;i< users.length; i++ ) {
listView.setItemChecked(i,users[i].isActive());
progress.show();
}
}
}
```

If the layout of your list items (android.R.layout.simple_list_item_checked) does not work very well for large texts you can always create your own layout.
TextView and CheckedTextView have some attributes that will help:
android:lines="2" // use more lines
android:ellipsize="marquee" // scroll your text horizontally
app:autoSizeTextType="uniform" // makes your text smaller/larger depending on available space
app:autoSizeMinTextSize="11sp"
app:autoSizeMaxTextSize="14sp"
PS: Set your ListView width and height to match_parent

Use this in initListViewData method
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_1 , users);
instead
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_checked , users);
2nd Suggestion
Create new layout have the below code in it. Refer this layout in your adapter that would work for you.
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"/>

Related

How to know which button user clicked and its text and id in Android Studio?

Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.

How to parse an integer and it gives a list of courses in android studio?

am developing an android application. My problem statement is as follows:
1) I need to have a ListView of courses available at some colleges.
2) I the points for each of them(e.g medicine:30,maths:24,English:14,French:28,etc...)
3) I need to parse the points(integer) to corresponding courses and lower courses than that.
Full sample:
Enter your points:(say integer 26 )(edittext)
After the person added his point he gets a list of courses he is likely to apply at the college like that:
You are eligible to apply for these courses:
maths
english(to be listed in as a listview)
P.s am using android studio.
can someone help me programmatically? thanks
mainactivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] courses = new String[]{
"Mathematics",
"Chemical Engineering",
"Civil Engineering",
"Agriculture",
"Law and Management",
"Sociology",
"Information and Communication Technologies",
"Computer Science",
"Information Systems",
"Software Engineering",
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event2icon);
EditText editText = null;
editText=(EditText)findViewById(R.id.edit_text);
final String point=editText.getText().toString();
final int point_integer=Integer.parseInt(point);
final ListView courselist = (ListView)findViewById(R.id.courseNames);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, courselist, false);
courselist.addHeaderView(header, null, false);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, courses);
courselist.setAdapter(adapter);
courselist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
if((Integer.parseInt(String.valueOf(point_integer))<=10))
{
}
}
});
}
}
event2icon.xml
<?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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_text"
android:text="Enter your points:"/>
<ListView
android:id="#+id/courseNames"
android:layout_width="match_parent"
android:layout_below="#+id/edit_text"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:dividerHeight="15.0sp"
>
</ListView>
</LinearLayout>
listviewheader.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
you need to parse points into integer and then by pass that integer to query to get list of eligible courses ;get point_integer from below code and use it into your query for database or arraylist and set aadapter to listview for eligible courses list related to that point
String point=edittext.getText().toString.trim();
int point_integer=Integer.parseInt(point);

ListView onItemClickListener works on part of custom row only

I have a custom ListView where each row consists of 3 items, a TextView (title), an ImageView (placeholder icon), and another TextView (content text). I have an OnItemLongClickListener set on the ListView and it is supposed to be that when a user clicks on the item in the ListView (which should be the entire row of 3 items all together as one), then a dialog comes up giving them the option to delete the whole item, which would then delete all 3 parts of that single list item.
But long clicking on the title view and the image do not trigger the listener. Only if the content TextView is long clicked, does the dialog box come up, which then deletes all 3, but clicking anywhere in the row should do that. However it doesn't. I need to find a solution because the user will not know to only click on the content TextView, they should be able to click anywhere.
I have looked on here to find a solution, and have tried adding these lines, but nothing has worked:
android:descendantFocusability="blocksDescendants" added to my ListView's LinearLayout.
android:clickable="false" to my ImageView.
android:focusable="false" and android:focusableInTouchMode="false" to both TextViews.
I don't have anything else to try. Any ideas?
UPDATE
When I added extra lines, like #Amrit suggested (code in his answer), the whole area when long clicked does now bring up the dialog box, but it creates this strange tint on the area I click, but only if I click the title TextView or the ImageView area. Oddly, that 2nd TextView still looks good and produces the dialog box as it should. Not sure how to get rid of this misaligned tint though:
TextTab.java
package org.azurespot.cutecollection.texttab;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import org.azurespot.R;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class TextTab extends Fragment {
private ArrayList<PoemListItem> poems = new ArrayList<>();
private ListViewPoemAdapter adapter;
private ListView listView;
String[] allSDCardFiles = null;
StringBuilder text;
PoemListItem wordsFromFile;
File[] files;
PoemListItem sampleItem;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.text_tab, container, false);
adapter = new ListViewPoemAdapter(getActivity(), poems);
// Attach the adapter to a ListView
listView = (ListView) v.findViewById(R.id.text_list_view);
listView.setAdapter(adapter);
if(adapter.getCount() == 0) {
// load contents of SD card
loadSDCard();
}
setupListViewListener();
return v;
}
private void loadSDCard(){
try {
// gets directory CuteWords from sd card
File cuteWordsDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOCUMENTS), "/Cute Words");
if (!cuteWordsDir.exists()){
cuteWordsDir.mkdir();
}
if (cuteWordsDir.isDirectory()) {
// lists all files in CuteWords, loads in Files[] array
files = cuteWordsDir.listFiles();
for (File singleFile : files) {
//Read text from file, put each line into StringBuilder
text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(singleFile));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
// get full file name with ext. and text in file
wordsFromFile = new PoemListItem(singleFile.getName(), text.toString());
adapter.add(wordsFromFile);
adapter.notifyDataSetChanged();
}
}
}
// get number of files in CuteWords directory
allSDCardFiles = new String[files.length];
// create a blank String version of PoemListItem
sampleItem = new PoemListItem(" ", " ");
// add the default icon/lines remaining to ArrayList (through adapter),
// if less than 9 files on SD card
for (int i = 0; i < (9 - allSDCardFiles.length); i++) {
adapter.add(sampleItem);
}
adapter.notifyDataSetChanged();
} catch(IOException e){
e.printStackTrace();
}
}
// so you can edit any of the list items
private void setupListViewListener() {
// to delete a list item
listView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> aView, View item,
final int pos, long id) {
if (adapter.getItem(pos) != sampleItem) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete")
.setMessage("Delete these cute words?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// delete from ArrayList first
poems.remove(pos);
adapter.notifyDataSetChanged();
// get file name then delete it
String name = files[pos].getName();
File file = new File(Environment.getExternalStorageDirectory(),
"/Documents/Cute Words/" + name);
file.delete();
// clear list and adapter
poems.clear();
adapter.clear();
adapter.notifyDataSetChanged();
// after each item delete, must refresh load with new arrangement
loadSDCard();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
return true;
}
});
}
}
ListViewPoemAdapter
package org.azurespot.cutecollection.texttab;
import android.content.Context;
import android.text.InputType;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.azurespot.R;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class ListViewPoemAdapter extends ArrayAdapter<PoemListItem> {
private TextView poemText;
private TextView poemTitle;
private ImageView poemPlaceholder;
public ListViewPoemAdapter(Context context, ArrayList<PoemListItem> poems) {
super(context, 0, poems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
PoemListItem poemListItem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.text_listview_row, parent, false);
}
poemTitle = (TextView) convertView.findViewById(R.id.text_title);
poemText = (TextView) convertView.findViewById(R.id.text);
poemPlaceholder = (ImageView)convertView.findViewById(R.id.icon_placeholder_poem);
poemText.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
poemText.setMovementMethod(new ScrollingMovementMethod());
poemPlaceholder.setBackgroundResource(R.drawable.ic_poem_placeholder);
poemPlaceholder.setScaleType(ImageView.ScaleType.CENTER_CROP);
poemPlaceholder.setLayoutParams(new LinearLayout.LayoutParams(150, 150));
poemTitle.setText(poemListItem.getTitle());
poemText.setText(poemListItem.getPoem());
return convertView;
}
}
text_tab.xml
<?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:descendantFocusability="blocksDescendants"
android:background="#2198bb">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_list_view"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:divider="#null"/>
</LinearLayout>
text_listview_row.xml
<?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="wrap_content"
android:layout_alignParentTop="true"
android:paddingBottom="20dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:id="#+id/icon_placeholder_poem"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_title"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="25sp"
android:textStyle="bold|italic"
android:hint="Title"
android:ellipsize="start"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top"
android:maxLines="10"
android:inputType="textMultiLine"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:textSize="20sp"
android:ems="10"
android:textStyle="italic"
android:hint="Quote or poem, here."
android:ellipsize="start"/>
<!--Line in-between the rows-->
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#7e8287"
android:paddingTop="20dp" />
</LinearLayout>
Set all child views inside listView items to not focusable or clickable.
android:focusable="false"
android:clickable="false"
If it is not enough try setting
android:descendantFocusability="blocksDescendants
to text_listview_row.xml linearlayout &
android:textIsSelectable="false"
to textview's inside text_listview_row.xml
UPDATE
Actually all that I needed was that one line android:descendantFocusability="blocksDescendants" but inside of my LinearLayout parent of the text_listiew_row.xml (not needed in text_tab.xml). Thank you!
Try setting the below tag fro your views inside list item view xml (text_listview_row.xml)
android:focusable="false"
so that list item click will work always perfectly
I think the problem lies in this line:
if (adapter.getItem(pos) != sampleItem) {
Add log before to verify that method was invoked successfully.
Log.d("TextTab", "onItemLongClick");
if (adapter.getItem(pos) != sampleItem) {
...
This isn't your case, but I had similar problem with click on a custom layout item. After a hour of looking for solution a found out that I set android:inputType to TextView inside my layout file which was blocking onClick() listener
(no idea why)
Don't use android:inputType with TextView.
It worked perfectly for me
Asked by many, The TextViews in list should not have width match parent.
Even if you set the "Focusable" to false it wont work.
Set the TextView Width to wrap_content
<TextView
android:id="#+id/itemchild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...

How do I get the ID of the item of row (each row has 3 items) from a ListView, which contains a clickable items (imagebuttons)?

I read many articles about this problem in stackoverflow but I didn't solve my problem. I have ListView that have rows, each row has 3 imagebuttons, when I click at my imagebutton code in "setOnItemClickListener" doesn't work.
I need which item(imagebutton) in rows was clicked. In my ListView will be many rows.
I hear about simpleCursorAdapter, fragmentlist, cursorloader, but my min required SDK is 10.
In this code I used 6 images but I going to use more images(100).
My java code :
package foxstrot.ghp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class images extends Activity {
int[] images = {R.raw.a2, R.raw.a3, R.raw.im2, R.raw.a4, R.raw.a5, R.raw.im6,};
ListView lv;
Intent i = new Intent(this, king.class);
final String LOG_TAG = "L";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images);
lv = (ListView) findViewById(R.id.lv);
ArrayList<Map <String, Object>> data = new ArrayList<Map<String, Object>>(images.length);
Map<String, Object> m;
for(int i = 0; i < images.length; i++){
m = new HashMap<String, Object>();
m.put("image1", images[i]);
m.put("image2", images[i+1]);
m.put("image3", images[i+2]);
i = i + 2;
data.add(m);
}
String[] from = {"image1","image2","image3"};
int[] to = {R.id.ib1, R.id.ib2, R.id.ib3};
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.a_, from, to);
lv.setAdapter(sAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
i.putExtra("id", id);
startActivity(i);
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
+ id);
}
});
}
}
MY XML CODE (CONTAINER WHICH USED IN ADAPTER)
<?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="125dp"
>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ib1"
android:layout_weight="1"
android:scaleType="fitXY"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ib2"
android:layout_weight="1"
android:scaleType="fitXY"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ib3"
android:layout_weight="1"
android:scaleType="fitXY"
/>
</LinearLayout>
setOnItemClickListener() is used to detect clicks on the list ITEMS (rows) not on specific buttons you may have on those items (rows).
For each button of the row on the xml you will need to add an onClick attribute with the function to handle the click: android:onClick="myFunction".
The on your activity you will have to create a function:
public void myFunction(View v) {...}
If you need to pass specific info about which row was clicked you can add a tag on the button's view on your adapter's getView() method. For more information on this check here: How do I get the row ID of the row from a ListView, which contains a clickable item?
In your images.xml layout file, set your ImageButtons's clickable and focusable attributes to false. The taps on the buttons would now pass-through and get handled by the ListView.
<ImageButton
android:id="#+id/ib1"
...
android:clickable="false"
android:focusable="false" />

Change text color of simple ListView Multiple choice Android

I am developing an app for cricket. My requirement is like this, if i select team 1 the list of available country name has to display and if i select a country name as India the list of player from India has to displayed and in that i have select a multiple players from that. I have done everything. But my problem is i am using android.R.layout.simple_list_item_multiple_choice for selecting players. I am using simple list view and the background of that list is black image. And my listview is like that
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8.5"
android:cacheColorHint="#00000000"
/>
Now the problem is the listview value is showing black in color. Already i have black background image. And the value also black in color. So its not looking good. How to change the color of listview values to white without changing o custom adapter.
And this is my adapter class
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,playersName);
lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvview.setAdapter(adapter);
You have to create Custome TextView to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_multiple_choice to ArrayAdapter you should pass custom list item XML, which has different TextColor attribute.
For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF00FF"
/>
Then passed it to adapter like as below:
new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName);
EDITED:
Here is the code which is working fine i have tested.
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName));
lv.setBackgroundColor(Color.BLACK);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3)
{
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lv.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) lv.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}
});
Layout of listview
<ListView
android:id="#+id/android:list"
android:layout_marginTop="60dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
/>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...other styles
//add this
<style name="ListFont" parent="#android:style/Widget.ListView">
<item name="android:textColor">#FFFFFF</item>
</style>
...other styles
</resources>
at layout xml put this attribute style="#style/ListFont" to listview
check below code:
package com.example.mapsdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
public class MainActivity extends Activity {
ArrayList<String> a = new ArrayList<String>();
private ListView lView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillarray();
lView = (ListView) findViewById(R.id.listView1);
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, a));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lView.setOnItemClickListener(new OnItemClickListener() {
private String my_sel_items;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// List list = new ArrayList();
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) lView.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}
});
}
private void fillarray() {
// TODO Auto-generated method stub
a.clear();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add("e");
}
}
your result in Log
03-26 12:25:06.106: V/values(3301): Selected Items,a
03-26 12:25:06.810: V/values(3301): Selected Items,a,b
03-26 12:25:07.466: V/values(3301): Selected Items,a,b,c
03-26 12:25:08.136: V/values(3301): Selected Items,a,b,c,d
Edited:
check this link you can use whatever font colour & listview background colour in this code.
Luksprog solution is acceptable and not difficult. But without the line
if (position == 1)

Categories

Resources