Fragment
//activity_mail.xml //container ni error aave to activity main_xml jain ne id container lai leve
<linearlayout android:id="@+id/container" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context="demoproject.aalap.com.fragment.MainActivity" 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">
<button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="btn1">
</button><button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="btn2">
</button></linearlayout>
//fragment1.xml khale textview j lkhvu aavi j rete freagment2.xml textview khale levu aema text ma home ne badle koi pn name lkhe devu
<textview android:layout_height="wrap_content" android:layout_width="match_parent" android:text="home">
//fragment1.java aavi j rete fragment2.java lai levo
public class fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}
}
//MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment1 f1=new fragment1();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container,f1);
ft.commit();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment2 f2=new fragment2();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container,f2);
ft.commit();
}
});
}
}
Comments
Post a Comment