open fragment under parent activity - java

I want to open a fragment in activity but under objects of Activity.
in my code RelativeLayout is parent and LinearLayout is child
but when open fragment on relativelayout I lose linearlayout :(
this is my code:
Main Activity Java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Button fragment1 , fragment2;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
fragment1 = this.findViewById(R.id.fragment1);
fragment2 = this.findViewById(R.id.fragment2);
relativeLayout = this.findViewById(R.id.relativeLayout);
fragment1.setOnClickListener(this);
fragment2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fragment1:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.relativeLayout , new fragment1());
ft.commit();
break;
case R.id.fragment2:
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.relativeLayout , new fragment2());
ft2.commit();
break;
}
}
}
Main Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/layout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
fragment1 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">
</android.support.constraint.ConstraintLayout>
fragment2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F">
</android.support.constraint.ConstraintLayout>
This is Main Activity: enter image description here
and when click on fragment1: enter image description here
Plz Help Me :)
TNX

Add a framelayout in relativelayout before the linearlayout and inflate the fragment in the framelayout.
<RelativeLayout
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=".MainActivity"
android:id="#+id/relativeLayout">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>

I think you should change your Fragment1 XML and Fragment2 XML from Constraint Layout to LinearLayout. It should work else initiate your LineaLayout in your MainActivity.

You are inflating fragments on Parent Layout. Make two child of Relativelayout and inflate fragment one of them like this:
<RelativeLayout 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=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
<!-- inflate fragments on FrameLayout -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout">
</FrameLayout>
</RelativeLayout>
Inflate your fragments on this FrameLayout
....Hope this helps :)

Related

How to open a new fragment inside the same fragment using viepager and tablayout

I have an application that contains TabLayout with 5 tabs.
Now the need arose to click on an item in the RecycleView to open a new fragment within the Fragment that the user was already using, as if clicking on the item opened the subitems
My problem is that it uses ViewPager(Container)
In the parent Fragment I have the following code
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position){
// MyListModel model = mList.get(holder.getAdapterPosition());
switch(holder.getItemViewType()){
case 1:{
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.Name.setTextColor(Color.parseColor("#245251")); //Todo Muda a cor da linha dos itens do RecycleView 08-08-2022
String nome = telemensagens.get(position).getName();
//String pasta = nome.substring(0,nome.lastIndexOf('-') );
String pasta = nome.lastIndexOf('_')>0 ? nome.substring(0,nome.lastIndexOf('_') ) : nome;
String index = nome.substring(nome.lastIndexOf('_') + 1).trim();
viewHolder.Name.setText(pasta);//telemensagens.get(position).getName());
viewHolder.Email.setText(telemensagens.get(position).getUrl());
final String music = telemensagens.get(position).getUrl();
viewHolder.Name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment myFragment;
/* Intent intent = new Intent(context, TelemensagensItensActivity.class);
intent.putExtra("PASTA", telemensagens.get(position).getName() );
context.startActivity(intent);*/
Bundle bundle = new Bundle();
String nome = telemensagens.get(position).getName();
if(MainActivity.nivelPasta==0)
MainActivity.nivelPastaFinal = nome.lastIndexOf('_')>0 ? Integer.parseInt(nome.substring(nome.lastIndexOf('_') + 1).trim()) : 1;
//MainActivity.nivelPastaFinal = Integer.parseInt(telemensagens.get(position).getName().substring(telemensagens.get(position).getName().length()-1,telemensagens.get(position).getName().length()));
MainActivity.nivelPasta++;
if(MainActivity.nivelPasta==MainActivity.nivelPastaFinal) {
myFragment = new AudiosItensActivity();
bundle.putString("PASTA",telemensagens.get(position).getName() );
MainActivity.audios.add(telemensagens.get(position).getName());
}
else {
myFragment = new AudiosActivity();
MainActivity.audios.add(telemensagens.get(position).getName());
bundle.putString("PASTA", MainActivity.nomePasta+ telemensagens.get(position).getName() );
MainActivity.nomeAudio += telemensagens.get(position).getName() + "/";
}
AppCompatActivity activity = (AppCompatActivity) context;
myFragment.setArguments(bundle);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, myFragment,"Audio").
addToBackStack(null).commit();
}
});
break;
}
case 2:{
break;
}
}
}
In this specific piece of code is the click of the item in the recycleView, when clicking on it I can open a new child fragment, BUT it ends up killing my TabLayout, that is, the tabs stop working and I can no longer navigate in the other tabs
AppCompatActivity activity = (AppCompatActivity) context;
myFragment.setArguments(bundle);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, myFragment,"Audio").addToBackStack(null).commit();
So my question is: How can I replace a single fragment and not my entire container with all fragments
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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="com.testapp.EventActivity">
<RelativeLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="100dp">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/adViewMob"
android:layout_weight="1">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:tabTextAppearance="#style/MinhaCustomTabText" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adViewMob"
android:layout_marginBottom="2dp"
android:animateLayoutChanges="true"
android:fitsSystemWindows="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adViewMob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_marginTop="5dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/ad_unit_id" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="170dp">
<com.dvinfosys.ui.NavigationListView
android:id="#+id/expandable_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:divider="#null"
android:groupIndicator="#null"
android:scrollbars="vertical" />
</ScrollView>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
fragment_container.xml
<FrameLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
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:layout_above="#id/adViewMob"
tools:context="com.mobileboss.bomdiatardenoite.ContainerFragment"
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adViewMob"
android:layout_marginBottom="0dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?actionBarSize"
android:id="#+id/rltLayout"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
</RelativeLayout>
</FrameLayout>

