Fragment not being replaced - android

I have a FragmentActivity that is supposed to swap out the front screen fragment (with buttons) to the fragment that the button points to when one is clicked.
Suppose the button that invokes SourceListFragment is clicked. Both onCreate() and onCreateView() of SourceListFragment are called, but somehow the screen stays the same, i.e. the front screen fragment isn't getting replaced.
I wonder what I'm doing wrong. Here's my code.
public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}
#Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.main_screen, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}
public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
Log.d("SOURCELIST", "SourceListFragment onCreateView\n");
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
return view;
}
public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}
// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}
Here's the layout:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="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:background="#FFF"
android:orientation="vertical"
android:id="#+id/container">
</FrameLayout>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="#+id/banner">
</RelativeLayout>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
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">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
fragment_sourceitem_list.xml:
<?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:orientation="vertical">
<ListView android:id="#android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="#+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />
</FrameLayout>
Any help will be appreciated.
Thanks

You add a Fragment into Linear layout with id main_screen, but the fragment should be added into FrameLayout. The transitions do work in case of LinearLayout, but the newly added Fragment is outside of the display.

Related

Set and call Fragment Tag correct

Can't get rid of this problem. I saw a couple other questions with a similar problem, but nothing fixed it.
Short story: I replace my placeholders with fragments in my MainActivty and would like to call a public method from that new fragment. But calling the fragment from his Tag, leads to a "Null object reference" error.
I tried multiple ways to set and call the Tag but without success
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/mainActivity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:menu="#menu/quick_settings"
tools:context="....">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
...
Main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annual_service);
createList();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void createList() {
Resources r = getResources();
String packageName = getPackageName();
//Array with all Fragments
fragmentList = new Fragment[]{
new FragmentA,
new FragmentB,
new FragmentC,
...
};
numberOfFragments = fragmentList.length;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int arrayIndex=0, fragmentNumber=1; fragmentNumber <= numberOfFragments; arrayIndex++, fragmentNumber++){
fragmentTransaction.replace(r.getIdentifier("fragment" + fragmentNumber, "id", packageName), fragmentList[arrayIndex], "fragment0"+fragmentNumber); // also try to set the TagName
}
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold(); // Method is implemented by a interface
FragmentA.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:id="#+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="20dp"
android:paddingStart="45dp"
android:visibility="gone">
...
</LinearLayout>
FragmentA.java
Button backBtn;
TextView header;
LinearLayout llayout;
Drawable defaulBG;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return setListener(inflater, container);
}
private View setListener(LayoutInflater inflater, ViewGroup container){
view = inflater.inflate(R.layout.fragment_general_information, container, false);
header = (TextView) view.findViewById(R.id.generalInformationHeaderText);
header.setOnClickListener(this);
llayout = (LinearLayout) view.findViewById(R.id.content);
defaulBG = inForkliftBtn.getBackground();
backBtn = (Button) view.findViewById(R.id.backBtn);
setRetainInstance(true);
return view;
}
public void unfold(){
if(llayout.getVisibility() == View.GONE){
llayout.setVisibility(View.VISIBLE);
}else{
llayout.setVisibility(View.GONE);
}
}
Call executePendingTransactions() on fragment manager after committing transaction
...
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold();
EDIT 1
You can try another option:
Do not add the executePendingTransactions()
In fragment's onViewCreated, if getTag() mathes "fragment01" call unfold method

Fragment is overlapping

I have created an app when i'm testing my app or i clicked on back button my fragment is overlapping other fragment. I don't know why it's happening. Please help me i need help..
My codes
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
initFragment();
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
android:background="#ff51b46d"
tools:context="com.example.hldev.docup.MainActivity">
<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.hldev.docup.MainActivity">
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Uploads.java
public class Uploads extends Fragment implements View.OnClickListener{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_uploads, container, false);
ivAttachment = (ImageView)view.findViewById(R.id.ivAttachment);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_email = (TextView)view.findViewById(R.id.tv_email);
bUpload = (Button)view.findViewById(R.id.b_upload);
tvFileName = (TextView)view.findViewById(R.id.tv_file_name);
ivAttachment.setOnClickListener(this);
bUpload.setOnClickListener(this);
return view;
}
private void goToProfile(){
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,profile);
ft.commit();
}
}
Just set a background color to your new <fragment /> in XML file or else it will be transparent and show a replaced fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="schemas.android.com/apk/res/android";
xmlns:tools="schemas.android.com/tools";
android:id="#+id/frag24"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green">
<!-- Your Content -->
</RelativeLayout>
</FrameLayout >
set white background color of your parent layout of your activity_uploads. also do same with all layout which you use in Fragment.
replace your code with below.
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
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"
android:background="#android:color/holo_green_dark"
tools:context="com.example.hldev.docup.Uploads">

