You are on page 1of 4

Name: ASHISH SHUKLA

Prn:2016017000760177
T.Y.B.C.A

Q12) Create android application using Fragments.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:weightSum="2"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:id="@+id/fragment2"
android:name="com.example.vidhi.fragmentt.Fragment_B"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment"
android:name="com.example.vidhi.fragmentt.Fragment_A"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"

/>

</LinearLayout>

Fragment_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:weightSum="2"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:id="@+id/fragment2"
android:name="com.example.vidhi.fragmentt.Fragment_B"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment"
android:name="com.example.vidhi.fragmentt.Fragment_A"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"

/>

</LinearLayout>

Fragment_fragmentb.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Fragment_B">

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


<TextView
android:layout_width="match_parent"
android:id="@+id/tv1"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</FrameLayout>

Mainactivity.java
package com.example.vidhi.fragmentt;

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

public class MainActivity extends AppCompatActivity implements Communicator {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void respond(String data) {
FragmentManager manager= getFragmentManager();
Fragment_B f2= (Fragment_B) manager.findFragmentById(R.id.fragment2);
f2.ChangeData(data);

}
fragment_A.java
package com.example.vidhi.fragmentt;

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

/**
* A simple {@link Fragment} subclass.
*/
public class Fragment_A extends Fragment implements View.OnClickListener {

Button btn_click;
int counter;
Communicator comm;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
comm= (Communicator) getActivity();
btn_click=(Button)getActivity().findViewById(R.id.btn1);
btn_click.setOnClickListener(this);
}

@Override
public void onClick(View v) {
counter++;
comm.respond("button was clicked "+counter+" Times");
}
}

Fragment_B.java
package com.example.vidhi.fragmentt;

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

/**
* A simple {@link Fragment} subclass.
*/
public class Fragment_B extends Fragment {

TextView text;
String data;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment__b, container, false);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
text= (TextView) getActivity().findViewById(R.id.tv1);
}
public void ChangeData(String data) {
this.data=data;
text.setText(data);

}
}
output

You might also like