Home » Android » java – Image is visible but text is not visible in ListView-Exceptionshub

java – Image is visible but text is not visible in ListView-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am trying to create a custom layout for list view in android studio. I was able to successfully get image in the in ListView on runtime but wasn’t able to print text in the list view . When i run my program in emulator it runs without any error but it doesn’t show any text it only shows image from drawables.

MainActivity.java

    import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private final  static String[] names={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
            "A","A","A","A","A","A","A","A","A","A","A","A","A","A",};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView ls = findViewById((R.id.customlistview));
        ls.setAdapter(new CustomListAdapter(getApplicationContext(),names,R.drawable.za));
    }
}

activity_main.xml

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/customlistview"
        android:layout_width="409dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

CustomListAdapter.java

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {
    private final Context context;
    private final String[] names;
    private final int image;
    private final LayoutInflater layoutInflater;

    public  CustomListAdapter(Context context,String[] names,int image) {

        this.context=context;
        this.image=image;
        this.names=names;
        layoutInflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rootview;
        rootview = layoutInflater.inflate(R.layout.list_item_layout, null);
        TextView tv= rootview.findViewById(R.id.textView);
        ImageView iv=rootview.findViewById(R.id.imageView);
        tv.setText(names[position]);
        iv.setImageResource(image);
        return rootview;

    }
}

list_item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:height="70dp"
        android:gravity="center"
        android:text="TextView"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_weight="2"
        app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
How to&Answers:

The issue is in your list_item_layout file. There is an issue with match_parent in your layout try using below code

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:height="70dp"
    android:gravity="center"
    android:text="TextView"
    android:textSize="18sp" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="70dp"
    android:layout_weight="2"
    app:srcCompat="@mipmap/ic_launcher" />

Answer:

The problem was caused by the text colour , changing the colour of textview text in list_item_layout resolved the issue.