I'm currently developing an application on Android Studio, and I've an issue with a scrollview. I create buttons dynamically (with a number of buttons equal to the number of buttons in my Database), but I can't scroll to the bottom of the page and see every button. I don't know if this issue is caused by the ScrollView or by the LinearLayout inside the ScrollView. Here is the XML and the Java code for it :
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:id="#+id/layoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_list"
tools:context=".GroupList">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="134dp"
android:fontFamily="#font/cartoon"
android:text="Artists"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/back"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="68dp"
android:layout_marginStart="11dp"
android:layout_marginEnd="67dp"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/back2" />
<ScrollView
android:id="#+id/listG"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="1dp"
android:fillViewport="true"
android:layout_marginTop="10dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back">
<LinearLayout
android:id="#+id/layoutG"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Java (the loop that creates buttons dynamically) :
while(db.selectArtist(i).size()>0){
List<String> data = db.selectArtist(i);
Log.i("Artiste",String.valueOf(data));
Button bouton = new Button(getApplicationContext());
bouton.setX(160);
bouton.setY(1 * i + 60);
bouton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layoutG.addView(bouton,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
bouton.setText(data.get(0));
bouton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent start = new Intent(getApplicationContext(), start.class);
start.putExtra("NomArtiste", String.valueOf(bouton.getText()));
start.putExtra("IdArtiste", String.valueOf(data.get(1)));
startActivity(start);
finish();
}
});
i=i+1;
listB.add(i + 1);
}
Here is the result
image
Thanks for your help, it's my first post here, and I'm new in coding in Java!
You need to wrap the whole layout with the scroll view as wrapping just the linearlayout (in context of your code) will make the layout scrollable but it wont include its child views fully.
consider using the following layout: (tweak some values to your comfort)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/your-desired-view-name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_marginTop="10dp"
android:background="your-desired-background-here"
android:fillViewport="true"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back"
tools:context="your-desired-context-here">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="134dp"
android:fontFamily="#font/cartoon"
android:text="Artists"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/back"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="68dp"
android:layout_marginStart="11dp"
android:layout_marginEnd="67dp"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/back2" />
<LinearLayout
android:id="#+id/layoutG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Related
I have a bookmark button in the action bar which is not clickable when I open a bottom sheet by clicking a button (the button is in fragment).
Here is the code :
activity_learn.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=".learn.LearnActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
mcq_fragment.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"
android:background="#color/light_cyan">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ic_close"
style="#style/ic_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_close"
tools:targetApi="lollipop" />
<ProgressBar
android:id="#+id/determinateBar"
style="#style/progressBar"
app:layout_constraintEnd_toStartOf="#+id/cardNumber"
app:layout_constraintStart_toEndOf="#+id/ic_close"
app:layout_constraintTop_toTopOf="parent" />
......more code ......
<com.adithya.memoneet.utils.FButton
android:id="#+id/check_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:fontFamily="#font/quando"
android:text="Check"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
app:buttonColor="#color/light_gray"
app:cornerRadius="30dp"
app:layout_constraintBottom_toTopOf="#+id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear_layout"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
correct_wrong_bottom_sheet_dialog.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"
app:layout_behavior="#string/bottom_sheet_behavior"
app:behavior_hideable= "true"
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="#font/fredoka_one"
android:lineSpacingExtra="1sp"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Oops! Correct Answer is: \nHeart" />
<ozaydin.serkan.com.image_zoom_view.ImageViewZoom
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_memoneet"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:visibility="gone"
app:autoPlay="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image"
app:showFullScreenButton="true"
app:showYouTubeButton="false" />
<com.adithya.memoneet.utils.FButton
android:id="#+id/continue_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="30dp"
android:fontFamily="#font/quando"
android:text="Continue"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
app:buttonColor="#color/light_gray"
app:cornerRadius="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/youtube_player_view"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
McqFragment.java
//below function is called when the user clicks the button in MCQ fragment
public static void showCorrectWrongBottomSheetDialog(final FragmentActivity activity, String answer, String explanation, boolean correct, String subjectType, final CallBackListener callBackListener) {
try {
final BottomSheetDialog dialog = new BottomSheetDialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setDimAmount(0.01f);
dialog.setContentView(R.layout.correct_wrong_bottom_sheet_dialog);
dialog.setCancelable(false);
ConstraintLayout bottom_layout = dialog.findViewById(R.id.bottom_layout);
FButton continue_button = dialog.findViewById(R.id.continue_button);
TextView textview = dialog.findViewById(R.id.textview);
String finalExplanation;
AppCompatImageView imageView = dialog.findViewById(R.id.image);
..more code..
}
when the bottom sheet is opened in mcq fragment I am not able to click the bookmark button in action bar
I have a Android Java app with a grouped recycler view. The app is maded with Mvvm pattern. I have headers who also have recyclerviews, and I show or hide it based on a value. The list loads ok, and the hide/show works ok, but I don't get two columns on the child recyclerview.
This is the Header Item:
<layout
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">
<data>
<import type="android.view.View"/>
<variable
name="model"
type="package.ItemCredentialHeader" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:text="#{model.title}"
android:textColor="#color/black"
tools:text="TITLE"/>
<TextView
android:id="#+id/headerclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:text="Hide"
android:textColor="#color/black" />
</FrameLayout>
<!--Child RecyclerView-->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/child_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:itemsAdapter="#{model.credentialsList}"
android:visibility="#{model.folded ? View.GONE : View.VISIBLE}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="#layout/item_credential" />
</LinearLayout>
This is the Item
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="model"
type="package.ItemCredentialProfile" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:background="#drawable/background_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/credentialData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/opensans_bold"
android:text="#{model.data}"
android:textColor="#color/black"
tools:text="DATO" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:text="#{model.typeName}"
android:textColor="#color/black"
android:textSize="13sp"
tools:text="Type" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:layout_marginBottom="10dp"
android:text="#{model.issuer}"
android:textColor="#color/black"
android:textSize="12sp"
tools:text="Issuer" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:layout_marginBottom="20dp"
android:background="#drawable/background_status"
android:statusColor="#{model.status}"
tools:backgroundTint="#color/status_green">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="3dp"
android:statusText="#{model.status}"
android:textColor="#color/white"
android:textSize="12sp"
tools:text="status" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_card_left">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:layout_marginVertical="10dp"
android:text="View detail >"
android:textColor="#color/light_blue_900"
android:textSize="12sp" />
</FrameLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
And this is the RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/actions_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="55dp"
android:layout_marginEnd="20dp"
android:clipChildren="false"
android:clipToPadding="false"
android:itemClick="#{viewModel}"
android:itemsAdapter="#{viewModel.headersList}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchView"
tools:listitem="#layout/item_credential_header"/>
That is how Android studio shows me the list:
But that's what I got on the device...
It doesn't matter how much groups or elements I have I only get one column
So... at the end it was all my fault...
I was setting the layout manager this way
#BindingAdapter({"android:itemsAdapter"})
public static void addItems(RecyclerView recyclerView,
LiveData<List<ListItem>> actions) {
if(actions!=null && actions.getValue()!=null) {
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(actions.getValue());
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(recyclerViewAdapter);
}
}
I solved just adding a new binding adapter for the child recyclerview
#BindingAdapter({"android:itemsGridAdapter"})
public static void addItemsToGrid(RecyclerView recyclerView,
LiveData<List<ListItem>> actions) {
if(actions!=null && actions.getValue()!=null) {
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(actions.getValue());
recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(),2));
recyclerView.setAdapter(recyclerViewAdapter);
}
}
I have a problem to include a ScrollView with a ConstraintLayout. How Can I mix the two? I would like to get a good resizing of the screen, but with the capability by scrolling the screen if the number of the buttons will increase.
I tried this, but without success. I used that ticket
here is the xml file:
<ScrollView
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:fillViewport="true">
<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:id="#+id/homeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/imagealliance"
android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"
android:gravity="center"
android:theme="#style/AppTheme"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:alpha="0.6"
android:scaleType="fitXY"
android:src="#drawable/logoalliance"
app:layout_constraintBottom_toTopOf="#+id/txt_login"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Here is the code for the buttons:
public class HomeFragment extends Fragment {
...
layout.addView(eventBtn);
if (i == 0)
{
test1 = eventBtn.getId();
Log.d("test1", String.valueOf(eventBtn.getId()));
}
if (i == countUser.length() - 1)
{
test2 = eventBtn.getId();
Log.d("test2", String.valueOf(eventBtn.getId()));
}
Here is the code for the layout:
// Create ConstraintSet
ConstraintSet constraintSet = new ConstraintSet();
// Make sure all previous Constraints from ConstraintLayout are not lost
constraintSet.clone(layout2);
// Create Rule that states that the START of btn_contact_us1 will be positioned at the END of btn_contact_us2
constraintSet.connect(imageView1.getId(), ConstraintSet.START, test1, ConstraintSet.END);
constraintSet.applyTo(layout2);
Thanks in advance
Try updating your xml by following code
<?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=".MainActivityStack">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:alpha="0.6"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Above is the path I follow until I reach the BeginnerFragment.
Above is the hierarchy of activity / fragments
Current Situation
In my application I have a Bottom Navigation, and in each fragment of the navigation, I have two tabs.
The Problem
My problem is that when I'm at BeginnerFragment and I press the cell back button, it returns to the StatusFragment.
I expected it to return to EducationHomeFragment, as I added it to BackStack, according to the codes below.
EducationFragment
public class EducationFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_education, container, false);
// Instanciando o Fragmento de página inicial da Educação
EducationHomeFragment educationHomeFragment = new EducationHomeFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, educationHomeFragment).addToBackStack(null).commit();
return root;
}
}
fragment_education
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/fragment_container"
tools:context=".education.EducationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
EducationHomeFragment
public class EducationHomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_education_home, container, false);
CardView beginnerFreelance = root.findViewById(R.id.cardview_beginner_skills);
beginnerFreelance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BeginnerFragment fragment = new BeginnerFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container , fragment)
.addToBackStack(null)
.commit();
}
});
return root;
}
}
fragment_education_home
<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:id="#+id/fragment_education_home_container"
tools:context=".education.EducationHomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEE"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_academic_education"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/academic_education"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_advanced_skills" />
<TextView
android:id="#+id/title_courses_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/general_courses"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_qualifications" />
<androidx.cardview.widget.CardView
android:id="#+id/cardview_doctoral_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_master_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/doctoral_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_master_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_specialization_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/master_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_specialization_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_higher_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_doctoral_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/specialization_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_higher_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_academic_education">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_master_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/higher_level_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_advanced_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_intermediate_skills">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_specialization_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/advanced"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_study_status"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/default_cardview_mgtop"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_study_status"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:tint="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/outline_menu_book_24" />
<TextView
android:id="#+id/text_study_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/not_studying"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image_study_status" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_qualifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/default_cardview_mgtop"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_study_status">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp">
<TextView
android:id="#+id/title_obtained_qualifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/qualifications_obtained"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/obtained_qualification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/no_course_completed"
app:fontFamily="#font/exo_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_obtained_qualifications" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_beginner_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_courses_available">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_basic_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/beginner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_intermediate_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_beginner_skills">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_higher_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/intermediate"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Why isn't FragmentEducationHome being added to the stack? I want to return to FragmentEducationHome when I click on the BeginnerFragment back button.
This is not a direct answer to the question.
But Google suggests to use Jetpack Navigation approach.
Which will be single Activity and multiple fragments.
Google Code Labs Link
This avoids lot of bipolarate code , were we had to handle backstack, keeping fragment count etc...
At first anyone will feel bit difficult to start with, but in a while , you can grasp and move on to new approach which is suggestible.
Nav_graph avoids programmers responsibility to add/ remove fragments
Allows passing data (raw data or data classes with Parcelable) from one fragment to another
Moving from one child nav graph to another
And much more...
I would suggest you do checkout this once starting with a new trial/project
EDIT
Even though one activity is suggested, we may have more than one Activity and its corresponding child fragment graphs if required. This purely depends on the requirement. In my last project we started with single activity concept. But as the app complexity increased, it was decided to move for two activity and its corresponding child frags.
With respect to Clean Code Architecture this is easily adaptable as well...
Good luck!
Please refer to this question:
Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it.
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate()
} else {
finish();
}
Make sure R.id.fragment_container lives in the parent activity xml (not the fragment xml) and in EducationHomeFragment use .add instead of .replace to add BeginnerFragment to the back stack
You already replace current container fragment by BeginnerFragment, so you can't back to previous fragment. Try to use add(id, fragment, tag) and addToBackStack(tag). Hope help you.
The main problem here is that the parent layout is nested scroll view so the height must be wrap content
so the view is like first image
what I want is to make the "don't have an account? register" TextView
to be at the bottom of the screen so I calculate the height of my
phone then the height of layout to subtract to subtract them and
assign the result to the top margin of textview
and here's my code
ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(() -> {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float parentViewHeightInDP = (int) (binding.parentConstraint.getHeight() / displayMetrics.density);
if (getContext() != null) {
float heightInDP = displayMetrics.heightPixels / displayMetrics.density;
float distanceBetweenLayoutBottomAndParenBottom = heightInDP - parentViewHeightInDP;
if (distanceBetweenLayoutBottomAndParenBottom > 32) {
if (topMargin == 0) {
topMargin = 1;
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) binding.loginTextDontHaveAccount.getLayoutParams();
layoutParams.topMargin = Math.round(distanceBetweenLayoutBottomAndParenBottom);
Handler handler = new Handler();
handler.postDelayed(() -> {
binding.loginTextDontHaveAccount.requestLayout(); },
50);
}
}
but that the result I got
so my real question here is why there still some space here, I mean
the TextView should be exactly at the bottom
What I would suggest you to make NestedScrollView as a child instead of parent.
Make RelativeLayout as your parent and use layout_alignParentBottom to set TextView at bottom. Something like this.
<?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">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground"
android:clickable="true"
android:fillViewport="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="650dp"
android:background="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="650dp"
android:layout_below="#+id/img1"
android:background="#color/colorPrimary" />
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try this:
<?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="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/emailtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#80000000" />
<EditText
android:id="#+id/passwordtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#80000000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enter"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="#id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is my app"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/enter" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use constraint layout. Pay attention to layout_constraintBottom_toTopOf button and layout_constraintTop_toBottomOf textview.