I am trying to make a basic Countdown app.
Below is my main Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.dot.timer.MainActivity">
<Chronometer
android:id="#+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="108dp"
android:textSize="70sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/fragment"
android:name="com.example.dot.timer.SetTimer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/chronometer" />
</android.support.constraint.ConstraintLayout>
I have created two fragments
1)SetTimer.java\fragment_set_timer.xml With a EditText and Button
2)TimerButtons.java\fragment_timer_buttons.xml Three Buttons
SetTimer Activity is below
public class SetTimer extends Fragment{
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_set_timer, container, false);
Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm=getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
Fragment fragment=new TimerButtons();
ft.replace(R.id.fragment,fragment);
ft.addToBackStack(null);
ft.commit();
}
});
return view;
}}
SetTimer xml is below
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dot.timer.SetTimer">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
app:layout_constraintBaseline_toBaselineOf="#+id/button"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText2"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
When i run the project and click on Set Button
this is what happens
and background frame is even Clickable
but shouldn't using .replace() destroy previous fragment
Why is it happening and how can i correct it?
Related
I wanted to refresh my fragment with SwipeToRefresh widget.
This is my XML Code
<?xml version="1.0" encoding="utf-8"?>
<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="#drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
and this is my JAVA code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1, container, false);
newconfig= getResources().getConfiguration();
db=new DBHandler(getActivity());
swipeRefreshLayout=view.findViewById(R.id.swipe);
recyclerView=view.findViewById(R.id.listview);
progressBar=view.findViewById(R.id.progress);
if(book.size()==0) {
book = db.readCourses();
}
progressBar.setVisibility(View.GONE);
BookFragAdapter adapter = new BookFragAdapter(Fragment_land1.this,book);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false));
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
this.onRefresh();
getFragmentManager().beginTransaction().detach(Fragment_land1.this).attach(Fragment_land1.this).commit();
swipeRefreshLayout.setRefreshing(false);
}
});
return view;
}
When I try to refresh it doesn't refresh and the widget doesn't stops after 250ms. I assume that the code inside onRefreshListener is not actually executing therefore the next line is ignored. I don't know how change it with. I surfed in Stackoverflow and Developer.android none of the answers solved my problem. Can anyone help me?
first check that you have added dependencies in build.gradle file
i.e implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
Secondly swipe to refresh layout tag should start from start instead of fragment
for eg :
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
third then write swipeRefreshLayout.setOnRefreshListener code in java file
The Dialog shows properly but the listview is always empty.
Here is what I get when I launch the Activity
enter image description here
public class MainFragment extends Fragment {
String[] cities = {"Tanger", "Meknes", "Casablanca",
"Fes", "Tetouan", "Taza", "Rabat"};
Dialog dialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView currentCity = view.findViewById(R.id.city);
currentCity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner, container, false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
EditText searchTextView = spinnerView.findViewById(R.id.editText);
ListView citiesListView = spinnerView.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
}
DialogLayout:
<?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"
android:background="#drawable/alert_dialog"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
android:textColor="#color/primaryColor"
android:fontFamily="#font/alhurra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:fontFamily="#font/aj_bold"
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentLayout
<?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"
android:background="#drawable/activitybackground"
tools:context=".MainFragment">
<TextView
android:id="#+id/city"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:fontFamily="#font/alhurra"
android:gravity="center"
android:text="مكنـاس"
android:textColor="#color/primaryColor"
android:textSize="20sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is a problem with your layout. ConstraintLayout is somehow hiding the listview. You have to do some changes in your layout files. For example, this has worked for me using LinearLayout
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
//View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner,binding.getRoot());
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ListView citiesListView = dialog.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
dialog.show();
==
<?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"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_below="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_below="#id/editText"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</RelativeLayout>
My recyclerView is not showing anything. I looked through the other similar questions posted on the site, but I can't find anything that seems relevant. When I open the activity, I just see the bar on top with the app name and nothing else below it.
My goal is to get a list of items, where each item has a "delete" icon that can be clicked to delete it. For this purpose, I have a custom layout for the recyclerView item.
I made a simplified version of the code to try and isolate the problem. Here it is:
Activity:
public class CategoryManagerActivity extends AppCompatActivity {
private RecyclerView categoryRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_manager);
categoryRecyclerView = findViewById(R.id.category_edit_recycler);
ArrayList<String> categoryList = new ArrayList<>();
categoryList.add("CAT1");
categoryList.add("CAT2");
categoryList.add("CAT3");
CategoryEditRecyclerViewAdapter adapter = new CategoryEditRecyclerViewAdapter(categoryList);
categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
categoryRecyclerView.setAdapter(adapter);
}
}
Adapter class:
public class CategoryEditRecyclerViewAdapter extends
RecyclerView.Adapter<CategoryEditRecyclerViewAdapter.CategoryEditViewHolder>{
private ArrayList<String> categories;
public CategoryEditRecyclerViewAdapter(ArrayList<String> items){
categories = items;
}
#NonNull
#Override
public CategoryEditViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.categories_edit_recycler_item,
parent, false);
return new CategoryEditRecyclerViewAdapter.CategoryEditViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryEditViewHolder holder, int position) {
final String category = categories.get(position);
holder.categoryName.setText(category);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
delete_category(category);
}
});
}
#Override
public int getItemCount() {
return categories.size();
}
private void delete_category(String category){
//I have code here to delete an item
}
public class CategoryEditViewHolder extends RecyclerView.ViewHolder{
TextView categoryName;
ImageView deleteButton;
public CategoryEditViewHolder(View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryEditText);
deleteButton = itemView.findViewById(R.id.deleteCategory);
}
}
}
recyclerView item layout:
(I used the #drawable/delete in other activities in the app, so I don't think it should be a problem)
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:layout_marginBottom="690dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="679dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Activity layout:
<?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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this for item layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/deleteCategory"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this for activity layout
<?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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Modify your RecyclerView Item Layout s below.
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_drawable_delete" />
</RelativeLayout>
Please modify your categories_edit_recycler_item Item Layouts below:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
I want to create an activity that can switch between two fragments with different background. I can switch betwee both fragment with two buttons.
I have my Activity setup like this when I start my activity :
cardEditor
My fragment is in green, the four button are not in the fragment. By default, my fragment set is addCardEditor. When I click the button "ajouter", the app is supposed to put the addCardEditor fragment. And when I click the button "Voir", I am supposed to see my second fragment wich is called viewCardEditor.
BUT when I start my activity, then when I push the "Voir" button, I have this
The background is still green without any button, but I have set the background to gray for my viewCardEditor fragment !
And when I push the "ajouter" button to have my addCardEditor displayed, it doesn't change anything ! I still have the exact same thing. If I push any button, it alwayws stay in this state ! Both of my fragment are overlap by this green background.
Then when I try to rotate the smartphone, I have an error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jean.cartememoire/com.example.jean.cartememoire.CardEditor}: android.view.InflateException: Binary XML file line #65: Binary XML file line #65: Error inflating class fragment
I made research on stackoverflow and other website but I didn't found any solution to my problem. Thank you for helping me
My cardEditor activity and his xml :
package com.example.jean.cartememoire;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import layout.AddCardEditor;
import layout.ViewCardEditor;
public class CardEditor extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_editor);
}
public void switchFragment(View v)
{
Fragment fg;
FragmentManager fm;
FragmentTransaction ft;
if(v == findViewById(R.id.buttonAddCard))
{
fg = new AddCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
if(v == findViewById(R.id.buttonViewCard))
{
fg = new ViewCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
}
}
Xml for cardEditor :
<?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/activity_card_editor"
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="com.example.jean.cartememoire.CardEditor"
android:orientation="vertical"
android:weightSum="1"
android:background="#android:color/black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="#string/modifier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonModCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="#string/supprimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonDeleteCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="#string/voir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonViewCard"
android:onClick="switchFragment"
tools:ignore="ButtonStyle" />
<Button
android:text="#string/ajouter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonAddCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
</LinearLayout>
<fragment
android:id="#+id/fragment_card_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:name="layout.AddCardEditor"
tools:layout="#layout/fragment_view_card_editor" />
</LinearLayout>
Then there is my first Fragment :
package layout;
public class AddCardEditor extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_add_card_editor, container, false);
}
}
with his xml :
<android.support.constraint.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="layout.AddCardEditor"
android:background="#android:color/holo_green_light">
<TextView
android:text="#string/ajouter_une_carte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:elevation="0dp"
tools:ignore="UnusedAttribute"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="129dp"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="129dp" />
<TextView
android:text="#string/difficult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView11"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="80dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:text="#string/th_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView8"
tools:ignore="UnknownId"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/textView9"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="#+id/textView9" />
<TextView
android:text="#string/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView9"
tools:ignore="MissingConstraints"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="104dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:text="#string/r_ponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView10"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="279dp"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTheme"
tools:ignore="MissingConstraints"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView5"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toRightOf="#+id/textView9"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="126dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editQuestion"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="#+id/textView9"
app:layout_constraintLeft_toRightOf="#+id/textView8"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="136dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editReponse"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/editQuestion"
app:layout_constraintLeft_toLeftOf="#+id/editQuestion" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/editReponse"
app:layout_constraintLeft_toLeftOf="#+id/editReponse" />
</android.support.constraint.ConstraintLayout>
Then the second fragment :
package layout;
public class ViewCardEditor extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_view_card_editor, container, false);
}
}
with his 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"
tools:context="layout.ViewCardEditor"
android:background="#android:color/darker_gray" />
PS : I put Hi in my title because I could not add Hi in the start of my post, so weird... and sorry for bad english
I want to create an activity that can switch between two fragments with different background.
If by "switch" you mean adding/removing fragments, then none of these fragments can be part of your XML layout - in general you can consider XML based structure as (kind of) read-only. So in your project you need to replace your <Fragment> by view group container, i.e. <FrameLayout>, then instantiate your Fragments from code at runtime and add to said container. And since these fragments were not part of the XML, you will be able to remove them (or replace) later w/o any problems.
Basically I have two fragments with two classes for each and one main class and main activity. I want to set one of the fragment as the home screen and when I press a button it shows the other fragment and hides the previous one.
Problems:
My first problem is that the application is not even opening, its saying,"Unfortunately Application stopped working".
Next, when I click on my button both the fragments are merging and the last fragment is not disappearing.
My main class:
public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UpcomingProject Upcoming = new UpcomingProject();
fragmentTransaction.replace(android.R.id.content, Upcoming);
fragmentTransaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
}
My Main Activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/upcomingproject"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/createproject"/>
</LinearLayout>
My Home page fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
>
<Button
android:id="#+id/button_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="#string/button_create"
android:layout_margin="15sp"
android:background="#drawable/drawable_buttoncreate"
android:clickable="true" />
</LinearLayout>
My home page fragment java class:
public class UpcomingProject extends Fragment implements View.OnClickListener {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.upcomingproject, container, false);
Button CreateP = (Button)rootView.findViewById(R.id.button_create);
CreateP.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
CreateProject Create = new CreateProject();
fragmentTransaction.replace(android.R.id.content, Create);
fragmentTransaction.commit();
}
}
My second fragment, the one I want to switch too on the click of a button:
public class CreateProject extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.createproject, container, false);
return rootView;
}
}
And the second fragment's xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView android:id="#+id/name_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_input"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_message"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/contact_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/contact_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="number"
android:hint="#string/contact_description"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/email_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/email_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="textEmailAddress"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/category_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton android:id="#+id/radio_individual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_individual"
/>
<RadioButton android:id="#+id/radio_NPO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NPO"
/>
<RadioButton android:id="#+id/radio_NGO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NGO"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/title_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/title_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="text" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/description_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_description" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:gravity="center"
android:layout_marginBottom="10dp">
<Button android:id="#+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_register"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
<Button android:id="#+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reset"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Try to use getSupportFragmentManager() in your Main Activity to replace first fragment. And getChildFragmentManager() when you replace second fragment
This is a very late reply, but I'll give it in case anyone else hits the same issue.
The reason for this problem was that you have explicitly defined the fragment in your xml file. If you want to swap the fragment it's best to define it as a FrameLayout and then swap it in your onCreate method using e.g.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
fts.add(R.id.content, UpcomingProject.newInstance());
fts.commit();
setContentView(R.layout.activity_main);
}
This is all explained very well and in much more detail in the link in hrskrs's comment above (https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments)