Skip to main content

Add Swipe To Refresh Feature To Your App using Android Studio

Add Swipe To Refresh Feature To Your App Using Android Studio




If you haven't watched the video watch it now!



Source Code


Android Manifest.xml





<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="swiperefresh.com.swipetorefresh">
    <uses-permission android:name="android.permission.INTERNET" />
<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
   </application>
</manifest>






MainActivity.java






package swiperefresh.com.swipetorefresh;


import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {



    private WebView web1;
    private SwipeRefreshLayout refreshLayout;







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

        web1 = (WebView)findViewById(R.id.web1);


        refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshLayout);



web1.loadUrl("https://google.com");

        web1.getSettings().setJavaScriptEnabled(true);
        web1.setWebViewClient(new WebViewClient());



        web1.setWebViewClient(new WebViewClient() {


            @Override            public void onPageStarted(WebView view, String url, Bitmap facIcon) {

            }

            @Override            public void onPageFinished(WebView view, String url) {


                if(refreshLayout.isRefreshing()){
                    refreshLayout.setRefreshing(false);
                }

            }
        });


        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override            public void onRefresh() {

                web1.reload();

            }
        });




    }

}




activity_main.xml



<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="swiperefresh.com.swipetorefresh.MainActivity">


<android.support.v4.widget.SwipeRefreshLayout    android:id="@+id/refreshLayout"    android:layout_width="match_parent"    android:layout_height="match_parent">


    <WebView        android:id="@+id/web1"        android:layout_width="match_parent"        android:layout_height="match_parent">

    </WebView>



</android.support.v4.widget.SwipeRefreshLayout>

</android.support.constraint.ConstraintLayout>


Build.Gradle   (Module:app)



apply plugin: 'com.android.application'
android {
    compileSdkVersion 26    defaultConfig {
        applicationId "swiperefresh.com.swipetorefresh"        minSdkVersion 19        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }
    buildTypes {
        release {
            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'    implementation 'com.android.support.constraint:constraint-layout:1.1.0'    implementation 'com.android.support:design:26.1.0'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.2'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}





If you have any doubts comment below


Comments

Did you like this tutorial ?

Popular posts from this blog

Add Edittext Inside Dialog Box

Add Edittext Inside Dialog Box




If you haven`t yet watched the video...Watch it right now!!!


The code used in the above video is available here.
final EditText edittext1= new EditText(MainActivity.this); LinearLayout.LayoutParams lpar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); edittext1.setLayoutParams(lpar); dialog.setView(edittext1);







textview1.setText(edittext1.getText());






Create Custom Popup Window using Sketchware

Create Custom Popup Window using Sketchware


Refer the video below if  you get stuck. 

1) Add a custom view named window.You can name it according to your wish. 
2)Design your popup window. 
2) Now, In the logic section,In OnCLick event of button.Add the following  code
Create a view
View popupView = getLayoutInflater().inflate(R.layout.window, null);
Popup window 

final PopupWindow popup = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Initialize 
Button b1 = popupView.findViewById(R.id.button1); Button b2 = popupView.findViewById(R.id.button2);
OnClickListeners
b1.setOnClickListener(new OnClickListener() { public void onClick(View view) { //below code will dismiss the popup window popup.dismiss(); } });
b2.setOnClickListener(new OnClickListener() { public void onClick(View view) {
popup.dismiss(); } });
Show the popup window 
popup.showAtLocation(popupView, Gravity.CENTER, 0, 0);