2

It's really weird the problem I am facing. I am trying to show image if no records available. But image not getting visible.

Here is the flow.

I have content_main.xml in which I have one recycle view, one relativeLayout for permission and one ImageView for Image.

If permission is not granted I would like to show Permission layout (Working). If permission is granted, then I am getting record from database and setting to adapter but If recordList size is 0 then I need to show Image (Not Working). Instead I am only able see fab button and white background.

Here is my content_main.xml (I am using navigation drawer so content_main.xml is included in app_bar_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.sandy.callrecorder.MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/no_records"
    android:visibility="gone"
    android:id="@+id/backgroundImage"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/record_recycle_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"       
    android:scrollbars="none"
    android:visibility="gone" />

<RelativeLayout
    android:id="@+id/permissionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingEnd="36dip"
    android:paddingLeft="36dip"
    android:paddingRight="36dip"
    android:paddingStart="36dip"
    >

    <Button
        android:id="@+id/btnAllowPermission"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/allow_permissions" />
</RelativeLayout>

Here is my MainActivity.java

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

    findViewById();
    managePermissions();
}

private void findViewById() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    backgroundImage = (ImageView) findViewById(R.id.backgroundImage);
    permissionLayout = (RelativeLayout) findViewById(R.id.permissionLayout);
    mBtnAllowPermission = (Button)findViewById(R.id.btnAllowPermission);
    
    recyclerView = (RecyclerView) findViewById(R.id.record_recycle_list);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.content_main);
    fab = (FloatingActionButton) findViewById(R.id.fab);

    mBtnAllowPermission.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(MainActivity.this, permission, MY_PERMISSIONS_REQUES);
        }
    });

    
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

private void managePermissions() {

    if (!hasPermissions(permission)) {
        permissionLayout.setVisibility(View.VISIBLE);
        backgroundImage.setVisibility(View.GONE);
    } else {
        initializeRecycleView();
        startService();
    }
    
}

private void initializeRecycleView() {

    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(mLayoutManager);
    DBHelper mDBHelper = new DBHelper(this);
    mDBHelper.open();

    ArrayList<CallRecord> listOfRecord = mDBHelper.getRecordList();
    mDBHelper.close();

    listOfRecords = new ArrayList<>();
    for (CallRecord objCallRecord : listOfRecord) {
        if (objCallRecord.getIsArchive() == 0) {
            listOfRecords.add(objCallRecord);
        }
    }
    setListToRecycle(listOfRecords);
}

private void setListToRecycle(ArrayList<CallRecord> listOfRecords) {
    // define an adapter
    if (listOfRecords.size() <= 0) {
        fab.setVisibility(View.VISIBLE);
        permissionLayout.setVisibility(View.GONE);
        backgroundImage.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    } else {
        fab.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
        backgroundImage.setVisibility(View.GONE);
        permissionLayout.setVisibility(View.GONE);
        mAdapter = new RecordListAdapter(listOfRecords, this);
        recyclerView.setAdapter(mAdapter);
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSIONS_REQUES: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (!isMyServiceRunning(TService.class)) {
                    Intent intent = new Intent(MainActivity.this, TService.class);
                    startService(intent);
                }

                initializeRecycleView();

            } else {

                if (isMyServiceRunning(TService.class)) {
                    Intent intent = new Intent(MainActivity.this, TService.class);
                    stopService(intent);
                }
            }

        }
    }
}
}  

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

1 Answers1

1

Check if you placed your images in a folder that is appropriate for the screen size you are testing with. Similar issue here: Android Drawable Image not Showing

Endre Börcsök
  • 477
  • 1
  • 8
  • 19