i have this toolbar
i want to remove the title of the fragment so i can display more items
is it from the xml file or the activity?
because i tried to add this to my main activity:
getActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolbar).setTitle("");
toolbar.setTitle(null)
but none of them worked
any suggestion?
this is MainActiviy code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
setupBottomNavMenu(navController)
setupSideNavigationMenu(navController)
setupActionBar(navController)
Since you are using a Navigation component you can use the addOnDestinationChangedListener to set your custom title after your setup method.
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.xxxx) {
//If in your setup you are using a Toolbar
toolbar.title = ""
//supportActionBar?.title = "" //If you are using setSupportActionBar()
} else {
//
}
}
You could create your own tool bar for each fragment and customize it as per your requirement. I have made a common toolbar xml file and reusing it in every fragment and customizing it. Look :
common_toolbar.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/const_item"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/margin_normal"
android:gravity="center"
android:padding="#dimen/padding_five"
android:text="Back"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:drawableStartCompat="#drawable/ic_action_chevron_left"
app:drawableTint="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title_toolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/menu"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
And then adding it to fragments's UI. Let's say, adding to fragment_user_details.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar_common"
android:id="#+id/toolbar_user_details"/>
<!--
Other View widgets
-->
</androidx.constraintlayout.widget.ConstraintLayout>
As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement. Make sure Activity has Theme.MaterialComponents.Light.NoActionBar theme.
Menu Items can also be added like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
Related
FolderActivity.java
public class FolderActivity extends AppCompatActivity {
private MyListAdapter myListAdapter;
private HashMap<String, List<VideoModel>> videodata;
private HashMap<String,List<String>> latestvideo=new HashMap<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
RecyclerView folderrecycleview=findViewById(R.id.folderrecyclerview);
final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
folderrecycleview.setLayoutManager(layoutManager);
myListAdapter=new MyListAdapter(null);
folderrecycleview.setAdapter(myListAdapter);
}
Fragment.kt
class VideoPlayer : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val intent = Intent(activity, FolderActivity::class.java)
startActivity(intent)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_video_player_activity, container, false)
}
}
fragmentvideoplayeractivity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_height="match_parent"
tools:context=".navigation.fragments.LocalVideoPlayerMain">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#color/blue"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mytitle"
android:textSize="18sp"
android:text="#string/app_name"
android:fontFamily="#font/lato_regular"
>
</TextView>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/folderrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="display the background theme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/recentplayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="24dp"
android:layout_marginBottom="32dp"
android:clickable="true"
android:text="#string/play_recent"
android:src="#drawable/ic_play_btn"
android:focusable="true"
app:backgroundTint="#color/blue"
android:contentDescription="TODO" />
</RelativeLayout>
</FrameLayout>
MainActivity.kt this is the bottom navigation bar and were the app source starts first
class MainActivity : AppCompatActivity() {
private var doubleBackToExitPressedOnce = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mainactivity)
val LocalVideoPlayerMain =LocalVideoPlayerMain()
val example1 = example2()
val example2=example3()
val example3=example4()
makeCurrentFragment(VideoPlayer)
bottomNavigationView.setOnNavigationItemSelectedListener{
when (it.itemId){
R.id.example1 -> makeCurrentFragment(VideoPlayer)
R.id.example2 -> makeCurrentFragment(example2)
R.id.example3 -> makeCurrentFragment(example3)
R.id.example4 -> makeCurrentFragment(example4)
}
true
}
}
private fun makeCurrentFragment(fragment: Fragment)=
supportFragmentManager.beginTransaction().apply{
replace(R.id.fl_wapper,fragment)
commit()
}
}
can any one please tell me how to make folderActivity work on fragment layout.
when i click the video player button on navigation bar, Folderactivity.java(it has same setcontentView(fragmentvideoplayeractivity.xml as fragment.kt) it just popup above the fragment layout and when i click back button it show the fragment.kt layout(which have a bottom navigation bar).the navigation bar also overlay by folderactivity.java
i need to show that overlayed recyclerview with the navigation bar
Overlayed Folder activity above the fragment(it automatically popup the activity it didn't open it on same navigation fragment layout)
i need to make this recycler into one complete activity like given below
if you know any solution please tell me
I created custom toolbar inside fragment,but i got view like this:
In themes.xml i disabled ActionBar:
<style name="Theme.Project" parent="Theme.MaterialComponents.DayNight.NoActionBar">
This is my custom toolbar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
>
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home"
android:fontFamily="#font/google_sans"
android:textColor="#color/black"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/fragment_home" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And this is HomeFragment.java:
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.toolbar_in_home, container, false);
return root;
}
}
How to remove this white thing on top of screen?
UPD:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
</androidx.constraintlayout.widget.ConstraintLayout>
This is fragment home(just ConstraintLayout)
UPD2:i also was trying use toolbar in activity_main and it works:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_examination, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
}
But i can't do changes if i want to go on other fragment.
I need to set another custom toolbar layout with button like this:
So this is how I did it. My app is based on single activity-multi-fragment model.
I had set activity theme to AppTheme.NoActionBar, so it enabled me to set a custom toolbar.
Later for each fragment I had a separate layout and within which added a custom toolbar like below
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
....
....
</androidx.appcompat.widget.Toolbar>
Within this layout I had added menu items.
If you don't want to follow this, may be you should look at getActionBar().setCustomView(). Keep your activity theme set, so getActionBar() is not null.
You can control action bar layout by calling getActionBar().setCustomView() in fragment onResume() or probably when you are adding or removing a fragment in your activity.
You can use your custom toolbar inside the main layout replace it from com.google.android.material.appbar.AppBarLayout.
Here is an example code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/navi"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:contentDescription="#string/todo"
android:src="#drawable/navi" />
<ImageView
android:layout_width="180dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:contentDescription="#string/todo"
android:src="#drawable/header2" />
</RelativeLayout>
<include layout="#layout/fragment_home" />
</RelativeLayout>
I have a simple project with 3 fragments and i'm trying to add animations to it transitions.
It navigates when i click the buttons but now i'm trying to add animations to those transitions in the nav graph but for some reason the destination of the actions are not being registered.
This is the code i have already:
Main Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupActionBarWithNavController(findNavController(R.id.fragment))
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
Fragment A:
class FragmentA : Fragment() {
private var _binding: FragmentABinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentABinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val navController : NavController = Navigation.findNavController(view)
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
binding.button2.setOnClickListener {
navController.navigate(R.id.fragmentC)
}
}
}
Main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context="com.example.MainActivity">
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment A 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:background="#android:color/holo_purple"
tools:context="com.example.FragmentA">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentB"
android:textColor="#color/black" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentC"
android:textColor="#color/black" />
</LinearLayout>
</FrameLayout>
And the other 2 fragments just have 2 buttons and nothing of logic aplicated yet.
So when i try to create an action for the navigation from Fragment A (Home fragment) to fragment B the desing window normally shows the id of the action and its destinantion but i don't know why it is not registering the destination when i create the action. It looks like this now:
I tried to add the destination manually but it doesn't works either because it just delete it and let it blank again.
Nav Graph xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/nav_graph"
app:startDestination="#id/fragmentA">
<fragment
android:id="#+id/fragmentA"
android:name="com.example.FragmentA"
android:label="fragment_a"
tools:layout="#layout/fragment_a" >
<action
android:id="#+id/action_fragmentA_to_fragmentB2"
app:destination="#id/fragmentB" />
</fragment>
<fragment
android:id="#+id/fragmentB"
android:name="com.example.FragmentB"
android:label="fragment_b"
tools:layout="#layout/fragment_b" />
<fragment
android:id="#+id/fragmentC"
android:name="com.example.FragmentC"
android:label="fragment_c"
tools:layout="#layout/fragment_c" />
</navigation>
Animation xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%" android:toXDelta="0%" android:duration ="800"/>
</set>
I added this custom animation to enterAnim field but its not taking it.
I don't really know whats going on and i didn't find any answer to this issue.
I found my error and it was on the Fragmnet A code. It was about the click listener.
I had it like:
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
but i need to add the action instead:
binding.button1.setOnClickListener {
findNavController().navigate(R.id.action_fragmentA_to_fragmentB)
}
The destination field is bugged i think, now everything works as it should.
I've trying to implement custom fragment with loading animation. Same code works fine with activity, but not works with fragment.
Here my fragment layout code:
<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="pw.osin.musicexpert.fragments.BusyFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/fragment_animation_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:background="#drawable/vinyl" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#color/colorPrimaryDark">
<TextView
android:id="#+id/fragment_animation_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/message_data_is_loading"
android:textColor="#color/colorPrimaryYellow"
android:textSize="20dp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
My methods which I invokes in OnCreate method in fragment
private fun setAnimation() {
Log.i(TAG, "Установка анимации")
val drawable = AnimationUtils.loadAnimation(this.context, R.anim.rotate_anim)
mAnimationDescription?.setText(R.string.message_data_is_loading);
mAnimationImage?.startAnimation(drawable)
}
private fun initViewItems(view: View?) {
Log.i(TAG, "Поиск элементов View")
mAnimationDescription = view?.findView<TextView>(R.id.animation_description);
mAnimationImage = view?.findView<ImageView>(R.id.animation_logo);
}
My rotate animation code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" />
</set>
Here I attach my animation fragment to activity in OnCreate method
private var mTransaction: FragmentTransaction? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
attachFragment()
}
private fun attachFragment() {
mTransaction = supportFragmentManager.beginTransaction()
mTransaction?.add(R.id.main_activity_container, BusyFragment.newInstance("Получаем дату"))
mTransaction?.commit()
}
Where I need to invoke my start animation method?
Thanks.
From the documentation of onResume:
Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.
You want to run the animation when it can be visible in the first place, hence I suggest starting the animation in your fragment's onResume or override the onFragmentsResumed method in the activity that this fragment is attached to.
i am having some trouble understanding and using fragments i have 3 buttons and 1 fragment views .using the buttons as Tabs i am trying to give the fragment a specific class when clicking on a button what i did is this :
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1" >
<Button
android:id="#+id/Tab1"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 1" android:background="#drawable/btn"/>
<Button
android:id="#+id/Tab2"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 2" android:onClick="switchToTab2" />
<Button
android:id="#+id/Tab3"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<fragment
android:id="#+id/fragment1"
android:name="com.example.newproject.fragmentclass"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
java class :
fragmentclass frt1;
fragmentclass2 frt2;
fragment3class frt3;
FragmentTransaction ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tabs_exp);
ft = getSupportFragmentManager().beginTransaction();
frt1 = new fragmentclass();
frt2 = new fragmentclass2();
frt3 = new fragment3class();
//ft.add(R.id.fragment1,frt1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu);
return true;
}
public void switchToTab2 (View v){
ft.replace(R.id.fragment1, frt2).commit();
}
public void switchToTab3 (View v) {
ft.replace(R.id.fragment1, frt3).commit();
}
when i click on button2 the fragment class2 is successfully visible except that the default class of the fragment remain visible as well is there anyway to hide it and make the new fragment class active
Adding fragments in your xml file will make is static through out your application and you can not change it dynamically.
If you want to add Fragments dynamically then you need to add FrameLayout in your xml file and load all your fragments dynamically in that layout and replace it one another while loading fragments one after another.
Do below change in your layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<FrameLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
Remove fragment1 from your XML file (so container is empty by default on layout creation time) and add it from code (i.e. in onCreateView() of parent fragment or in onCreate() of Activity), as fragments cannot be removed out of hierarchy if they are part of XML layout file.
I'd also change container to be FrameLayout instead of LinearLayout if you plan to have just one child in it.