ViewPager image gallery inside a Fragment

Hello im new android developer and i try to do the next things with no success
i have Activity, inside the activity i have fragment and inside it i have ** view pager image galley**.
the problem is when i run the app with the activity with the fragment and and gallery inside it the app crash
but when i tried to run the activity with the gallery and the fragment separately they works fine
whats the problem?
here my code:
Activity
public class infoActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_info);
}
}
Activity 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"
android:weightSum="1">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="co.checkit.mobile.checkit.Model.gallery_fragment"
tools:layout="#layout/imagegalleryfragment"
android:id="#+id/fragmentid" />
</LinearLayout>
ass you can see i set the fragment inside the activity
Fragment class
public class Hotelsgallery_fragment extends Fragment {
ImageAdapter adapter;
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.imagegalleryfragment, container, false);
viewPager = (ViewPager)myFragmentView.findViewById(R.id.viewpager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
return myFragmentView;
}
}
i set the gallery inside the fragment class
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/viewpager">
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
</LinearLayout>
instead of putting fragment inside xml, put frame layout
<?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"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/frame_layout_id" />
</LinearLayout>
create fragment inside a activity, inflate using frame layout id
YourFragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(R.id.frame_layout_id, fragment);
ft.commit();

Simple fragement is not working

I am practicing to multi-pane screen using fragment, but it's not working properly. I want two frame in single activity. One should be static and another will dynamic list.
main_biddingwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="200dp"
android:weightSum="1"
android:id="#+id/ag">
<fragment
android:layout_width="163dp"
android:layout_height="match_parent"
android:name="driver.project_detail"
android:id="#+id/fragment"
android:layout_row="0"
android:layout_column="0" />
<fragment
android:layout_width="184dp"
android:layout_height="match_parent"
android:name="driver.bid_list"
android:id="#+id/fragment2"
android:layout_row="0"
android:layout_column="19" />
</GridLayout>`
main.class
public class biddingWindow extends Activity{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
}
}
bidlist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
bid_list.java
public class bid_list extends Fragment {
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
return inflater.inflate(R.layout.bidlist,container, false);
}
}
output screen
Just Create the objects of both the fragments in activity and you can access everything in fragment
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ag">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="driver.project_detail"
android:id="#+id/fragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="driver.bid_list"
android:id="#+id/fragment2"/>
</LinearLayout>
class Main extends Activity
{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
FrameLayout frame1 = (FrameLayout)findViewById(R.id.fragment);
FrameLayout frame2 = (FrameLayout)findViewById(R.id.fragment);
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
frame1.removeAllView();
frame1.addView(fragment1)
frame2.removeAllView();
frame2.addView(fragment2)
}
}

App crashes after adding Toolbar to a Fragment

I'm trying to switch from an Activity with a fragment without a Toolbar (R.id.main_screen) to one that has a Toolbar (R.id.source_items_list).
However, whenever I have a Toolbar in R.id.source_items_list, it crashes. The error I'm getting is
android.view.InflateException: Binary XML file line #10: Error
inflating class android.support.v7.widget.Toolbar
which comes from the line
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false); in the following code.
I wonder what I'm doing wrong. Here's my code.
public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}
#Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.container, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}
public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
Log.d("SOURCELIST", "SourceListFragment onCreateView\n");
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
return view;
}
public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}
// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}
Here's the layout:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="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:background="#FFF"
android:orientation="vertical"
android:id="#+id/container">
</FrameLayout>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="#+id/banner">
</RelativeLayout>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
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">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
fragment_sourceitem_list.xml:
<?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:id="#+id/source_items_list"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"/>
<ListView android:id="#android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="#+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />
</FrameLayout>
Any help will be appreciated.
Thanks

Categories

Resources