TabLayout original fragment remaining on screen - android

I am using a Tablayout to switch between Fragments on an Activity; however, the Fragment that I originally assign to the Activity seems to not be replaced when tabs are clicked - a new fragment just seems to be added. This new fragment has the implementation I want the TabLayout to have. That is, when someone clicks a tab, the Fragment is replaced with the fragment associated with that Tab. I genuinely can not see what is causing this as I feel like I am replacing the entire fragment by referencing its ID in the XML file, and I don't think I assign the fragments anywhere else. The key snippets of code are below. I have attached pictures to help you see what I am talking about. Thank You!
Main code pertaining to TabLayout:
#Override
public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
super.onCreate(savedInstanceState);
popup = new LoadingDialog(this);
setContentView(R.layout.main_activity_layout);
SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
Boolean islogged = sharedPref.getBoolean("loggedin", false);
tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position){
case 0:
FragmentTransaction FTg = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg.replace(R.id.ma_layout, gen);
FTg.commit();
break;
case 1:
FragmentTransaction FTs = FM.beginTransaction();
Fragment sum = new FragmentOne();
FTs.replace(R.id.ma_layout, sum);
FTs.commit();
break;
case 2:
FragmentTransaction FTf = FM.beginTransaction();
Fragment forecast = new FragmentTwo();
FTf.replace(R.id.ma_layout, forecast);
FTf.commit();
break;
case 3:
FragmentTransaction FTr = FM.beginTransaction();
Fragment rates = new FragmentThree();
FTr.replace(R.id.ma_layout, rates);
FTr.commit();
break;
}
}
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"
android:id="#+id/main_activity"
>
<fragment
android:name="com.example.schadtj.portalapp.FragmentGeneral"
android:layout_gravity="center_horizontal"
android:id="#+id/ma_layout"
android:layout_width="match_parent"
android:layout_height="558dp"
>
</fragment>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:text="User Info"
android:icon="#drawable/information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Summary"
android:icon="#drawable/calculator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Forecast"
android:icon="#drawable/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Rates"
android:icon="#drawable/money_bag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TabLayout>

Change Like this in java:
#Override
public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
super.onCreate(savedInstanceState);
popup = new LoadingDialog(this);
setContentView(R.layout.main_activity_layout);
SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
Boolean islogged = sharedPref.getBoolean("loggedin", false);
tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position){
case 0:
FragmentTransaction FTg = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg.replace(R.id.ma_layout, gen);
FTg.commit();
break;
case 1:
FragmentTransaction FTs = FM.beginTransaction();
Fragment sum = new FragmentOne();
FTs.replace(R.id.ma_layout, sum);
FTs.commit();
break;
case 2:
FragmentTransaction FTf = FM.beginTransaction();
Fragment forecast = new FragmentTwo();
FTf.replace(R.id.ma_layout, forecast);
FTf.commit();
break;
case 3:
FragmentTransaction FTr = FM.beginTransaction();
Fragment rates = new FragmentThree();
FTr.replace(R.id.ma_layout, rates);
FTr.commit();
break;
}
}
}
FragmentTransaction FTg1 = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg1.replace(R.id.ma_layout, gen);
FTg1.commit();
}
Change fragmentto FrameLayout
<FrameLayout
android:layout_gravity="center_horizontal"
android:id="#+id/ma_layout"
android:layout_width="match_parent"
android:layout_height="558dp"/>

Related

How can I implement this particular "AnimatedBottomNavigationBar" for Android? Errors with methods

I implemented the BottomBar from this library https://github.com/Droppers/AnimatedBottomBar
I created the tabs XML which is
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/tab_home"
android:icon="#drawable/ic_action_home"
android:title="#string/Home" />
<item
android:id="#+id/tab_publish"
android:icon="#drawable/ic_action_publish"
android:title="#string/Publish" />
<item
android:id="#+id/tab_map"
android:icon="#drawable/ic_action_map"
android:title="#string/Map" />
<item
android:id="#+id/tab_profile"
android:icon="#drawable/ic_action_profile"
android:title="#string/Profile" />
</menu>
and have it in my activity's XML as
<?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="wrap_content"
android:layout_height="wrap_content"
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_bar"></androidx.fragment.app.FragmentContainerView>
<nl.joery.animatedbottombar.AnimatedBottomBar
android:id="#+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="#color/button_black"
app:abb_indicatorColor="#color/instagramPink"
app:abb_indicatorAppearance="round"
app:abb_indicatorHeight="4dp"
app:abb_indicatorMargin="16dp"
app:abb_selectedIndex="1"
app:abb_selectedTabType="text"
app:abb_tabs="#menu/tabs"
app:abb_badgeTextColor="#color/instagramPink"
app:abb_tabColor="#color/instagramPink"
app:abb_tabColorSelected="#color/white"
/>
</RelativeLayout>
Then the code for my activity is
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
switch (item.getItemId()) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
binding.bottomBar.setSelectedItemId(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}
However, those two commented lines are giving me an error saying the methods do not exist. This is how I normally do it with my navigation bars, but I can't seem to find the way of setting it up for this particular one despite going over the docs. Please help!
Thank you
Try this:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
//REPLACEMENT
switch (item.id) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
//REPLACEMENT
binding.bottomBar.selectTabById(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}

I can't find a way of successfully replacing one fragment with another on button click

