Pages

Thursday, September 15, 2016

Android Example-Creating two Fragment that can communicate via MainActivity

Android Program for Dividing our Activity into Fully Encapsulated Reusable Components using Fragement


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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="in.kantapp.example141.MainActivity">


    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="in.kantapp.example141.F1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal" />
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="in.kantapp.example141.F2"
        android:id="@+id/fragmen2"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java
package in.kantapp.example141;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.annotation.SuppressLint;
import android.app.Activity;

@SuppressLint("NewApi")
public class MainActivity extends AppCompatActivity implements F2.Listener
{

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



    @Override
    public void onItemSelected(String str) {
        // TODO Auto-generated method stub
        F1 fragment = (F1) getFragmentManager().findFragmentById(
                R.id.fragment1);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setText(str);
        }
    }

}

fragment_f1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF9800"
    android:orientation="vertical"
    tools:context="in.kantapp.example141.F1">

    <!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:gravity="center_horizontal"

        android:text="Fragment One"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:paddingTop="20dp"
        android:text="Change Time By Click Fragment two Button"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/settime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="#000000" />

</LinearLayout>

F1.java
package in.kantapp.example141;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class F1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_f1, container, false);
        return view;
    }

    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.settime);
        view.setText(item);
    }
}

fragment_f2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.kantapp.example141.F1"
    android:orientation="vertical"
    android:background="#FFCC80">

    <!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment Two"
        android:id="@+id/f2"
        android:layout_gravity="center_horizontal|top" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Current Time"
        android:id="@+id/presstime"

        android:layout_gravity="center_horizontal" />

</LinearLayout>

F2.java
package in.kantapp.example141;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.icu.text.DateFormat;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.Date;

@SuppressLint("NewApi")
public class F2 extends Fragment {
    private Listener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_f2, container, false);
        Button button = (Button) view.findViewById(R.id.presstime);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDetail();
            }
        });
        return view;
    }

    public interface Listener {
        public void onItemSelected(String link);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Listener) {
            listener = (Listener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " must implemenet fragement2.listener");
        }
    }

    public void updateDetail() {
        String newTime = String.valueOf(DateFormat.getTimeInstance().format(new Date()));
        listener.onItemSelected(newTime);
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
        listener = null;
    }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example141">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

No comments:

Post a Comment