Friday, August 4, 2017

Android application using fragment.



/*******************************************************************************************
Program Name: Write an android application using fragment.
Author: 141740
Date: 27/9/2016
*******************************************************************************************/

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

<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>


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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:orientation="vertical"
tools:context="com.example.sujithbaby.fragment2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="button1"
android:onClick="Fragment1Click"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="button2"
android:onClick="Fragment2Click"
android:layout_weight="1"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/frag_ch"
android:name="com.example.sujithbaby.fragment2.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
</LinearLayout>
</LinearLayout>


MainActivity.java
package com.example.sujithbaby.fragment2;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

public void Fragment1Click(View v){
Fragment myFragment;
myFragment= new FragmentOne();
FragmentManager fm= getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction= fm.beginTransaction();
fragmentTransaction.replace(R.id.frag_ch,myFragment);
fragmentTransaction.commit();

}


public void Fragment2Click(View v){
Fragment myFragment;
myFragment= new FragmentTwo();
FragmentManager fm= getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction= fm.beginTransaction();
fragmentTransaction.replace(R.id.frag_ch,myFragment);
fragmentTransaction.commit();

}
}


fragment_fragment_one.xml
<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"
android:id="@+id/f1"
tools:context="com.example.sujithbaby.fragment2.FragmentOne">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/frag1"
android:textColor="#fff"
android:background="#116BA4"
/>

</FrameLayout>


FragmentOne.java

package com.example.sujithbaby.fragment2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}


fragment_fragment_two.xml
<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"
android:id="@+id/f2"
tools:context="com.example.sujithbaby.fragment2.FragmentTwo">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/frag2"
android:textColor="#fff"
android:background="#A41223"
/>
</FrameLayout>



FragmentTwo.java
package com.example.sujithbaby.fragment2;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}
}


Strings.xml
<resources>
<string name="app_name">Fragment</string>
<string name="frag1">Fragment 1</string>
<string name="frag2">Fragment 2</string>
</resources>



OUTPUT
 


No comments:

Post a Comment