Kotlin Fragment overlapping issue - android

I am using recyclerview. Then problem is when i click one of the row, new fragment should be open. It opens but, old fragment still appears. this is my screenshot:
As you see, when i click "opel" new fragment opens. But it is overlapping.
This is my fragment 1:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvarackategori"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my new fragment:
<?xml version="1.0" encoding="utf-8"?>
<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=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>
this is my code:
override fun userItemClick(position: Int) {
//super.userItemClick(position) No need of it.
when (position) {
0 -> {
var fragmentOpel=OpelYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentOpel)
transaction.commit()
Log.e("aki", "opel: " + position)
} //start a fragment
1 -> {
var fragmentChevrolet=ChevroletYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentChevrolet)
transaction.commit()
Log.e("aki", "chevrolet: " + position)
} //start a fragment

Add a background to your new Fragment:
<?xml version="1.0" encoding="utf-8"?>
<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/white"
tools:context=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>

Basically that's how fragments work.
The methods name replace might be misleading, it doesn't replace the fragments it only lay a fragment in a layout.
so if you want the other fragment to be dismissed you have to remove it. otherwise just setting a background for your 2nd Fragment should workaround your issue

Related

add same fragment in the same framelayout activity

I want to add dynamic inputs using one fragment, this fragment includes one input and everytime that I click the button the fragment will be insert in the activity layout,
actually I can do it but when I click the button for the second time it's add the fragment input but is overlaps with de old fragment input
inputFragment.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=".fragments.InputFragment">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/contInput"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Usuario"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
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:id="#+id/cont"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".controller.Activity">
<Button
android:id="#+id/btn"
android:layout_width="324dp"
android:layout_height="81dp"
android:layout_marginTop="82dp"
android:text="Insert input"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_container"
tools:ignore="MissingConstraints" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="414dp"
android:layout_height="289dp"
android:layout_marginTop="36dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="-2dp" />
</androidx.constraintlayout.widget.ConstraintLayout >
activity.kt
class Activity : AppCompatActivity(){
private var myList = ArrayList<InputFragment>()
private lateinit var binding: ActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btn.setOnClickListener {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
val nuevoFragmento = InputFragment()
fragmentTransaction.add (R.id.fragment_container, nuevoFragmento)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
}
}
This is not achievable with fragments, when a new fragment is added to the fragment container, the old will not be visible. I will suggest using something that allows list based views such as the Recyclerview. But for a simple small number of inputs, manually hiding inputs and toggling the visibility is the simplest solution

Shared Element Transition from Activity to Fragment

I have a simple Activity with an ImageView:
<RelativeLayout
android:id="#+id/rl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:transitionName="#string/pic_viewer_transition" />
</RelativeLayout>
I want to share this ImageView to the Fragment in this Activity. Here's Fragment XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<ImageView
android:id="#+id/iv_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:transitionName="#string/pic_viewer_trasition"/>
Here's my code to provide Sharing:
iv_picture.setOnClickListener {
val f = PictureViewer()
val args = Bundle().apply {
putParcelable("TESTING", picInfo)
}
f.arguments = args
f.sharedElementEnterTransition = TransitionCustom()
f.sharedElementReturnTransition = TransitionCustom()
supportFragmentManager.beginTransaction()
.addSharedElement(iv_picture, activity.getString(R.string.pic_viewer_transition))
.replace(R.id.rl_container, f)
.addToBackStack(null)
.commit()
}
But nothing happens. This code creates and shows my Fragment without any transition. What am I doing wrong?

Android BottomNavigationView overlaps Recyclerview

I have a BottomNavigationView and both my fragments have a RecyclerView which content is partially hidden under the BottomNavigationView
Lots of people seem to have had this problem yet I tried all of the answers (make rootview LinearLayout and set height of FrameLayout to 0 etc...) and none of them work.
This is my activity_main.xml file
<?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"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:itemTextColor="#color/color12"
app:itemIconTint="#color/color12"
app:menu="#menu/menu_sample_store">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
this is how I initialize it (MainActivity.java)
private void initHandlers()
{
this.adapter = new ViewPagerAdapter(getSupportFragmentManager(),this);
viewPager.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id)
{
case R.id.menu_option_local:
fragment = new AlphaListFragment(getApplicationContext());
break;
case R.id.menu_option_store:
fragment = new BetaListFragment(getApplicationContext());
break;
}
setActiveFragment(fragment);
return true;
}
});
}
private void setActiveFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, fragment).commit();
}
And if it might help, this is the xml file of a fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewBeta"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="#+id/loadingBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
EDIT: SOLUTION FOUND
Make the rootview of activity_main a LinearLayout
Set its orientation to vertical
Set the layout_weigth of the FrameLayout to 1
Make sure that the FrameLayout has a height of 0dp

