-2

I have successfully created a listview that contains all the options (text). However I would like to add unique icons next to each of the options. How can I go about doing this, with my excising code?

Here is what I am trying to achieve:

enter image description here

Here is my code: AccountSettingsActivity.java

//All Options in Account Settings
private  void setupSettingsList(){
    ListView listView = findViewById(R.id.lvAccountSettings);

    ArrayList<String> options = new ArrayList<>();
    options.add(getString((R.string.editProfile)));
    options.add(getString(R.string.notifications));
    options.add(getString(R.string.privacy_settings));
    options.add(getString(R.string.security));
    options.add(getString(R.string.ads));
    options.add(getString(R.string.help));
    options.add(getString(R.string.about));
    options.add(getString(R.string.logout));

    ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options);
    listView.setAdapter((adapter));
}

listview_row_adjustmnet.xml (This simply changes the text color and size of the options in the listview)

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listViewAdjustment"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:textSize="17sp"
    android:gravity="fill"
    android:textColor="@color/white"/>

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Joe
  • 263
  • 5
  • 20
  • 1
    this is a good example of what you need https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial – Azhagthott Jan 31 '20 at 04:10
  • 1
    You have to use a custom adapter for this purpose. You will have to create a custom adapter and set the adapter to the list view. – Pronoy999 Jan 31 '20 at 05:50
  • 1
    Does this answer your question? [Custom Adapter for List View](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – Pronoy999 Jan 31 '20 at 05:51
  • Does this answer your question? [A custom ListView with a custom adapter in Android Studio](https://stackoverflow.com/questions/28406084/a-custom-listview-with-a-custom-adapter-in-android-studio) – Harpreet Jan 31 '20 at 08:07

3 Answers3

0

Create Array in /res folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="images">
        <item>@drawable/img1</item>
        <item>@drawable/img2</item>
        <item>@drawable/img3</item>
    </array>

</resources>

update Account Setting:

    //All Options in Account Settings
    private  void setupSettingsList(){
        ListView listView = findViewById(R.id.lvAccountSettings);

TypedArray images = getResources().obtainTypedArray(R.array.images);

        ArrayList<String> options = new ArrayList<>();
        options.add(getString((R.string.editProfile)));
        options.add(getString(R.string.notifications));
        options.add(getString(R.string.privacy_settings));
        options.add(getString(R.string.security));
        options.add(getString(R.string.ads));
        options.add(getString(R.string.help));
        options.add(getString(R.string.about));
        options.add(getString(R.string.logout));
    // add images to adapter
        ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options, images);
        listView.setAdapter((adapter));
    }

Add Image View in listview_row_adjustmnet.xml

in Adapter set the image:

imageView.setImageResource(images.getResourceId(i, -1));
Junaid
  • 1,301
  • 1
  • 15
  • 21
0

Use a data model with title and icon like below

class Data {

 public int imageId;
 public String txt;

 Data(Drawable imageId, String text) {

   this.imageId = imageId;
  this.txt = text;
}

}

Data menuItemSearch = new Data(ContextCompat.getDrawable(this, R.drawable.ic_search_orange), resources.getString(R.string.title))

and when set drawable in image use this one

yourImage.setImageDrawable(data.imageId)
Avinash
  • 867
  • 4
  • 11
0

Firstly, You have to add in your listview_row_adjustmnet.xml.

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal|top"/>
<TextView
    android:id="@+id/listViewAdjustment"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:textSize="17sp"
    android:gravity="fill"
    android:textColor="@color/white"/>

And for Code AccountSettingActivity class:

private  void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);

ArrayList<String> options = new ArrayList<>();


options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
ArrayList<ImageView> images = new ArrayList<>();
//Please add Customized addapter 
}

for the customized adapter, you find help in this Link https://www.youtube.com/watch?v=_YF6ocdPaBg.

To set an ImageView in code Please see this answer Using variable to change image in Java.

Muhammad umer
  • 1,401
  • 1
  • 8
  • 8