Using Fragment in Android programming [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was practicing android app development watching the Youtube video.
The following code is exactly same with the result that the instructor coded except the import part.
Even though the code is same, mine has 4 errors on it.(That's why I added some import part;;)
Could you please look at it and teach me how to fix them?
I saw similar questions in here but their solutions were not effective.
I got "activity_input.xml" to show the buttons and fragment.
This following code is on "InputActivity.java".
Three xml and java files for fragment.
which are "Fragment1.java", "Fragment2.java", "StartFragment.java", "fragment1.xml", "fragment2.xml", "start_fragment.xml".
The id of two button is "btn1", "btn2"
The id of layout that will display the fragment is "myFragment"
package com.example.money;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
public class InputActivity extends Activity {
Fragment fragment;
Button btn1, btn2, btn3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
FragmentManager fm = getFragmentManager(); //Here error 1 on getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
StartFragment myFragment = new StartFragment();
ft.add(R.id.myFragment, myFragment); ////Here error 2 on add
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
btn2.setOnClickListener(btnOnClickListener);
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment newFragment;
if (v == btn1)
newFragment = new Fragment1();
else if (v == btn2)
newFragment = new Fragment2();
else
newFragment = new StartFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//Here error 3 on getFragmentManager().beginTransaction()
//Here error 4 on replace
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
};
}

Use this code and organize your imports correctly in eclipse use control+shift+o.and chose correctly.
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
public class InputActivity extends Activity {
Fragment fragment;
Button btn1, btn2, btn3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.to_do_list);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
StartFragment myFragment = new StartFragment();
ft.add(myFragment,R.id.myFragment);
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
btn2.setOnClickListener(btnOnClickListener);
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment newFragment;
if (v == btn1)
newFragment = new Fragment1();
else if (v == btn2)
newFragment = new Fragment2();
else
newFragment = new StartFragment();
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
};
}

Related

how to add a fragment to the navigation drawer menu item fragment

I have a store item fragment in the navigation drawer item menu in this fragment I will like to known how to add another fragment to it if the user click on the Imageview icon then still maintain my navigation drawer below is my try
package com.example.entertainmentlab.ui.store;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.entertainmentlab.R;
import com.example.entertainmentlab.ui.setting.SettingViewModel;
public class StoreFragment extends Fragment {
private StoreViewModel StoreViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
StoreViewModel =
ViewModelProviders.of(this).get(StoreViewModel.class);
View root = inflater.inflate(R.layout.fragment_store, container, false);
final ImageView MusicButton = root.findViewById(R.id.music_btn);
//I want to move to the next Fragment if the user click the music icon
MusicButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Fragment fragment = new BlankFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_store, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}catch (Exception e ){
Toast.makeText(getActivity(), "erro "+e, Toast.LENGTH_SHORT).show();
}
}
});
// StoreViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
// #Override
// public void onChanged(#Nullable String s) {
// textView.setText(s);
// }
// });
return root;
}
}
if the user click the image icon I want to send he/she to the music fragment and then still maintaining the drawer and allow him to return back to the previous fragment
i suggest to you use a AlertDialog for do this work but you can make full screen AlertDialog...

FragmentTransaction add method is accepting the wrong parameter

Hey guys I was learning how to create an Android apps and today I am trying to make Fragment. So the method add has several type of parameter input (cause Android studio shows me 3 different parameter input), like (Fragment, String) (Int, Fragment) and (Int, Fragment, String). What I want to do is to use the first type of parameter, but somehow AndroidStudio keeps on detecting my code as (Int, Fragment).
This is the screenshot
I have looked up in the other stackoverflow forum and I have make sure that I use
import android.support.v4.app.FragmentTransaction;
Here is my code btw:
package listview.bookapp.activities;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import listview.bookapp.R;
public class ProfileMainActivity extends FragmentActivity {
Button profileMain;
Button profileEdit;
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_main);
profileMain = findViewById(R.id.profile_main);
profileEdit = findViewById(R.id.profile_edit);
frameLayout = findViewById(R.id.frameForFragment);
profileMain.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
ProfileMainFragment profileMainFrag = new ProfileMainFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frameForFragment, profileMainFrag);
ft.commit();
}
});
profileEdit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
ProfileEditFragment profileEditFrag = new ProfileEditFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frameForFragment, profileEditFrag);
ft.commit();
}
});
}
}
Wonder if I missed something or this is just a bug from AndroidStudio (I am using Android Studio 3.2.1).
Cheers!

