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
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();
}
});
}
}
strings.xml
<?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>
activity_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"
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>