I went through some questions and made the changes as I thought were necessary, but the application still crashes every time I press the button to replace fragments. The fragments have the usual code and are just for simple layouts. On the launch, the activity will display the fragment I first add.
Here's my MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(fl.isVisible())
{
fl.onActivityCreated(savedInstanceState);
fragmentTransaction.replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}
else {
fl.onActivityCreated(savedInstanceState);
fragmentTransaction.replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}
}
});
}
}
And here's my activitymain.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"
android:orientation="vertical"
tools:context="com.example.aditya.registrationnlogin.MainActivity">
<RelativeLayout
android:id="#+id/fragcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:layout_gravity="center_horizontal">
</RelativeLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="SWITCH" />
</LinearLayout>
You have to call fragmentManager.beginTransaction(); for every new transaction otherwise it will throw an exception
if(fl.isVisible())
{
//fl.onActivityCreated(savedInstanceState);
fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}else{
//fl.onActivityCreated(savedInstanceState);
fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}
and use setArgument to pass bundle without interrupting the fragment life cycle
// use this it will work sure dude !!
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager =
getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = null;
Class fragmentClass = null;
if(fl.isVisible())
{
fragmentClass = FragmentRegistration.class;
}
else {
fragmentClass = FragmentLogin.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate
(backStateName, 0);
if (!fragmentPopped && fragmentManager.findFragmentByTag(backStateName) ==
null){ //fragment not in back stack, create it.
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment, backStateName);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
});
}
}
if(fl.isVisible())
{
//put your argument like this
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fr.setArguments(args)
fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}else{
//put your argument like this
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fr1.setArguments(args)
fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}

startactivity in onClick button inside of navigationdrawer not work

I want to make a startActivity to MapaQRF.java from HomeF.java that is inside navigationdrawer.
I mean, I want the onclick (button) do the same as the onNavigationDrawerItemSelected (in MapaQRF pocision)
fragment_home.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" tools:context="cl.bikepurranque.www.bikeqrf.HomeF">
<Button
android:layout_width="190dp"
android:layout_height="wrap_content"
android:text="Mapa Paraderos QR"
android:id="#+id/btnMap"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:onClick="onClickMap"/>
</FrameLayout>
HomeF.java (Here's the problem)
public void onClickMap(View view){
Intent intent = new Intent(this.getActivity(), MapaQRF.class);
startActivity(intent);
}
NavDrawer.java
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = null;
switch (position){
case 0:
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 1:
fragment = new DetectarQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 2:
Intent intent = new Intent(this, Login.class);
startActivity(intent);
break;
}
}
Fixed
The problem was that was calling an activity when I needed to call a fragment.
removed: "android:onClick="onClickMap"" on Button (fragment_home.xml)
added in HomeF.java
(onCreateView) Method
btnMap = (Button) rootView.findViewById(R.id.btnMap);
btnMap.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v == btnMap){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.Fragment fragment = null;
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}
Try to use getActivity()
public void onClickMap(View view){
Intent intent = new Intent(getActivity(), MapaQRF.class);
getActivity().startActivity(intent);
}

Fragment Navigation between fragments

I'am new in Android and I work with fragments. I have activity (MainActivity) and 3 Fragments, A->B->C. I need to back navigate from C fragment to B, from B to A and backward. I was cliked on Fragment A see A, clicked on B see B, clicked on C see C, but when I clicked Back my app was closed. Why ?
Here is a example what I call fragments
Fragment Subcategory = new Subcategory();
Bundle bundle = new Bundle();
bundle.putInt("id", i);
String title = dba.getTitle(i,true);
bundle.putString("title", title);
Subcategory.setArguments(bundle);
FragmentTransaction transaction_cat = getFragmentManager().beginTransaction();
transaction_cat.replace(R.id.fragment_container, Subcategory);
transaction_cat.addToBackStack(null);
transaction_cat.commit();
I can't found any simple example which show me how to navigate between fragments, and after reading this article I don't understand how it works.
Tell me please, and write example how I must do this.
I think you can try override method onBackPressed() for the activity.
update:
Here is part of my code.
public class MainActivity extends Activity {
RelativeLayout mContainer;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (RelativeLayout) findViewById(R.id.fragment_container);
addFragment();
}
private void addFragment() {
FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragment_container);
if (f == null) {
f = new MainActivityFragment();
f.setArguments(getIntent().getExtras());
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, f);
ft.addToBackStack("" + count++);
ft.commit();
}
}
private void replaceFrament() {
FragmentManager fm = getFragmentManager();
Fragment f;
f = new BlankFragment();
f.setArguments(getIntent().getExtras());
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, f);
ft.addToBackStack("" + count++);
ft.commit();
}
public void onAddFragmentClick(View v) {
replaceFrament();
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Fragment"
android:layout_alignParentBottom="true"
android:onClick="onAddFragmentClick"/>
</RelativeLayout>

Start a Fragment From An Activity?

I Recently used this to start A Fragment From Activity in Navigation Bar Activity (In The OnListItemClick() ) :
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
The problem is that The fragment is not opening on top of Main Activity on Click of Button after
Instantiating the fragment with its default constructor .
But Now I'm trying to the same but its not working :
MainActivity.java
XML of MainActivity has A button As :
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/buttonflat"
android:onClick="startFrag"
android:layout_width="230dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:text="Button" />
and in Java Of MainActivity:(On Click Of Button )
public void startFrag(View v)
{
fragment = new Frag_FAB();
}
java of fragment:
public class Frag_FAB extends Fragment {
public Frag_FAB() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find_people,container,false);
return view;
}
}
Xml oF Fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#010008"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:text="#string/stuff"
android:textColor="#color/highlighted_text_material_dark"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp" />
</RelativeLayout>
add the fragment transaction after the switch statement:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
your code must be something like:
switch(position){
...
...
...
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
...
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment); //* Here you add the fragment! :)
You need to add it to the layout via a FragmentTransaction:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
You have to add your fragment to the FragmentManager. Here is an example :
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

Categories

Resources