Android how to make the ProgressBar(circle) to cover the full screen when pressing button? -
i try display progessbar when clicking submit button. hide when data finished loading. however, progressbar didn't cover full screen. instead, it's cover button. please refer screenshot, should easier understand mean.
what want achieve bottom part of screenshot.
main4activity.java
public class main4activity extends appcompatactivity { progressbar progressbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main4); progressbar = (progressbar) findviewbyid(r.id.progressbar); progressbar.setvisibility(view.gone); final edittext username=(edittext)findviewbyid(r.id.username); final edittext email=(edittext)findviewbyid(r.id.email); final edittext password=(edittext)findviewbyid(r.id.password); final button submit=(button)findviewbyid(r.id.submit); submit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { progressbar.setvisibility(view.visible); final string get_username=username.gettext().tostring(); final string get_email=email.gettext().tostring(); final string get_password=password.gettext().tostring(); response.listener<string> response_listener=new response.listener<string>() { @override public void onresponse(string response) { try{ jsonobject jsonobject=new jsonobject(response); boolean result=jsonobject.getboolean("register_result"); progressbar.setvisibility(view.gone); if(result){ alertdialog.builder builder=new alertdialog.builder(main4activity.this); builder.setmessage("registration done!") .setnegativebutton("back",null) .create() .show(); }else{ alertdialog.builder builder=new alertdialog.builder(main4activity.this); builder.setmessage("user existed! please try different email") .setnegativebutton("back",null) .create() .show(); } }catch(jsonexception e){ e.printstacktrace();; } } }; register_request register_request=new register_request(get_username,get_email,get_password,response_listener); requestqueue queue=volley.newrequestqueue(main4activity.this); queue.add(register_request); } }); textview register=(textview)findviewbyid(r.id.login); register.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent=new intent(main4activity.this,login_activity.class); main4activity.this.startactivity(intent); } }); } }
xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.collection_tutorial.main4activity" android:orientation="vertical"> <progressbar style="?android:attr/progressbarstylelarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressbar" android:layout_centervertical="true" android:layout_centerhorizontal="true" android:indeterminate="false" android:layout_gravity="center" /> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/password" android:layout_below="@+id/email" android:layout_centerhorizontal="true" android:hint="password" android:layout_margin="10dp" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/username" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:hint="username / restaurant name" android:layout_margin="10dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="existing user? please login here->" android:id="@+id/login" android:layout_below="@+id/submit" android:layout_centerhorizontal="true" android:layout_margin="10dp" /> <button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="submit registration" android:id="@+id/submit" android:layout_below="@+id/password" android:layout_centerhorizontal="true" android:layout_margin="10dp" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/email" android:layout_below="@+id/username" android:layout_centerhorizontal="true" android:hint="email " android:layout_margin="10dp" /> </relativelayout> i try put progressbar , 3 edittext , button same relativelayout, doesn't work either. can help?
first of i'll assign id inner relativelayout:
<relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" > with can reference in code relativelayout, i'll add new field:
progressbar progressbar; relativelayout container; then onclick i'll hide container:
@override public void onclick(view view) { progressbar.setvisibility(view.visible); container.setvisibility(view.gone); ... and make visible when task done:
@override public void onresponse(string response) { try { jsonobject jsonobject = new jsonobject(response); boolean result = jsonobject.getboolean("register_result"); progressbar.setvisibility(view.gone); container.setvisibility(view.visible);... hope helps.
however, i'll not recommend user experience in app. there other mechanism notify user, check material design guidelines:
https://material.google.com/components/progress-activity.html
btw, tested code , works way mentioned you're expecting it. here's gist code, it's better if use snippets because renamed couple of variables:
https://gist.github.com/moxi/396b073f9df063dc3c943579c93f1be9
see result on gif: https://giphy.com/gifs/uaaye6j9hy6dy

Comments
Post a Comment