• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps List Item is not highlighted upon Selection (ListAdapter used)

gcc2878

Lurker
Aug 19, 2013
5
0
Hi,
I am trying to create a Dialog with list items in it with the help of List Adapter.

Dialog is working fine.

ISSUE:-
--> Selected list row is NOT Highlighted for whichever list item "TextView.setBackgroundColor" is used
--> Selected list row is Highlighted for whichever list item "TextView.setBackgroundColor" is NOT used

I want all the list items highlighted upon Selection irrespective of background color (i am setting background color as BLACK)


Please suggest on this .............................

MainActivity.java
[HIGH]
package com.example.testpro;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {

class Item {
String number;
int content;

public Item (String number, int content)
{
//this.number = "h";
//this.content = content;
}
}


public void myListDialog()
{
final Item[] items = {
new Item("Email", android.R.drawable.ic_menu_add),
new Item("Facebook", android.R.drawable.ic_menu_delete),
new Item("...", 0),//no icon for this one
new Item("...", 0)//no icon for this one
};

ListAdapter adapter = new ArrayAdapter<Item>(
MainActivity.this,
android.R.layout.select_dialog_item,
android.R.id.text1,
items){

public View getView(int position, View convertView, ViewGroup parent) {
//User super class to create the View
View v = super.getView(position, convertView, parent);
TextView myView = (TextView)v.findViewById(android.R.id.text1);



//--------------------ISSUE AREA --------------------------------------------------
if( (position == 0) || (position == 2) )
{
myView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.tick, 0, 0, 0);
myView.setText("No Highlight on Selection");

myView.setTextColor(getResources().getColor(R.color.blue));
myView.setBackgroundColor(getResources().getColor(R.color.grey));
}
if( (position == 1) || (position == 3) )
{
myView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.error, 0, 0, 0);
myView.setText("Highlight on Selection");
}
//--------------------ISSUE AREA --------------------------------------------------

//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
myView.setCompoundDrawablePadding(dp5);

return v;
}
};

new AlertDialog.Builder(MainActivity.this)
.setTitle("Choose Item")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(MainActivity.this,"Selected Choice: "+item,Toast.LENGTH_SHORT).show();
}
}).show();

//-------------------------------------------------------------------------
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button pdring_btn=(Button)findViewById(R.id.btnpdring);
pdring_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myListDialog();
}
});
}
}

[/HIGH]

strings.xml
[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="grey">#ACACAC</color>
<color name="blue">#0000BB</color>
<string name="app_name">testPro</string>
<string name="action_settings">Settings</string>

</resources>
[/HIGH]


activity_main.xml
[HIGH]
<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=".AndroidGPSTrackingActivity" >

<Button android:id="@+id/btnpdring"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Open Dialog"
android:textColor="#000000"
android:layout_centerInParent="true"/>

</RelativeLayout>

[/HIGH]
 
I am little confused on this, from what I understand you want all of the items highlighted when selected but your getView only seems to want to highlight those items at the positions you have stated.

Furthermore you will need to trigger the getview when the items are selected and to do this you will need to use a onItemclick method if the listadapter has such a method and when this method is triggered, then you will need to use notifyDataSetChanged() which then refreshes the adapter and will call the getview again.

Although I had issues with using getview I know you can use it. Read my post here :

http://androidforums.com/application-development/770370-listview-onitemlongclick-getview-solved.html



Thanks

TimCS
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones