Android Many Activity Inside Fragment
Below I wrote the code for
how to call many activities inside activity using fragment.Why Many Activity?
Sometime we need to use external jar file . An external jar file having only activity no fragment so that time we need.
What I did?
Main Activity
|
--------------------------------------------------------------------------------------
Fragment 1 Fragment
2
(Its having tabview with activity) (Its
having tabview with activity)
·
I hope you understand this
flow.
·
I wrote below code as per as my
requirement.
·
More question ask me.
Main Activity Sourece Code
package
com.vijay.fragmentex;
import
android.os.Bundle;
import
android.app.Activity;
import
android.app.FragmentManager;
import
android.app.FragmentTransaction;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
/**
*
* @author Vijayakumar
m
* http://www.android-ever.com/
*
*/
public class
MainActivity extends Activity {
public static
FragmentTransaction fragmentTransaction;
public static
FragmentTabView fragmentActivity1;
public static
FragmentActivity2 fragmentActivity2;
public static
FragmentManager fragmentManager;
Button btn1, btn2;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 =
(Button)findViewById(R.id.button1);
btn2 =
(Button)findViewById(R.id.button2);
fragmentManager =
getFragmentManager();
btn1.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
fragmentActivity1();
}
});
btn2.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
fragmentActivity2();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu
menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main,
menu);
return true;
}
public static void
fragmentActivity1() {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentActivity1 = new
FragmentTabView();
fragmentTransaction.replace(R.id.frag_layout,fragmentActivity1);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}
public static void
fragmentActivity2() {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentActivity2 = new
FragmentActivity2();
fragmentTransaction.replace(R.id.fraglayout2,fragmentActivity2);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}
}
Fragment
1 source code
package
com.vijay.fragmentex;
import
android.app.Fragment;
import android.app.LocalActivityManager;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.ImageButton;
import
android.widget.TabHost;
/**
*
* @author vijayakumar
*
*/
public class
FragmentTabView extends Fragment implements
TabHost.OnTabChangeListener,OnClickListener{
TabHost tabHost;
View vi;
ImageButton close;
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
vi=inflater.inflate(R.layout.fragmenttabview, null);
mileageDialog(savedInstanceState);
return vi;
}
/**
*
Method for creating the tab host inside the fragment
* @param
bund
*/
public void
mileageDialog(Bundle bund) {
tabHost =
(TabHost) vi.findViewById(android.R.id.tabhost);
Intent mapView = new
Intent(getActivity(), Activity1.class);
mLocalActivityManager.dispatchCreate(bund);
tabHost.setup(mLocalActivityManager);
tabHost.addTab(tabHost.newTabSpec("Activity1").setIndicator("Activity1",null).setContent(mapView));
tabHost.setCurrentTab(0);
}
@Override
public void
onTabChanged(String tabId) {
}
@Override
public void
onClick(View v) {
}
}
Fragment
2 source code
package
com.vijay.fragmentex;
import
android.app.Fragment;
import android.app.LocalActivityManager;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.ImageButton;
import
android.widget.TabHost;
/**
*
* @author vijayakumar
*
*/
public class FragmentActivity2 extends
Fragment implements TabHost.OnTabChangeListener,OnClickListener{
TabHost tabHost;
View vi;
ImageButton close;
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
vi=inflater.inflate(R.layout.fragmenttabview, null);
mileageDialog(savedInstanceState);
return vi;
}
/**
*
Method for creating the tab host inside the fragment
* @param
bund
*/
public void
mileageDialog(Bundle bund) {
tabHost =
(TabHost) vi.findViewById(android.R.id.tabhost);
Intent mapView = new
Intent(getActivity(), Activity2.class);
mLocalActivityManager.dispatchCreate(bund);
tabHost.setup(mLocalActivityManager);
tabHost.addTab(tabHost.newTabSpec("Activity2").setIndicator("Activity2",null).setContent(mapView));
tabHost.setCurrentTab(0);
}
@Override
public void
onTabChanged(String tabId) {
}
@Override
public void
onClick(View v) {
}
}
Main
Activity XML Source code
<RelativeLayout 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=".MainActivity"
>
<FrameLayout
android:id="@+id/frag_layout"
android:layout_width="wrap_content"
android:layout_height="260dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="120dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:text="Call Activiy2" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="26dp"
android:layout_toLeftOf="@+id/button2"
android:text="Call
Activiy1" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fraglayout2"
android:layout_alignBottom="@+id/frag_layout"
android:layout_marginBottom="80dp"
android:layout_marginLeft="32dp"
>
</FrameLayout>
</RelativeLayout>
Fragment
XML Source Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</TabHost>
</LinearLayout>
DownLoad Source Code
very nice am beginner for android may i use single xml file and single java (Activity) and using fragment its possible please update sample program
ReplyDelete99 brand new Why didn't the custom stickers simply lower the prices of such items to take advantage of the custom size label across the world. This means that doctors practicing in places like New York and the Robert Wood Johnson Foundation.
ReplyDeleteFeel free to visit my webpage :: labels