Click here to Skip to main content
16,019,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing an application where i need to call two fragments on an activity replacing another fragment,one of the fragment will contain a date time picker and another fragment will contain the list of the related items of the selected date,i don't have any idea how to do this,so any suggestion will be cordially appreciated,thanks in advance....
Posted

1 solution

Try this Code..
Java
package balaji.fragment_static;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
}


XML
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <fragment>
		android:name="balaji.fragment_static.Fragment1"
		android:id="@+id/fragment1"
		android:layout_weight="1"
		android:layout_width="0dp"
		android:layout_height="match_parent" />
    <fragment>
		android:name="balaji.fragment_static.Fragment2"
		android:id="@+id/fragment2"
		android:layout_weight="1"
		android:layout_width="0dp"
		android:layout_height="match_parent" />    

</fragment></fragment></linearlayout>


Java
package balaji.fragment_static;

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

public class Fragment1 extends Fragment {
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment1, container, false);
		
	}
}


XML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#7B68EE"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
     <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="30dp"
        android:text="Fragment 1" />
    
</textview></relativelayout>


Java
package balaji.fragment_static;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi") 
public class Fragment2 extends Fragment {
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment2, container, false);
	}
}


XML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#00FF00"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="30dp"
        android:text="Fragment 2" />

</textview></relativelayout>
 
Share this answer
 
Comments
Umer farooq 17-May-17 9:34am    
but what when i have multiple fragments, each have its own layout and i want to interact with that particular layout on the screen when my fragment called. suppose i have a form to fill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900