Fragment replaces entire screen

I have the following structures and fragment replace/add does not only replace its host view but whole screen. What can cause this?
Main Activity layout without data
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
app:labelVisibilityMode="unlabeled"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</LinearLayout>
Fragment layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
tools:context=".fragment.HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:background="#drawable/transparent_button"
android:onClick="#{viewModel::onFloatingButtonClicked}"
android:src="#drawable/ic_baseline_add_24" />
</FrameLayout>
Main activity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this, new ViewModelFactory(getApplication())).get(MainViewModel.class);
activityMainBinding.setViewModel(mainViewModel);
activityMainBinding.executePendingBindings();
fragmentList.put(FragmentsEnum.HOME.getValue(), new HomeFragment());
fragmentList.put(FragmentsEnum.PROFILE.getValue(), new ProfileFragment());
Fragments.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment, fragmentList.get(FragmentsEnum.HOME.getValue()), FragmentsEnum.HOME.getValue()); // DEFAULT FRAGMENT
}
Fragments util
public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().add(containerId, fragment, tag).commit();
}
}
On onview of main activity I am setting Home Fragment active when the activity starts. But it hides my bottom nav bar and replaces entire screen.
I think that this is a problem with the usage of LinearLayout as the root element for the fragment and bottomnav. Try replacing it with either ConstraintLayout or CoordinatorLayout. If none of those work try removing the top padding from the Fragment.
Your FrameLayout will just overlap the BottomNavigationView. Furthermore you should use the FragmentContainerView for your nav host. You'd want to have your FragmentContainerView explicitly above your BottomNavigationView. I changed your LinearLayout to a ConstraintLayout with proper constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

Add fragment below another overlapping

