I am trying to make a Login screen where the Register, Login and Forgot Password screens are all fragments, placed like this:
Forgot Password | Login | Register
I found a simple video on YouTube showing me how to do this. The only issue I have is that when I start the Activity, it opens on the Forgot Password fragment since it is the first one, but I am wanting it to start in the middle, the Login fragment. Here is the code for the main activity. What should I change or add to this code or am I taking the wrong approach for what I am trying to achieve
Access.java
public class Access extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.access);
initializePaging();
}
private void initializePaging() {
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, AccessForgotPassword.class.getName()));
fragments.add(Fragment.instantiate(this, AccessLogin.class.getName()));
fragments.add(Fragment.instantiate(this, AccessRegister.class.getName()));
PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager accessViewPager = (ViewPager) findViewById(R.id.accessViewPager);
accessViewPager.setAdapter(mPagerAdapter);
}
}
access.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:id="#+id/accessMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".access.Access">
<android.support.v4.view.ViewPager
android:id="#+id/accessViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
AccessForgotPassword.java
public class AccessForgotPassword extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_forgot_password, container, false);
}
}
access_forgot_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessForgotPasswordMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
</LinearLayout>
AccessLogin.java
public class AccessLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_login, container, false);
}
}
access_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessLoginMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
</LinearLayout>
AccessRegister.java
public class AccessRegister extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.infalte(R.layout.access_register, container, false);
}
}
access_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessRegisterMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0000FF">
</LinearLayout>
Set
accessViewPager.setCurrentItem(1);
Try adding accessViewPager.setCurrentItem(1) in end of onCreate()
Try to set accessViewPager.setCurrentItem(1); in the onCreate() of your activity.
Just after setting adapter you can set manually set the second fragment to view pager
accessViewPager.setCurrentItem(1); // 0= ForgotPassword, 1=LoginScreen,...so on
Related
I wanted to implement a BottomNavigationView (toolbar at the bottom) in my app. My GoogleMap fragment is of type FragmentActivity, not fragment. Would this be a problem? Is there an easy way to convert it to a fragment? I've checked a lot of tutorials and none of them fit the situation I'm in or address it at all. Thanks in advance.
Hope this answer can help you !
Example Activity
public class ExampleActivity extends AppCopmactActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);
initFragment(savedInstanceState);
}
private void initFragment(Bundle savedInstanceState) {
if (findViewById(R.id.mainFragmentContainer) != null) {
if (savedInstanceState != null) {
return;
}
ExampleFragment exampleFragment = new ExampleFragment ();
getSupportFragmentManager().beginTransaction().add(R.id.mainFragmentContainer, exampleFragment ).commit();
}
}
Example Fragment
public class ExampleFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_example, container, false);
return view;
}
#Override
public void onViewCreated(final View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
example_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.Base">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/mainFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tool_bar" />
</LinearLayout>
I use fragments to display multiple views in one activity. I have 2 framelayouts in one linearlayout. The onCreate and onCreateView of the fragment gets called but the view of the fragment is not displayed. Is what i'm trying to do not possible? Or is there a way to fix issue?
Layout of the activity:
<?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=".view.activity.StandardFlowActivity">
<FrameLayout
android:id="#+id/standardFlowDownloadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/standardFlowBaseContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Layout of the fragment
<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.example.sennevervaecke.crossexperience.view.fragment.WedstrijdFragment">
<ListView
android:id="#+id/wedstrijdListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Fragment Class
public class WedstrijdFragment extends Fragment implements AdapterView.OnItemClickListener{
private ArrayList<Wedstrijd> wedstrijden;
private WedstrijdFragmentCom communication;
public WedstrijdFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreate is called");
super.onCreate(savedInstanceState);
wedstrijden = LocalDB.getWedstrijden();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreateView is called");
View view = inflater.inflate(R.layout.fragment_wedstrijd, container, false);
ListView listView = view.findViewById(R.id.wedstrijdListView);
WedstrijdAdapter adapter = new WedstrijdAdapter(getContext(), wedstrijden);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communication = (WedstrijdFragmentCom) activity;
} catch (ClassCastException e){
e.printStackTrace();
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
communication.onWedstrijdItemClick(wedstrijden.get(i));
}
}
Code in the onCreate of the activity to add the fragment:
wedstrijdFragment = new WedstrijdFragment();
getSupportFragmentManager().beginTransaction().add(R.id.standardFlowBaseContainer, wedstrijdFragment, "wedstrijd").commit();
Thanks in advance!
Your first layout standardFlowDownloadContainer width and height is match_parent , so the standardFlowBaseContainer FrameLayout is out of the screen. you can change your standardFlowDownloadContainer's height to 20dp and run your project , you will see your Fragment content.
Can somebody explain what is the problem here? The error is bellow:
Code in MainAcitivity:
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = (SvePonudeFragment) fm.findFragmentById(R.id.ponudice);
fragment.reloadData();
Fragment Class:
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ArrayList<Ponuda> listaPonuda = null;
initializeAdapter(listaPonuda);
return rootView;
}
private void initializeAdapter(List<Ponuda> preuzetePonude){
RVAdapter adapter = new RVAdapter(preuzetePonude);
rv.setAdapter(adapter);
}
public void reloadData(){
System.out.println("I ENTEEERED");
}
}
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:layout_height="match_parent"
android:id="#+id/ponudice">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/rv"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
What is wrong with this? Can somebody explain me?
this is the error:
W/System.err: java.lang.NullPointerException
W/System.err: at com.example.filip.dajsve.Activities.MainActivity.onDataLoaded(MainActivity.java:190)
W/System.err: at com.example.filip.dajsve.Loaders.DatabaseDataLoader.loadData(DatabaseDataLoader.java:22)
I found the problem. The problem is that you don't link your fragment with mainActivity. first you should add your fragment in xml of mainActivty and give the fragment an id then access it in the main activity with the id of fragment not using this id android:id="#+id/ponudice"> because this id related with relative layout not the fragment. check the xml code of main activity you should find fragment tags like code below
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="theNameOfYourPackage.theNameOfFragment"
android:id="#+id/ponudice_in_main_activity"/>
Do it like this
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = new SvePonudeFragment();
fm.beginTransaction().replace(R.id.framecontainer,fragment).commit();
and your activity_main.xml file will be
<FrameLayout 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:id="#+id/framecontainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="16dp"
>
</FrameLayout>
I'm new to android development and having some issues with replacing a fragment with another fragment.
In my MainFragment.java, the first thing I do in onCreate is check for internet connectivity, and if there is not, I replace that fragment with the InternetCheckFragment. However, in InternetCheckFragment.java, when I try to inflate my check_internet.xml, the app closes, but there is no error.
activity_main.xml:
<fragment 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/main_browse_fragment"
android:name="com.ui.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ui.MainActivity"
tools:deviceIds="tv"
tools:ignore="MergeRootFrame" />
MainActivity.java:
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainFragment.java:
public class MainFragment extends VerticalGridFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (isConnectedToInternet() == true)
{
// DO stuff
}
else
{
showInternetError();
}
}
public void showInternetError()
{
getFragmentManager().beginTransaction().replace(R.id.show_internet_error , new InternetCheckFragment())
.commit();
}
....
}
InternetCheckFragment.java:
public class InternetCheckFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
return inflater.inflate(R.layout.check_internet, parent, false);
}
}
check_internet.xml:
<?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:id="#+id/show_internet_error"
android:background="#color/black_transparent">
<Button android:id="#+id/btn_retry"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="RETRY"
android:layout_centerInParent="true"/>
</RelativeLayout>
However, I'm getting the error No view found for id.
I'm sure I'm not setting something correctly? Can someone point me in the right direction?
Thanks!
You are inflating a Fragment in a Fragment using the Fragment's FragmentManager.
This FragmentManager cannot handle this.
Try using the SupportFragmentManager with AppCompatActivity.getSupportFragmentManager() and the support Fragments from android.support.v4.app.Fragment
Ugh!..it was a stupid mistake, I replaced R.id.show_internet_error within getFragmentManager().beginTransaction().replace(R.id.show_internet_error , new InternetCheckFragment()).commit(); with R.id.main_browse_fragment and now it works.
I have question, because I do not know what is wrong. Does not display the activity of activity_main_fragment.xml me and I do not know why. Do you have any suggestions what to change?
I created two fragments fragment_a and fragment_b and I want to display them connected to the fragment_a when you click on an item in a listview information came to me in fragment_b.
Communicator.java
public interface Communicator {
public void respond(int i);
}
strings.xml
<string-array name="titles">
<item>Zmiany</item>
<item>Ilę</item>
<item>Trudne pytania</item>
</string-array>
<string-array name="descriptions">
<item>Zmianysadas</item>
<item>Ilęvcxcvcx</item>
<item>Trudne pytaxcvcxvxcnia</item>
</string-array>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFBB00" >
<ListView
android:id="#+id/listView22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#99CC00">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
activity_main_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/fragment1"
android:name="com.odpad.odpadygdansk.FragmentA"
android:layout_width="150dp"
android:layout_height="match_parent" />
<fragment
android:id="#+id/fragment2"
android:name="com.odpad.odpadygdansk.FragmentB"
android:layout_width="470dp"
android:layout_height="match_parent" />
</LinearLayout>
FragmentA.java
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{
ListView list;
Communicator communicator;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
communicator = (Communicator) getActivity();
list = (ListView) getActivity().findViewById(R.id.listView22);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return inflater.inflate(
R.layout.fragment_a, container, false);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
communicator.respond(i);
}
}
FragmentB.java
public class FragmentB extends Fragment{
TextView text;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
text = (TextView) getActivity().findViewById(R.id.textView);
return inflater.inflate(
R.layout.fragment_b, container, false);
}
public void changeData(int i)
{
Resources res = getResources();
String[] descriptions = res.getStringArray(R.array.descriptions);
text.setText(descriptions[i]);
}
}
EcologicalInformationActivity.java
public class EcologicalInformationActivity extends FragmentActivity implements Communicator{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
}
#Override
public void respond(int i){
FragmentManager manager = getFragmentManager();
FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
f2.changeData(i);
}
}
EcologicalInformationActivity extends FragmentActivity so you should call getSupportFragmentManager() in place of getFragmentManager(). See documentation here. If your fragments are not already instances of the support library version, you'll need to change that too.