I have faced such problem. I'm working on an application using ViewPagerIndicator library by Jake Wharton. I would like to change fragment on button click (fragments do change when user swipes through pages though), but my code doesn't work. Any ideas for solving that? In my code below I tried changing FirstFragment to SecondFragment on rightButton click. I would be grateful for any of your help...
FirstFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout1, container, false);
rightButton = (Button) view.findViewById(R.id.rightButton);
rightButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
android.support.v4.app.Fragment fragment = new SecondFragment();
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment1, fragment).commit();
}
});
return view;
}
I change fragments when swiping using this code(I don't know, if it's related to the problem, but I'll post it anyway) TestFragmentAdapter.java file
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstFragment.newInstance(0);
case 1:
return SecondFragment.newInstance(1);
case 2:
return ThirdFragment.newInstance(2);
default:
return null;
}
}
That's my layout file to FirstFragment.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/d_blue"
android:id="#+id/fragment1"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/d_blue"
android:orientation="vertical"
android:padding="10dip">
<Button
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:shadowColor="#color/text_shadow"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="SKIP"
android:textColor="#color/light_font"
android:textSize="14dp"
android:textStyle="bold" />
<Button
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:shadowColor="#color/text_shadow"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="NEXT"
android:textColor="#color/light_font"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
How about using setCurrentItem() of the ViewPager instead of replacing the Fragments by yourself.
Related
I have a nav fragment section called GroupbookFragment which uses a TabLayout with 3 Tabs.
(GroupbookKrippeFragment, GroupbookHafenFragment & GroupbookKindergartenFragment)
Each Tab has a CardView with a RecyclerView in it. The goal is to have a global "Add Child" Button on the GroupbookFragment level, which forward you to another activity with a form where you can create a new child. After filling data & clicking a save button, you'll come back to the GroupbookFragmentview and the new created data from the form will show up in the correct CardView within one of the three corresponding fragments.
So far I successfully implemented all functionalities when the methods and "Add child" button are already IN the corresponding tab child fragment (e.g. GroupbookKrippeFragment). The problem here is, whenever I change the tab from the tabLayout, the "Add Child" button is moving away aswell. So after moving all methods & the button into the parent fragment GroupbookFragment I can navigate through all pages and fill the form & save, but the recyclerView won't update and show the new created data in the child's recyclerView anymore?!
Thanks in advance for any help! Here is the code view:
GroupbookFragment.java
public class GroupbookFragment extends Fragment {
// initializing elements
RecyclerView recyclerView;
ChildListAdapter childListAdapter;
List<Child> children = new ArrayList<>();
RoomDb database;
FloatingActionButton addChildBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_groupbook, container, false);
// inflate the layout for GB Krippe fragment
View viewKrippe = inflater.inflate(R.layout.fragment_groupbook_krippe, container, false);
// link recycler view to correct element
recyclerView = viewKrippe.findViewById(R.id.recycler_view_krippe);
// Tab layout initialization
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
GroupbookAdapter adapterGroupBook = new GroupbookAdapter(getChildFragmentManager(), getLifecycle());
viewPager.setAdapter(adapterGroupBook);
// set 3 Tab titles
tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add Child code
database = RoomDb.getInstance(getContext());
children = database.mainDAO().getAll();
updateRecycler(children);
// link var to app element & set on click route
addChildBtn = view.findViewById(R.id.child_add_btn);
addChildBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupbookFragment.this.getContext(), AddChildActivity.class);
startActivityForResult(intent, 101);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Child new_child = (Child) data.getSerializableExtra("child");
database.mainDAO().insert(new_child);
children.clear();
children.addAll(database.mainDAO().getAll());
childListAdapter.notifyDataSetChanged();
}
}
}
void updateRecycler(List<Child> children) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(GroupbookFragment.this.getContext()));
childListAdapter = new ChildListAdapter(GroupbookFragment.this.getContext(), children, childClickListener);
recyclerView.setAdapter(childListAdapter);
}
private final ChildClickListener childClickListener = new ChildClickListener() {
#Override
public void onClick(Child child) {
}
#Override
public void onLongClick(Child child, CardView cardView) {
}
};
}
GroupbookFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Fragments.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorAnimationMode="elastic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tabLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager_groupBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- Add floating button to add child-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/child_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="#dimen/margin_cardView"
android:backgroundTint="#color/green"
android:src="#drawable/ic_add_child"
app:tint="#color/white" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
GroupbookKrippeFragment.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Groupbook.GroupbookKrippeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/groupBook_krippe_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/margin_cardView"
app:cardCornerRadius="#dimen/cornerRadius_Card"
app:cardElevation="#dimen/elevation_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/groupBook_krippe_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/creme_500"
android:layout_weight="6"
android:padding="#dimen/padding_default">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline_group_1_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<ImageView
android:id="#+id/ic_groupBook_krippe_1"
android:layout_width="#dimen/iconSize_groupBook_header"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_groupbook_krippe_1_logo"
app:tint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline_group_1_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/title_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gruppe 1"
android:textAllCaps="true"
android:textSize="#dimen/textSize_groupBook_title"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/desc_groupBook_krippe_1"/>
<TextView
android:id="#+id/desc_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gesamt: 12 | Anwesend: 10"
android:textSize="#dimen/textSize_groupBook_subTitle"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/title_groupBook_krippe_1"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="#dimen/padding_default"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="ID"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Vorname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nachname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtstag"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtsort"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Konfession"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nationalität"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
</TableRow>
</TableLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_krippe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
I am trying to add a fragment from an activity using the add(R.id.containerId, fragment);. However the log gives the java.lang.IllegalArgumentException: No view found for id error.
The fragment layout contains an id in the root element. I know that placing the container id in a child element is supposed to fix things, but this has made no difference for me.
Here is the root element in the fragment layout xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical"
tools:context=".view.fragment.Fragment">
Here is the onCreate() method in my activity:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment);
currentFragment = R.id.fragment_container;
transaction.commit();
Here is the onCreateView() method in the fragment:
return inflater.inflate(R.layout.fragment, container, false);
Here is the entire activity code (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
public class MainActivity extends AppCompatActivity {
private int currentFragmentId;
private FeedFragment feedFragment = new FeedFragment();
private EventsFragment eventsFragment = new EventsFragment();
private SearchFragment searchFragment = new SearchFragment();
private MoreFragment moreFragment = new MoreFragment();
// Add other fragments
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_feed_container, feedFragment);
currentFragmentId = R.id.fragment_feed_container;
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_feed:
// Add the feed fragment
replaceFragment(feedFragment, currentFragmentId);
currentFragmentId = R.id.fragment_feed_container;
return true;
case R.id.action_events:
// Add the events fragment
replaceFragment(eventsFragment, currentFragmentId);
currentFragmentId = R.id.fragment_events_container;
return true;
case R.id.action_search:
// Add the search fragment
replaceFragment(searchFragment, currentFragmentId);
currentFragmentId = R.id.fragment_search_container;
return true;
case R.id.action_more:
// Add the more fragment
replaceFragment(moreFragment, currentFragmentId);
currentFragmentId = R.id.fragment_more_container;
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
/**
* Replaces the current fragment with a new fragment
*
* #param newFragment
* #param currentFragmentId
*/
private void replaceFragment(Fragment newFragment, int currentFragmentId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(currentFragmentId, newFragment);
transaction.commit();
}
}
Here is the full fragment class (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
public class FeedFragment extends Fragment {
public View mView;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_feed, container, false);
return mView;
}
}
Here is the code for the full fragment_feed.xml file (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_feed_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical"
tools:context=".view.fragment.FeedFragment">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/top_card_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/card_profile_image_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
android:id="#+id/card_info_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="start"
tools:text="Julien Durrand invited you" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
tools:text="3 months" />
</LinearLayout>
<ImageView
android:id="#+id/card_main_image_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/top_card_linear_layout"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_main_image_view"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="start"
tools:text="30K" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Here is the main parts of the logcat stacktrace (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomfinet.magpie/com.tomfinet.magpie.view.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}
Stuff that I have checked:
Fragment layout is correct in the onCreateView() method in the fragment.
Fragment layout container id in child of the fragment xml file.
How do I fix this error?
Thank you.
I am using viewPager and fragments to develop an application . In one screen I use DialogFragmentto display an alert message .
My DialogFragment code is below,
public class ErrorFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.error_fragment, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setLayout(350,300);
Button button=(Button)rootView.findViewById(R.id.yesButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Print -"," Ok");
if(getActivity()!=null){
Log.d("Print -"," Ok2");
((LoginActivity)getActivity()).setCurrentItem(4);
Log.d("Print -", " Ok4");
}
Log.d("Print -"," Ok3");
dismiss();
}
});
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
In my button action , I have program to open another fragment however nothing is happening except DialogFragment getting closed.
Below is my layout,
<?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:background="#drawable/button_border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/errorContent"
android:id="#+id/errorTextView1"
android:textSize="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/errorTextView1"
android:id="#+id/space4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/yesButton"
android:text=" Yes "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/noButton"
android:text=" No "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:layout_alignTop="#+id/yesButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/noButton"/>
</RelativeLayout>
How can I open other fragment by clicking particular button in DialogFragment ?
Even though fragment shifting is not happening the console print inside the button action is getting printed.
I'm pretty new to Android Studio and I'm having difficulty starting a new activity. I've triple checked my code and I can't figure out what my problem is. I've also google searched for a couple of hours but nobody seems to have had the same problem as me. Logcat isn't reporting a problem, but when I run the app, and click the button, the app goes to a blank screen. Please help!
public class OriginalFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_original, container, false);
return rootView;
}
}
public class MainFragment extends Fragment {
private AlertDialog mDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Sets up the about button
View aboutButton = rootView.findViewById(R.id.about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.about_title);
builder.setMessage(R.string.about_text);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
mDialog=builder.show();
}
});
View originalButton = rootView.findViewById(R.id.original_button);
originalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), OriginalActivity.class);
getActivity().startActivity(intent);
}
});
View pictureButton = rootView.findViewById(R.id.picture_button);
originalButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(getActivity(), PictureActivity.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
#Override
public void onPause(){
super.onPause();
if (mDialog != null)
mDialog.dismiss();
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<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:clipChildren= "false"
tools:context= "org.example.abstract_art.MainActivity">
<fragment android:id= "#+id/main_fragment"
class= "org.example.abstract_art.MainFragment"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
tools:layout= "#layout/fragment_main" />
</FrameLayout>
<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:background="#drawable/select_shapes_background"
tools:context="org.example.abstract_art.OriginalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Shapes"
android:id="#+id/textView"
android:textSize="#dimen/menu_text_size"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="org.example.abstract_art.OriginalFragment"
android:id="#+id/fragment"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
tools:layout="#layout/fragment_original" />
</RelativeLayout>
<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:background="#drawable/menu_background"
android:elevation="#dimen/elevation_high"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context=".org.example.abstract_art.MainFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/menu_space"
android:text="#string/long_app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="#dimen/menu_text_size"/>
<Button
android:id="#+id/original_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/original_label"/>
<Button
android:id="#+id/picture_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/picture_label"/>
<Button
android:id="#+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/about_label"/>
</LinearLayout>
<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:background="#drawable/menu_background"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context="org.example.abstract_art.OriginalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circles"
android:id="#+id/circlesText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rectangles"
android:id="#+id/rectanglesText"
android:layout_below="#+id/circlesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squares"
android:id="#+id/squaresText"
android:layout_below="#+id/rectanglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triangles"
android:id="#+id/trianglesText"
android:layout_below="#+id/squaresText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4 Point Stars"
android:id="#+id/fourStarText"
android:layout_below="#+id/trianglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 Point Stars"
android:id="#+id/sixStarText"
android:layout_below="#+id/fourStarText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Art!"
android:id="#+id/createOriginalButton"
android:layout_below="#+id/sixStarText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please Make sure Below Things
You have Define Main Activity, OriginalActivity,PictureActivity in your Manifest file
Please Make sure You also set Layout in all Activity or Fragment and Bind Components Of All Layout in that class
try below code
Button originalBtn; //Define class Level
originalBtn=(Button)rootView.findViewById(R.id.original_button);
Now you can set click Listener and code in it
Second main things that you define two times originalButton Click Listener so i think thats why you get problem
I think second Listener should be pictureButton Click Listener
I think that it's a bit difficult to explain the problem but I'll try. I have a class which extends Fragment and I call it with the follow code
frag = new SearchDoctor();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
Here is a part of class code
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout.search_doctor, container, false);
spinnerCity = (Spinner) rootView.findViewById(R.id.cityList);
The first time that class is called it's alright.
But the next time when I call it thead exit at the line "rootView = inflater.inflate(R.layout.search_doctor, container, false);".
I have understand that the problem is with the fragment that contained in search_doctor.xml (layout) because I had tried it with other layouts many times and it was any problem.
Here is the layout search_doctor.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" >
<Spinner
android:id="#+id/cityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/city_arrays"
android:prompt="#string/city_prompt" />
<Button
android:id="#+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/specialtyList"
android:layout_marginRight="16dp"
android:text="Αναζήτηση" />
<Spinner
android:id="#+id/specialtyList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cityList"
android:entries="#array/specialty_arrays"
android:prompt="#string/specialty_prompt" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ListView
android:id="#+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp" >
</ListView>
<Button
android:id="#+id/btnMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/resultList"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:text="Χάρτης" />
</RelativeLayout>
Please if someone could help me I'll be very pleasure.
Thank's a lot in advance!
This is the solution! I find on the forum so thank you all.
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
Can you try to replace
rootView = inflater.inflate(R.layout.search_doctor, container, false);
with
rootView = inflater.inflate(R.layout.search_doctor, null, false);
Not sure if it helps but if the problem with view hierarchy that might be useful.