I have to create an application in which I have to work with Fragment.
In the MainActivity, there is a webView. From the SecondActivity I
have starting using Fragment.
Here is the code of SecondActivity:
package com.dev.testapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class Second extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public void choosefragment(View view) {
Fragment fg;
if(view == findViewById(R.id.secondbtn2)) {
fg = new SecondFragment();
}
else
{
fg = new FirstFragment();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frag1, fg);
fragmentTransaction.commit();
}
}
Hhere is its second.xml file:
<?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="#ffffff" >
<Button
android:id="#+id/secondbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first fragment"
android:onClick="chooseFragment" />
<Button
android:id="#+id/secondbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second fragment"
android:onClick="chooseFragment"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.dev.testapp.fragment1"
android:id="#+id/frag1"
/>
</LinearLayout>
After that I created two more classes for Fragments as it is required
code of FirstFragment:
package com.dev.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1,container, false);
}
}
And here is the SecondFragment:
package com.dev.testapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2,container, false);
}
}
When I am done with all this, my SecondActivity is showing an error that I don't know how to handle:
As noted in previous answers, your problem is incorrect Fragment library for FirstFragment.
Change line 5 of FirstFragment.java:
import android.support.v4.app.Fragment;
to:
import android.app.Fragment;
Your second fragment is of the type android.app.Fragment while your first fragment is android.support.v4.app.Fragment.
your activity use this import android.app.Fragment;
your fragment is import android.support.v4.app.Fragment;
So you either have both extending the Fragment in the app package or in the support library
Related
I have a TabLayout on a activity and when user clicks on any tab then a fragment is loaded. One of those fragments contains a NavigationView running inside a DrawerLayout. Now the problem is I have to implement MVVM for the whole app and so for the fragments too, I have done this for Activities but I am having trouble while implementing this for Fragments and how to deal with ItemSelected with MVVM and what to bind and how? I am unable to find a proper working sample of the same. Here's the code:
MoreFragment.java:
package com.abc.Fragments;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.abc.MainActivity;
import com.abc.MoreFragmentViewModel;
import com.abc.R;
public class MoreFragment extends Fragment {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(MoreFragmentViewModel.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.fragment_more, container, false);
return fragView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDrawerLayout = getView().findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(Gravity.END);
}
}
fragment_more.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/grey"
app:navigationItemSelectedListener="#{()-> viewModel.onMyItemSelected()}"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
MoreFragmentViewModel.java:
package com.abc;
import android.arch.lifecycle.ViewModel;
import android.databinding.BaseObservable;
public class MoreFragmentViewModel extends ViewModel {
public void onMyItemSelected(){
System.out.print("Item Clicked");
}
}
Please do any possible help.
Use:
DataBindingUtil.inflate(inflater, layoutId(), container, false)
To inflate your layout.
And don't forget to add:
<data>
<variable
name="model"
type="MoreFragmentViewModel"/>
</data>
To your fragment layout.
This question already has answers here:
incompatible types: HomeFragment cannot be converted to Fragment in Android
(10 answers)
Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager
(1 answer)
Closed 5 years ago.
Main Activity
package com.example.richu.fragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeFragment(View view){
Fragment fragment;
if(view == findViewById(R.id.button3)){
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
if(view == findViewById(R.id.button4)){
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
}
}
MainActivity.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.richu.fragment.MainActivity">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="2dp"
tools:layout_editor_absoluteY="5dp" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="292dp"
tools:layout_editor_absoluteY="5dp" />
<fragment
android:id="#+id/fragment1"
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="13dp"
tools:layout_editor_absoluteY="13dp" />
</LinearLayout>
FragmentOne.java
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.richu.fragment.R;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}
FragmentOne.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"
android:background="#color/colorAccent"
tools:context="layout.FragmentOne">
</FrameLayout>
FragmentTwo.java
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.richu.fragment.R;
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}
}
FragmentTwo.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"
android:background="#color/colorAccent"
tools:context="layout.FragmentTwo">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Hello" />
</FrameLayout>
I was trying to learn Fragment basics. Just added Two buttons in an activity. When one button is clicked, FragmentOne should load and when the second button is clicked, FragmentTwo should load.
The following codes in MainActivity.java was showing errors and the error is Incompatible types. FragmentOne and FragmentTwo is the two Fragments that i created.
- fragment = new FragmentOne();
- fragment = new FragmentTwo();
Please find a solution for this. Thank You.
In Main Activity, you use import android.app.Fragment;
in FragmentTwo and FragmentOne You use import android.support.v4.app.Fragment;
You must decide with version is better for you.
Link show the difference:
Difference between android.app.Fragment and android.support.v4.app.Fragment
The problem is that there is a mismatch in fragments types between objFragment and fragmentManager. The first is from package android.support.v4.app while the second is from the android.app package. To solve the problem change the getFragmentManager() to getSupportFragmentManager(). Just replace the code lines
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
to
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
I have already looked at the other answers but none of them help me.
I made sure to use import android.support.v4.app.Fragment; so that's not the problem.
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
Error :
Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
Why am I getting this?
In activity_main.xml I have a FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:id="#+id/fragmentContainer">
</FrameLayout>
myFragment.java
package com.example.nirvan.fragmentsexample2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
return inflater.inflate(R.layout.fragment,container,false);
}
public myFragment(){}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="145dp"
android:text="fragment One"/>
</RelativeLayout>
Sloppy mistake . You should call v4.app.Fragment instead of app.Fragment .
Open myFragment.java
Don't
import android.app.Fragment;
Do
import android.support.v4.app.Fragment;
Change this
import android.app.Fragment;
to
import android.support.v4.app.Fragment;
in myFragment class
Also i would suggest to have proper naming convention for your fragment.
No need for android:orientation="vertical" in relative layout.
I'm noob on android development, and im trying to do a simple app, for details about certain products and stuff.
i have the the following layout:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
There are 2 tabs on that tabHost navigation, each one extends fragment like this
package co.com.smartmedia.vademecumtg;
import co.com.smartmedia.vademecumtg.R;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LineasTab extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false);
}
}
In the first tab of that navigation i have a listView, and each item of that list, must call another activity that will show details about it.
im calling a new activity by calling this method from an itemClickListener:
public void intentForMedView(int pos){
Log.w("debbuging", "start");
Intent intent = new Intent(getActivity().getApplicationContext(), MedViewActivity.class);
intent.putExtra(EXTRA_MEDTITLE, array_sort.get(pos).getNombre());
intent.putExtra(EXTRA_MEDCONTENT, array_sort.get(pos).getLinea());
Log.w("debbuging", "send intent"+intent);
startActivity(intent);
}
and the activity that i'm calling is the following:
package co.com.smartmedia.vademecumtg;
import co.com.smartmedia.vademecumtg.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MedViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_med_view);
Intent intent = getIntent();
String title = intent.getStringExtra(MedicamentosTab.EXTRA_MEDTITLE);
setTitle(title);
TextView text = (TextView) findViewById(R.id.text);
String lineName = intent.getStringExtra(MedicamentosTab.EXTRA_MEDCONTENT);
text.setText(lineName);
}
}
My problem is:
When the detail activity is shown, my navigation Disappears (the tabHost navigation), and i don't want it....
some tips or ways to do it ?
thaks...
I got the answer now, The way to accomplish this architecture, is to use FragmentTransaction
http://developer.android.com/guide/components/fragments.html
Cause it isn't necessary another activity... just play with the fragments of the UI.
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
And take care with the android version cause android.support.v4.app.FragmentManager cannot cast to the android.app.FragmentManager
Hope this be usefull
Im trying to Use Fragements. But in MainActivity.java file the transaction.add(R.id.my_layout, testfrag, "");not Initiated correctly.please find below the code i used.
package com.example.testtabs;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;
public class MainActivity extends TabActivity {
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragementTest testfrag=new FragementTest();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.my_layout, testfrag, "");//Error is here
}
}
activity_main.xml :
<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: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=".MainActivity"
android:id="#+id/my_layout"
>
Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)
Fragement.test
package com.example.testtabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link android.support.v4.app.Fragment} subclass.
*
*/
public class FragementTest extends Fragment {
public FragementTest() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragement, container, false);
}
}
change:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
to:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
or---
change:
import android.support.v4.app.Fragment;
to
import android.app.Fragment;
All the Fragment should be android.app.Fragment. If you want to use android.support.v4.app.Fragment, you should use getSupportFragmentManager() to get the Manager.
Its working for me:
Instead of using getFragmentManager use getSupportFragmentManager();