A confusion about Fragment in Android Studio [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is the second day I learn android studio.
I do not know why there is an error here.
help me please!!
//MainActivity.java
public void ChangeFragment(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.add(R.id.FragmentView, fragment2);
//error is there: "Wrong 2nd argument type. Found: 'com.example.myfirstapp.Fragment2', required: 'android.app.Fragment'"
fragmentTransaction.commit();
}
#
//Fragment2.java
import android.support.v4.app.Fragment;
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_fragment2, container, false);
Button button1 = (Button) view.findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView text = (TextView) view.findViewById(R.id.textView1);
text.setText("I am a Fragment");
}
});
return view;
}
#
//activity_fragment2.xml
<LinearLayout
android:id="#+id/FragmentView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp">
</LinearLayout>
https://developer.android.com/guide/components/fragments.html#Transactions
If you are using android.support.v4.app.Fragment (which you should), then you need to use getSupportFragmentManager(), not getFragmentManager().
import android.app.FragmentManager;
import android.app.FragmentTransaction;
FragmentManager fragmentManager = **getFragmentManager()**;
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
import android.app.Fragment;
public class Fragment2 extends Fragment {..}
#
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
FragmentManager fragmentManager = **getSupportFragmentManager()**;
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
import android.support.v4.app.Fragment;
public class Fragment2 extends Fragment {..}

Error:(31, 40) error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment

I have a problem in this code and I don't know which one I should use android.support.v4.app.Fragment or android.app.Fragment;
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(){
android.support.v4.app.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();
}
}
Do not mismatch android.support.v4.app.Fragment with android.app.Fragment, use anyone of them in the whole app.
private void initFragment(){
android.support.v4.app.Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
OR
private void initFragment(){
android.app.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();
}
So if you are using support libraries then use getSupportFragmentManager() and it's supported other methods which are related to support library or else for android app fragment usage don't use support library function. It will create issues with 'Type mismatch'.
And this is highly recommended.
Support library imports for fragment trasaction:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
Android library imports for fragment transactions:
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

Android Fragment Type Mismatch

I am new to android and am following the tutorial at Dartmouth. http://www.cs.dartmouth.edu/~campbell/cs65/lecture08/lecture08.html
I am following all the codes and at the MainActivity.java,
// create the fragments
Fragment mFindFragment = new FindFragment();
Fragment mChatFragment = new ChatFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
getApplicationContext()));
I have encountered this error: Type mismatch: cannot convert from FindFragment to Fragment. So I follow the fix error suggestions and change the code to
// create the fragments
FindFragment mFindFragment = new FindFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));
Now, there is a new error: The constructor MyTabsListener(FindFragment, Context) is undefined.
Just in case the imports are critical, here they are:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
For myTabsListener:
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public Context context;
public MyTabsListener(Fragment fragment, Context context) {
this.fragment = fragment;
this.context = context;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
ft.replace(R.id.container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
ft.remove(fragment);
}
}
For my FindFragment class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FindFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.findfragment, container, false);
}
}
I am pretty confused here. I am not sure if this is related to my import, lib setup or other problems. Thanks in advance!
You need to change this
import android.support.v4.app.Fragment;
// you will use this import when you want fragment from support library
// in that case you will extend FragmentActivity which is the base class for support based fragments
to
import android.app.Fragment;
in FindFragment.java.
Similarly do the same in ChatFragment.java also

Categories