ID cannot be found - android

I have created an Activity containing a FrameLayout with the ID "container", though I cannot refer to this view, I have tested it in a simple reference:
activity 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"
android:gravity="center_horizontal"
android:orientation="vertical"
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="app.chrono.name.LoginActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment()); //container is written in red
}
I do not understand why this container cannot be refered to. It should be accessible. It is written in red and seems to not exist.

Your code is right,
Click on the line that is below your code findviewbyid and one suggestion comes press alt+enter that's all.
Also try to invalidate cache and restart :
Go to file and invalidate cache and restart.

Container is a view but in specific it is a layout view i.e FrameLayout.
So change the type to FrameLayout instead of View.
FrameLayout container = (FrameLayout)findViewById(R.id.container);
EDIT:
Also if you find first solution not working add the id directly in the fragment transition like this.
fragmentTransaction.add(R.id.container, new LoginFragment());

You should commit(); your fragment transaction.
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
fragmentTransaction.commit();

First remove this line from your code
container = findViewById(R.id.container);
Use this function for opening fragment
public void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
}
and call in onCreate like this
loadFragment(new LoginFragment());
And make sure you import these
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

Related

Android - Call a fragment inside tab

I try to create an application that has two pages. First page is a tabbed page and second page is a detail page.
I create a tapped page using this guide. I have a mainactivity that contains view pager and 3 tabs(Tab1, Tab2, Tab3). Tab3 contains a button to navigate detail page. Each tab has own .java(extends Fragment) and .xml file. I edit fragmentThree.xml layout :
<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:id="#+id/fragmentThree"]>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/goToDetail"
android:text="GO TO DETAIL" />
I create a new fragment for detail page. I set onclick method inside of Tab3.java;
goToDetail.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.mainActivity, new Detail());
transaction.commit();
}
});
When I press the button, it doesn't do anything. I don't replace the fragment.
Use you viewpager id instead of main layout id, where you set the tab layout fragment.
transaction.replace(R.id.viewpager, new Detail());
use Frame layout in Main Activity
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent">
In clicklistiner :-
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.parent, new Detail());
transaction.commit();

Fragments overlapping on FrameLayout

I am trying to add multiple fragments to a FrameLayout. I need to add them one below another. But they are overlapping.
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Create instance of fragments
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentSecondaryList secFrag = new FragmentSecondaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();
}
}
They looks like this. How can I solve this?
Use the replace method instead of the add method.
The replace method hides the current fragment before adding new one, the add method simply adds the new fragment without disposing off the older fragment.
In order to retain the back stack, use the fragment transaction with backstack management.
I need to display both fragments at same time, one below another
You can try the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Now add fragmments using:
fragmentTransaction.add(R.id.fragment_container1,firstFrag);
make sure you Fragment's layout_height = "wrap_content"
Add background to your fragments and make "android:clickable="true" to parent layout.
Do this way:
First fragment
FragmentPrimaryList firstFrag = new FragmentPrimaryList();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// add fragment to the fragment container layout
fragmentTransaction.add(R.id.fragment_container,firstFrag);
fragmentTransaction.commit();
second fragment
FragmentSecondaryList secFrag = new FragmentSecondaryList();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,secFrag);
fragmentTransaction.commit();

How to call Fragment inside a Fragment

I have created a fragment, and used this inside another Fragment layout.
This is the XML of my layout.
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_facebook"
>
</WebView>
<fragment
class="com.loading.LoadingAnimation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_facebook_loadingAnimation"
/>
Now i want to get the ID of this fragment. I used following procedure but i am getting incompatible type error message.
Loading Animation loadingAnimation;
loadingAnimation = (LoadingAnimation) getFragmentManager().findFragmentById(R.id.wv_facebook_loadingAnimation);
Required LoadingAnimation Found android.support.v4.app.fragment
You need to call getChildFragmentManager() instead of getFragmentManager().
You may try below sample too:
DashboardFragment innerFragment = new DashboardFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
innerFragment .setEnterTransition(new Slide(Gravity.RIGHT));
innerFragment .setExitTransition(new Slide(Gravity.LEFT));
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
folderName = mFileList.get(position).getName();
fragmentTransaction.add(R.id.content_frame, innerFragment, folderName);
fragmentTransaction.addToBackStack(folderName);
fragmentTransaction.commit();

issue with fragment painting over other fragment. How do I replace a fragment?

I have code like this to take my currently shown fragment in my activity and show a different fragment. Sometimes the first view is displayed on top of the other view.
What could cause that? Is there a better way to do this?
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.remove(from);
fragmentTransaction.commit();
getFragmentManager().executePendingTransactions();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, to);
if(showBackButton) {
fragmentTransaction.addToBackStack(to.toString());
}
fragmentTransaction.commit();
You might try the replace command instead. Something like:
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, iFrag);
transaction.commit();
The fragment_container is just a FrameLayout inside its own XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#888888" />
iFrag is an instantiated class that extends Fragment.

How can I add a fragment from within a viewpager that covers the viewpager tabs

Currently I have a viewpager setup according to the documentation. Within the first fragment tab I have setup a recycler view that contains a list of cardviews. Within each cardview I have a button which should open a new fragment. I would like this new fragment to overlap the action bar tabs (something similiar to how facebook comments work when you click the comment button). I am not sure how to get it working. I created an extra framelayout and relative layout above my viewpager hoping that it would overlap, however this doesn't seem to work. How can I have my new fragment cover the action bar tabs as well? I am not asking how to replace the current viewpager fragment if that makes sense.
I filled the new fragment with a red background, the result after envoking the onclick listener is shown here: http://i.imgur.com/yHYpzGr.png
In my onclick listener I have:
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, commentFragment,"comment_fragment_tag");
fragmentTransaction.commit();
My main activity layout that contains the viewpager:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/main_activity">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</FrameLayout>
Try this .. hope it works
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, commentFragment,"comment_fragment_tag");
fragmentTransaction.commit();
or
Fragment fr = new FragmentTwo() ;
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fr);
ft.commit();
hear fr is the new fragment that would be replaced

Categories

Resources