Reputation: 63
I have a progressBar which should show on button click in constraintlayout
which is not working, I have newslist
activity which extends baseActivity
which extends appCompatActivity
//////NewsListActivity
public class NewsListActivity extends BaseActivity {
Button buttonTesting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_list);
buttonTesting = (Button) findViewById(R.id.button_test);
buttonTesting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_SHORT).show();
if (progressBar.getVisibility()==View.VISIBLE)
showProgressBar(false);
else
showProgressBar(true);
}
});
}
}
//////BaseActivity
public abstract class BaseActivity extends AppCompatActivity {
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_frame);
progressBar = constraintLayout.findViewById(R.id.progress_bar);
getLayoutInflater().inflate(layoutResID,frameLayout,true);
super.setContentView(layoutResID);
}
public void showProgressBar(boolean visibility){
progressBar.setVisibility(visibility? View.VISIBLE:View.INVISIBLE);
}
}
///////layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_frame"
></FrameLayout>
<ProgressBar
android:id="@+id/progress_bar"
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"
style="?android:attr/progressBarStyleLarge"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have a progressBar which should show on button click in constraintLayout
which is not working, I have newslist activity which extends baseActivity
which extends appCompatActivity
Upvotes: 1
Views: 738
Reputation: 19243
exchange this
super.setContentView(layoutResID);
to this
super.setContentView(constraintLayout);
you are inflating new View
(constraintLayout
), taking progressBar
from it, but NOT setting this whole View
as a content View
, instead still calls super.setContentView(layoutResID)
. so there is no ConstraintLayout
, no frame and no progress bar, just inflating strigthly given layout resource. your progressBar
is in memory (as inflated and tied to Context
), so showProgressBar
doesn't throw NPE, but also it still isn't added to Activity
, so it's not visibile no matter of visibility
attr
Upvotes: 3