Search code examples
javaandroidandroid-progressbar

Cannot view progressBar created in BaseActivity(parent) in MainActivity(child)?


I am trying to make a single base activity for all the redundant actions. I tried putting the progress bar in BaseActivity and tried extending it in MainActivity, But it is not showing up at all. My Code

activity_base.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/activity_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    public ProgressBar progressBar;

    @Override
    public void setContentView(int layoutResID) {

        ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
        progressBar = constraintLayout.findViewById(R.id.progressBar);
        Log.i(TAG, "setContentView: Progressbar" + progressBar.getVisibility());

        getLayoutInflater().inflate(layoutResID, frameLayout, true);
        super.setContentView(layoutResID);
    }

    public void showProgressBar(boolean visibility) {
        progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }
}

MainActivity.java


public class MainActivity extends BaseActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_get_data = (Button) findViewById(R.id.btn_get_data);

        btn_get_data.setOnClickListener(view -> {


       if (progressBar.getVisibility() == View.VISIBLE) {
                Log.i(TAG, "onCreate: DISABLING PROGRESS BAR");
                showProgressBar(false);
            } else {
                showProgressBar(true);
            }

   }


}

There are other things in the app, but they have no connection with the progress bar. And yes I have declared the button and other things properly.

Thanks in advance!


Solution

  • Change

    super.setContentView(layoutResID); 
    

    to

    super.setContentView(constraintLayout); 
    

    in setContentView() method of the BaseActivity.