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!
Change
super.setContentView(layoutResID);
to
super.setContentView(constraintLayout);
in setContentView() method of the BaseActivity.
Related
I am new to android development.I want to use same layout file for two different activities.here is my code
public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
on();
}
}
});
}
public void on()
{
Toast.makeText(this, "button on", Toast.LENGTH_SHORT).show();
}
}
second activity
public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("hello MainActivity2");
}
}
});
}
}
My layout file
<?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">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ToggleButton
android:id="#+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
here no text is coming on if i checked the button only toast is coming.
I am searching for an answer to this.
I dont know how to use this. any type of help will be useful.
The issue
You are providing the constraints for the views which overlap each other. This way, they do not get visible. You must change your layout or constraint the views in different way. I will give solution for changing the layout.
The solution
Try this code in your 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp" />
<ToggleButton
android:id="#+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout >
Another issue might be occurring. Let me explain it here.
You are only setting the text when the button is checked. Not when it is not checked. Try to. display different text every time it's checked or not. Try this code in second activity:
public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("hello MainActivity2");
} else {
textView.setText("bye MainActivity2");
}
}
});
}
}
I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}
i have created a base activity with a progressbar and a framelayout to inflate other activity layouts which are extending base activity. but when i call the showProgressbar method from base activity in other activities. nothing happens. it doesn't showup.
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_content"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.AppCompat.ProgressBar"
android:id="#+id/progress_bar"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
BaseActivity.class
public abstract class BaseActivity extends AppCompatActivity {
private ProgressBar mProgressbar;
#Override
public void setContentView(int layoutResID) {
ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base,null);
FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
mProgressbar = constraintLayout.findViewById(R.id.progress_bar);
getLayoutInflater().inflate(layoutResID,frameLayout,true);
super.setContentView(layoutResID);
}
public void showProgressBar(boolean visibility){
mProgressbar.setVisibility(visibility ? View.VISIBLE : View.INVISIBLE);
}
}
MainActivity.class
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
showProgressBar(true);
}
}
replace this : super.setContentView(layoutResID)
with this : super.setContentView(constraintLayout); you are trying to have base layout and child activity layout and you are inflating them correctly but when you pass super.setContentView your child layoutResID all above code is ignored.
I have modify the solution. I am able to get the progress bar but the progress bar never gets hide
Here is class to create a progress bar with relative layout
public class ProgressBarHandler {
private ProgressBar mProgressBar;
private Context mContext;
private View _baseView;
private View _hideView;
public ProgressBarHandler(Context context,View baseView, View hideView) {
mContext = context;
_baseView = baseView;
_hideView = hideView;
ViewGroup layout = (ViewGroup) _baseView;//((Activity) context).findViewById(android.R.id.content).getRootView();
mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
mProgressBar.setIndeterminate(true);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rl = new RelativeLayout(context);
rl.setGravity(Gravity.CENTER);
rl.addView(mProgressBar);
layout.addView(rl, params);
hide();
}
public void show() {
mProgressBar.setVisibility(View.VISIBLE);
_hideView.setVisibility(View.INVISIBLE);
}
public void hide() {
mProgressBar.setVisibility(View.INVISIBLE);
_hideView.setVisibility(View.VISIBLE);
}
}
This is my xml file
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:id="#+id/profile_activity">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/layout_to_hide"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleTextAppearance="#android:color/transparent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#mipmap/wallpaper"
android:orientation="vertical">
<com.deerwalk.androidcommon.RoundedImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"
android:layout_height="100dp"
android:layout_alignWithParentIfMissing="false"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_margin="20dp"
android:src="#drawable/avatar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/profile_image"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/txt_name"
android:layout_gravity="center_horizontal"
android:text=""
android:textColor="#color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/txt_dob"
android:layout_gravity="center_horizontal"
android:text=""
android:layout_marginTop="5dp"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
Here is the activity class which call the progress bar
public class ProfileActivity extends AppCompatActivity {
FrameLayout profile_root_layout;
CoordinatorLayout layot_to_hide;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
profile_root_layout = (FrameLayout) findViewById(R.id.profile_activity);
layot_to_hide = (CoordinatorLayout) findViewById(R.id.layout_to_hide);
new userProfileTask().execute();
}
public class userProfileTask extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).show();
super.onPreExecute();
}
#Override
protected Boolean doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).hide();
}
}
}
The progress bar never gets hides.Where I am doing mistake.
Make a separate class then make a static method with the parameter on it the parameter contain Activity ... call this method on ur activity and pass the Activity name in it
Its very simple, you can do in multiple ways.
1) You can define base activity and write your code for progress dialog and extends all activity from BaseActivity used Progressdialog.
2) You can have custom separate class and used in all activities.
etc
Here is good example you can modify your code like this example.
ProgressHud
Or use this too.
I would suggest create a dialog and call in your activity class like this
dialog = new Dialog(getActivity(), android.R.style.Theme_Black);
View views = LayoutInflater.from(getActivity()).inflate(R.layout.activity_dialog, null);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(R.color.feed_item_bg);
dialog.setContentView(views);
dialog.show();
and in your async task onPostExecute dismiss dialog
dialog.dismiss();
public static void getProgress(Activity activity,FrameLayout relativeLayout){
final ProgressBar pb;
int progressStatus = 0;
final Handler handler = new Handler();
pb = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams lp = new LayoutParams(550,LayoutParams.WRAP_CONTENT);
pb.setLayoutParams(lp);
LayoutParams params = (LayoutParams) pb.getLayoutParams();
pb.setLayoutParams(params);
pb.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
relativeLayout.addView(pb);
new Thread(new Runnable() {
#Override
public void run() {
int progressStatus = 0;
while(progressStatus < 100){
progressStatus +=1;
try{
Thread.sleep(20);
}catch(InterruptedException e){
e.printStackTrace();
}
final int finalProgressStatus = progressStatus;
handler.post(new Runnable() {
#Override
public void run() {
pb.setProgress(finalProgressStatus);
}
});
}
}
}).start(); // Start the operation
}
You could use a parent Activity. This activity class extends AppCompatActivity . Other Activities will extend this Activity. For example the name of this activity is AbstractActivty.
public class AbstractActivty extends AppCompatActivity{
AlertDialog alertDialogProgressBar;
public void showLoadingProgressBar(String message, boolean cancalable) {
if (isActivityPaused)
return;
AlertDialog.Builder adb = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.alert_dialog_progressbar_layout,null);
TextView tv = (TextView) view.findViewById(R.id.idTextViewMessage);
tv.setText(message);
adb.setView(view);
alertDialogProgressBar = adb.create();
alertDialogProgressBar.setCancelable(cancalable);
alertDialogProgressBar.show();
}
public void closeProgressBar() {
if (alertDialogProgressBar == null) {
return;
}
alertDialogProgressBar.hide();
}
}
This class have a method which could be called by any other activity that extends
AbstractActivty
class MainActivty extends AbstractActivty{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// when require to show progressbar
showLoadingProgressBar("Loading....",true)
// when required to hide call
closeProgressBar();
}
}
Note in Alert Dialog we have used a custom layout
"alert_dialog_progressbar_layout.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="wrap_content"
android:orientation="vertical"
android:padding="#dimen/text_padding_l">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineVertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".3"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guidelineVertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="#+id/idTextViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Loading...."
android:textColor="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/guidelineVertical"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hope this will be of help . Thank you.
I have the same request of this Post and I followed the FoamyGuy's Answer but I don't get the introLayout with the ImageView displaying.
I just want to show my introLayout first and then change it to my WebView.
here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible"
>
<ImageView android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
/>
</LinearLayout>
in MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout introLayout = (RelativeLayout) findViewById(R.id.introLayout);
introLayout.setVisibility(View.GONE);
webview = (WebView)findViewById(R.id.browser);
webview.setVisibility(1);
}
Help?
I don't know if it would make any difference but you could try :
webview.setVisibility (View.VISIBLE);
Instead of :
webview.setVisibility (1);
PS: With View.GONE you make the View not visible and with View.VISIBLE you make the View visible :).
Just because you did the opposite thing in your MainActivity then you discribed.
Your introLayout is set View.GONE, change to
introLayout.setVisibility(View.VISIBLE);
You need to make some logic in your code, when you will hide this "#+id/introLayout" and then show your WebView.
Ok, here is how would i do that if you dont need that layout after few seconds:
ActivityMain
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent startSpalsh = new Intent("com.yourpackagename.secondactivity");
startActivity(startSpalsh);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
}
}
splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
SecondActivity
public class SecondActivity extends AppCompatActivity {
WebView webView;
TextView txtChk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
webview = (WebView)findViewById(R.id.browser);
}
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
</LinearLayout>
First, change de ubication of intro. Set it below of WebView in your layout. WebView and intro don't need to set tag visibility. So... In your onCreate() apply this:
new Handler().postDelayed(new Runnable() { #Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);
2000 = 2 seconds. Change this like you want.
This code will be later that your initialize WebView and IntroLayout.
Change the LinearLayout by RelativeLayout in your layout how principal container.