Bottom nav view - one activity and two fragments

I'm trying to make a bottomNavigation that goes between the main activity, and two fragments. Right now I went after a tutorial and created a third fragment:
Here's the bottomNavigation listener:
private BottomNavigationView.OnNavigationItemSelectedListener bottom_navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.Piano:
setTitle("Pitch Piano");
PianoFragment fragment = new PianoFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frame, fragment, "Piano");
fragmentTransaction1.commit();
return true;
case R.id.Rhythm:
setTitle("Rhythm");
Rhythm fragment2 = new Rhythm();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frame, fragment2, "Rhythm");
fragmentTransaction2.commit();
return true;
case R.id.Intervals:
setTitle("Intervals and Chords");
Intervals_and_chords fragment3 = new Intervals_and_chords();
android.support.v4.app.FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frame, fragment3, "Intervals");
fragmentTransaction3.commit();
return true;
}
return false;
}
};
Main Activity layout (shortened):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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.stefanawesome.piano.MainActivity">
<RelativeLayout
android:layout_width="581dp"
android:layout_height="503dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:id="#+id/lin_lay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.031"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/upperback"
android:layout_alignStart="#+id/upperback"
android:layout_below="#+id/myRectangleView"
app:itemBackground="#color/bottomNavView"
app:itemIconTint="#drawable/bottomnavbutton"
app:itemTextColor="#drawable/bottomnavbutton"
app:menu="#menu/bottom_nav_items" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Rhythm Fragment layout:
<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="com.stefanawesome.piano.Rhythm">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/rhythmtoolbar"
/>
</RelativeLayout>
</FrameLayout>
Interval Fragment layout:
<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="com.stefanawesome.piano.Intervals_and_chords">
<!-- TODO: Update blank fragment layout -->
</FrameLayout>
Screenshot when the bottom nav bar is on the rhythm section:
Instead of jumping between the main activity, and then the two fragments, the main activity layout never goes away, even if you go to one of the fragments, the fragment layout is behind the main layout.
Also, if I added code in any of the fragments, the program crashes.

changing view to a view with fragment in android

im trying to use setContentView to switch to a layout with a fragment inside it
but it crashes every time
what should i do?
this is my entire code:
(it must switch to fragment_layout when i click the button in the activity_main)
(fragmant_layout has a fragment and the layout for the frgment is te layout1)
(it crashs when i cick the button to switch)
main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void switchlayout(View view) {//when the button in clicked
setContentView(R.layout.fragment_layout);
}
}
the fragment class:
public class frgment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout1,container,false);
}
}
activity_main.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"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".fragmenttest.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="switchlayout"
android:layout_marginTop="74dp"
android:id="#+id/button" />
</RelativeLayout>
fragment_layout.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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="alirezamellat.fragmenttest.frgment"
></fragment>
</LinearLayout>
layout1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"></View>
</LinearLayout>
Please consider the following points::
On your parent layout (activity_main.xml), inside the Relative layout, add another layout called FrameLayout.
On your fragment layout(fragment_layout.xml), remove the fragment and add the layouts of layout1.xml to fragment_layout.xml
On your fragment class (MyFragment), inflate the fragment_layout.xml to the container and return the view (class name should start with uppercase letter, so name it like MyFragment)
On Activity class, place the following code on onclick listener.
//onClick on OnClickListener
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_layout, fragment)
.commit();
Once the layout of an Activity has been set it can not be changed at a later time.
There are a number of ways you can achieve this from which the easiest would be to have a wrapper layout and then injecting the fragment inside it using the FragmentManager class.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragmenttest.MainActivity">
<FrameLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="switchlayout"
android:layout_marginTop="74dp"
android:id="#+id/button" />
</RelativeLayout>
And then on your button click add your fragment.
getSupportFragmentManager().beginTransaction().add(R.id.wrapper, MyFragment.newInstance(), "FragTag").commit();
Remember that you have to take care of your button since it will still be visible on top of you fragment.

Categories

Resources