I'm was trying to display fragment below another fragment, but i'm facing Issue with view, when i invoke the 2nd fragment that should come below first fragment it's overlap.
My First fragment container RelativeLayout with Id mainLayout and 2nd fragment with Id carLayout.
I assume the issue with XML code.
My XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/topLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainLayout">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/carLayout">
</RelativeLayout>
</LinearLayout>
i need to set fragment 2 below fragment 1
but it'as overlapping.
as I understand you need to set two fragment in same view
// add your first fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container1 , YOUR_FIRST_FRAGMENT).commit;
// add your second fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container2 , YOUR_SECOND_FRAGMENT).commit;
<linearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<FrameLayout
android:id="#+id/frame_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/frame_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</linearLayout>
or simply
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="YOUR FRAGMENT CLASS"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>
carLayout is a RelativeLayout so new fragment will be over the last one, change it to a LinearLayout with orientation vertical.
You need another view below your carLayout.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/anotherLayout"
android:layout_below="#id/carLayout">
</RelativeLayout>
The 'add' method adds a fragment to a view, so you can modify your method to use a different container every time:
public void replaceFragment(Fragment someFragment, int containerId) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(containerId , someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
case R.id.home_menu:
fragment = new mainFrog();
replaceFragment(fragment,R.id.carLayout);
public void Haraj_cars(View view) {
fragment = new carFrog();
replaceFragment(fragment,R.id.anotherLayout);
}

ViewPagerIndicator not showing up

I am using JakeWharton's ViewPagerIndicator on my landscape app but I think I am messing up with the layout. The ViewPagerIndicator does not show up. I just started learning Android Development so any help is appreciated!
My guess is that it has to do with my Linear Layout...should it be relative? Am I covering up the pager indicator? I know that if I move the indicator code above the view pager, I see the indicator but everything else is white. My fragment's layout is a relative layout with height and width matching the parent.
My app is an activity with fragments.
Edit: I set the indicator height to wrap content but now it floats on the top of the screen. How do I make it go down?
My_Activity.java
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
CirclePageIndicator circleInd = (CirclePageIndicator) findViewById(R.id.circles);
circleInd.setViewPager(pager);
circleInd.setStrokeColor(0xAA000000);
//circleInd.setCurrentItem(mAdapter.getCount() - 1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0: return FirstFragment.newInstance("First");
case 1: return SecondFragment.newInstance("Second");
default: return FirstFragment.newInstance("First");
}
}
#Override
public int getCount() {
return 2;
}
}
}
and the 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.frontrowqa.myfirstapplication.MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_gravity="bottom" />
</LinearLayout>
And my fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="#+id/PasscodeText"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:hint="Passcode"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/ipAddressText"
android:layout_below="#id/PasscodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/intercomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ipAddressText"
android:hint="Intercom Event Number"
android:backgroundTint="#android:color/black"/>
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
Set your activity xml like this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp" />
</LinearLayout>

Overlapping layouts using fragment transaction android

So I am trying load my login fragment from my main activity. I think I have a problem not replacing the whole layout in when I use the fragment transcation replace() method.
Here is my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
#Override
protected void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LoginFragment lf = new LoginFragment();
ft.replace(R.id.mapView, lf);
ft.addToBackStack("map to login");
ft.commit();
}
activity_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:showIn="#layout/activity_maps">
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/activity_maps" />
<Button
android:id="#+id/btn
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_fragment.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"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LoginActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:inputType="textPersonName"
android:text="#string/Username"
android:ems="10"
android:id="#+id/username"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:inputType="textPersonName"
android:text="#string/Password"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/ForgotPassword"
android:id="#+id/forgotPassword"
android:layout_gravity="center_horizontal"
android:textColor="#197cff" />
<Button
android:id="#+id/GoogleButton"
android:layout_width="215dp"
android:layout_height="wrap_content" />
</LinearLayout>
Any recommendations on how I should restructure my xml files?
Thanks for your time!
Any recommendations on how I should restructure my xml files?
Your problems have nothing to do with the xml layouts. First of all embedding that MapFragment in the activity layout and then replacing it with another fragment as the activity starts(onStart()) doesn't make any sense at all. Secondly, embedded fragments(meaning fragments declared in the xml with the fragment tag) can't be used in fragment transactions, they are meant to be static. So remove the MapFragment from the xml layout and do a simple transaction to add the LoginFragment in a empty ViewGroup from the activity layout.

Categories

Resources