Wednesday, 18 February 2015

How to Finish Parent Activity from Child Activity in Android

In previous post i explained "how to back from child activity to fragment"


Here i will explain how to finish parent activity from child activity in Android..

Parent Activity


public class HomeScreen extends Activity
{
      static HomeScreen firstActivity ; //Create one Activity variable"firstActivity "
      @Override
       protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
setContentView(R.layout.newhome);
        firstActivity= this;
}


        public static HomeScreen getActivity() 
                          //Declare one function "getActivity()"
          {
            return   firstActivity ;  
                     // Assign Activity "HomeScreen"  to Activity Variable "firstActivity"
          }


}



In Child activity call the getActivity() function to finish the child activity.HomeScreen.getActivity().finish();  
                 // the fuction returns the Activity variable "firstActivity"

Happy Coding :)




Android back from a Child Activity started by a Fragment

If you want to back from a child activity to Fragment put the following code in your Manifest File



 <activity
           android:name="com.assests.AssestsActivity"  //Parent Fragment Activity
           android:label="Assests"
           android:launchMode="singleTop" 
           android:parentActivityName="com.docx.HomeScreen" //Parent activity 
           android:screenOrientation="portrait" >
 </activity>



Below Manifest code is for ChildActivity


  <activity
            android:name="com.assests.VechicelsDetails"  //Child Activity
            android:label="Vechicels"
            android:parentActivityName="com.assests.AssestsActivity"
            android:screenOrientation="portrait" >
  </activity>
   <activity
            android:name="com.assests.DepositsDetails"  //Child Activity
            android:label="Deposits"
            android:parentActivityName="com.assests.AssestsActivity"
            android:screenOrientation="portrait" >
 </activity>

If you click any child activity, it will go to the particular fragment


Wednesday, 11 February 2015

Android Webview Swipe To Refresh


Add the following Layout and Class file into your project.

Mainactivity.java



package com.dhinakaran.swiperefreshlayout;

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity 

{
 WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeView =(SwipeRefreshLayout)findViewById(R.id.swipe);
browser = (WebView)findViewById(R.id.webView1);
        browser.setWebViewClient(new MyBrowser());
        browser.loadUrl("http://androiddhina.blogspot.in/");
swipeView.setColorScheme(android.R.color.holo_blue_dark,android.R.color.holo_blue_light, android.R.color.holo_green_light,android.R.color.holo_green_dark);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() 
{
@Override
public void onRefresh() 
{
        swipeView.setRefreshing(true);
             ( new Handler()).postDelayed(new Runnable() 
{
@Override
public void run() 
{
                         swipeView.setRefreshing(false);
browser.loadUrl("http://androiddhina.blogspot.in/");
}
}, 4000);
}
});
}
   private class MyBrowser extends WebViewClient  //If you click on any link inside the webpage of the WebView , that page will not be loaded inside your WebView. In order to do that you need to extend your class from WebViewClient by using the below function.
      {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }
}

activity_main.xml file


<android.support.v4.widget.SwipeRefreshLayout    xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/swipe"
 android:layout_width="match_parent"
 android:layout_height="match_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=".MainActivity" >

<ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

 <WebView
 android:id="@+id/webView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/textView1"
 android:layout_alignParentBottom="true"
 android:layout_below="@+id/button1" />

</ScrollView>

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


AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.dhinakaran.swiperefreshlayout" 
 android:versionCode="1" android:versionName="1.0" > 
 <uses-sdk android:minSdkVersion="8"
 android:targetSdkVersion="18" /> 
 <uses-permission android:name="android.permission.INTERNET"/> 
 <application 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher"        
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" >
               <activity 
                 android:name="com.example.swiperefreshlayout.MainActivity"                                                            android:label="@string/app_name" >                            
              <intent-filter> 
              <action   android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" /> 
              </intent-filter>
          </activity>
 </application>
</manifest>

Happy Coding:)