본문 바로가기
안드로이드 자바

[Android][Java] 어디서나 사용가능한 프로그레스바

by teamnova 2023. 3. 26.

앱개발을 하다보면 프로그레스바를 띄워야 하는 상황이 많습니다.

 

액티비티·프래그먼트에 프로그레스바를 띄우려고 할 때,

 

보통은 리소스파일 안에 프로그레스바를 만들고, 자바파일에도 선언/초기화 하는 과정을 반복 해야 합니다.

 

static을 활용해서 반복되는 코드를 줄이고, 뷰가 이동되더라도 안전하게 hide되는 progressbar 코드를 작성하였습니다.

 

ToolBox.java에 선언된 progressView를 원하는 곳에서 사용하시면, 언제 어디서든 원할 때마다 로딩창을 구현하실 수 있습니다.

 

--------------------------------------------------

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.stickode2">

    <application
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Stickode2"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name=".SubActivity"/>
    </application>

</manifest>

 

 

 

 

--------------------------------------------------

progressbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BA000000">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

 

 

 

 

--------------------------------------------------

activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        android:textColor="@color/purple_200"
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="go to Sub Activity "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/btnProgressbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="show progressbar"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

 

---------------------------------------------------

activity_sub.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="subActivity"
        android:textColor="@color/purple_700"
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="go to Main Activity "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnProgressbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="show progressbar"
        app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

 

---------------------------------------------------

MainActivity.java


public class MainActivity extends AppCompatActivity{

    Activity _activity;
    Button btn, btnProgressbar;
    boolean isClick = false;

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

        // 현 액티비티의 정보를 가지고 있는 this를 activity변수에 초기화
        _activity = this;

        btn = findViewById(R.id.btn);
        btnProgressbar = findViewById(R.id.btnProgressbar);

        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent i = new Intent(_activity, SubActivity.class);
                startActivity(i);
            }
        });
        btnProgressbar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(isClick){
                    hideProgressingView(_activity);
                    isClick = false;
                }
                else{
                    showProgressingView(_activity);
                    isClick = true;
                }
            }
        });
    }

}

 

 

 

 

--------------------------------------------------

SubActivity.java


public class SubActivity extends AppCompatActivity{

    Activity _activity;
    Button btn, btnProgressbar;
    boolean isClick = false;

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

        // 현 액티비티의 정보를 가지고 있는 this를 activity변수에 초기화
        _activity = this;

        btn = findViewById(R.id.btn);
        btnProgressbar = findViewById(R.id.btnProgressbar);

        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent i = new Intent(_activity, MainActivity.class);
                startActivity(i);
            }
        });
        btnProgressbar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(isClick){
                    hideProgressingView(_activity);
                    isClick = false;
                }
                else{
                    showProgressingView(_activity);
                    isClick = true;
                }
            }
        });
    }
}

 

 

 

 

--------------------------------------------------

ToolBox.java


public class ToolBox{
    /** 프로그레스바 메소드 */
    public static ViewGroup progressView;

    public static void showProgressingView(Activity activity){
        Log.d("TAG", "ENTER :: showProgressingView()\n");
        progressView = (ViewGroup) activity.getLayoutInflater().inflate(R.layout.progressbar_layout, null);
        View v = activity.findViewById(android.R.id.content).getRootView();
        ViewGroup viewGroup = (ViewGroup) v;
        viewGroup.addView(progressView);
        Log.d("TAG", "EXIT :: showProgressingView()\n");
    }

    public static void hideProgressingView(Activity activity){
        Log.d("TAG", "ENTER :: hideProgressingView()\n");
        View v = activity.findViewById(android.R.id.content).getRootView();
        ViewGroup viewGroup = (ViewGroup) v;
        viewGroup.removeView(progressView);
        Log.d("TAG", "EXIT :: hideProgressingView()\n");
    }
}