Fragment is not fully displayed in Activity - java

In my app I have an Activity containing a simple container (LinearLayout) in which I want to display a Fragment. It all works and the fragment is shown but it is kind of "cut off" at the end of the screen. Meaning if in the fragment the screen is completely filled out with buttons, in the actual running app the last button is not or only partly shown.
To make it easier to test I created this new project where in the fragment there are only 3 big buttons which, in the design function in android studio, do not extend the screen but then when I run it are cut off.
<?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=".BlankFragment">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:textSize="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button9" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="vertical"
tools:context=".MainActivity"/>
package com.example.fragmenttestapp;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
package com.example.fragmenttestapp;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
}
}
Would appreciate some help! :)

I am 99% sure that your views are not fitting into your screen because of your huge text size on your views (100dp), large text size will resize your view, and you are using a fixed size for you texts size.
Remember that fixed size on views makes your layout non-responsive
After I removed your text size from the views it was alright, here is an example:
<?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">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/button9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/button10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button10"
android:layout_width="w"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button9" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And if you want to use large text maybe use Autosizing TextViews

Related

Why doesn't the replace function for fragments work?

I'm trying to open a Fragment from a Activity by using the fragmentManager.replace() function, but it doesn't do anything and the Logcat tells me nothing either. I have tried using both FragmentContainerView and FrameLayout inside my activity_main.xml, and even using the activity's ConstraintLayout ID to host the Fragment, but to no avail.
Here are the codes for my Activity that should open the fragment, and the fragment code itself:
OsActivity.java
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import com.leonardomaito.autocommobile.fragments.NewOsFragment;
import autocommobile.R;
public class OsActivity extends AppCompatActivity {
NewOsFragment newOsFragment = new NewOsFragment();
private View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_os);
}
public void createNewOs(View view) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.replace(R.id.osFragmentContainer, newOsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
NewOsFragment.java
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import autocommobile.R;
public class NewOsFragment extends Fragment {
private Button btOsNext;
private Button btOsCancel;
private Fragment nextOs = new Fragment();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_os, container, false);
btOsNext = view.findViewById(R.id.btNextOs);
btOsCancel = view.findViewById(R.id.btCancelNewOs);
return view;
}
public void nextOsStep(View view) {
FragmentTransaction fragmentManager = getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_new_os, nextOs);
}
}
ActivityOs.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/activity_os"
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:layout_marginBottom="0dp"
android:background="#color/white"
tools:context="com.leonardomaito.autocommobile.activities.OsActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/osFragmentContainer"
android:name="com.leonardomaito.autocommobile.fragments.NewOsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="#layout/fragment_new_os">
</androidx.fragment.app.FragmentContainerView>
<include
android:id="#+id/include"
layout="#layout/layout_header_search" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glRecyclerLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="124dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewOs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="650dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.0"
tools:listitem="#layout/layout_os_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/btNewOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Inserir OS"
android:onClick="createNewOs"
android:src="#drawable/custom_plus_icon"
app:backgroundTint="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.98"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.988"
app:rippleColor="#color/autocom_blue" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_new_os.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/fragment_new_os"
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.leonardomaito.autocommobile.fragments.NewOsFragment">
<TextView
android:id="#+id/tvServiceOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Ordem de Serviço"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/viewHorizontalBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<View
android:id="#+id/viewHorizontalBar"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginTop="32dp"
android:background="#color/autocom_blue"
app:layout_constraintBottom_toTopOf="#+id/glViewBottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glViewBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="57dp" />
<TextView
android:id="#+id/tvCliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="Cliente"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.084"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewHorizontalBar" />
<EditText
android:id="#+id/etClientInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/custom_border"
android:drawableStart="#drawable/custom_search_icon"
android:ems="17"
android:importantForAutofill="no"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvCliente" />
<TextView
android:id="#+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Endereço"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.094"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etClientInput" />
<EditText
android:id="#+id/etAddressInput"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#drawable/custom_border"
android:ems="17"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvAddress"
android:importantForAutofill="no" />
<TextView
android:id="#+id/tvTelephone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Telefone"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etAddressInput" />
<EditText
android:id="#+id/etTelephoneInput"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#drawable/custom_border"
android:ems="17"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTelephone"
android:importantForAutofill="no" />
<android.widget.Button
android:id="#+id/btNextOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:background="#drawable/custom_button"
android:minWidth="125dp"
android:onClick="nextOsStep"
android:text="Avançar"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etTelephoneInput"
app:layout_constraintVertical_bias="0.155" />
</androidx.constraintlayout.widget.ConstraintLayout>
Shouldn't the replace function be opening my fragment on top of my activity? Is there something I'm doing wrong? The majority of other posts adressing this situation seemed to use .add instead of .replace, and were fixed by doing so.

How do I get rid of the extra spacing that is appearing at the top of my Android app?

I'm building a homepage for an Android app for one of my college classes. My group is using Android Studio. I've got the layout that I was but I have this random extra space at the top above where it says Welcome.
I cannot figure out how to get rid of that random spacing at the top and it's pushing the rest of my objects down (reports list disappears below nav bar)
Here's my fragment_home.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:text="#string/home_welcome"
android:textAlignment="center"
android:textSize="20pt"
app:layout_constraintBottom_toTopOf="#+id/text_appts"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/text_appts"
android:layout_width="365dp"
android:layout_height="55dp"
android:ems="10"
android:text="#string/title_appointments"
android:textSize="14pt"
app:layout_constraintBottom_toTopOf="#+id/recycler_appts"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_appts"
android:layout_width="360dp"
android:layout_height="125dp"
app:layout_constraintBottom_toTopOf="#+id/text_chats"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.484"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_appts"
app:layout_constraintVertical_bias="0.226" />
<EditText
android:id="#+id/text_chats"
android:layout_width="365dp"
android:layout_height="55dp"
android:ems="10"
android:text="#string/title_chats"
android:textSize="14pt"
app:layout_constraintBottom_toTopOf="#+id/recycler_chats"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recycler_appts" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_chats"
android:layout_width="360dp"
android:layout_height="125dp"
app:layout_constraintBottom_toTopOf="#+id/text_reports"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.484"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_chats"
app:layout_constraintVertical_bias="0.226" />
<EditText
android:id="#+id/text_reports"
android:layout_width="365dp"
android:layout_height="55dp"
android:ems="10"
android:text="#string/title_reports"
android:textSize="14pt"
app:layout_constraintBottom_toTopOf="#+id/recycler_reports"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recycler_chats" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_reports"
android:layout_width="360dp"
android:layout_height="125dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.424"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_reports"
app:layout_constraintVertical_bias="0.226" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here's my HomeFragment.java
package com.example.telemedicine.ui.home;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.telemedicine.R;
import com.example.telemedicine.ui.utilities.RecyclerItem;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private RecyclerView recyclerView_appt, recyclerView_chat, recyclerView_report;
private RecyclerView.Adapter mAdapter_appt, mAdapter_chat, mAdapter_report;
private RecyclerView.LayoutManager layoutManager_appt, layoutManager_chat, layoutManager_report;
private String[] apptData = {"Physical - 9/29 # 10:00am", "Vaccination - 10/4 # 1:30pm", "Check-Up - 10/19 # 9:00am"};
private String[] chatData = {"Dr. Jane Smith", "Dr. Hayden Lee", "Dr. Michael Dean"};
private String[] reportData = {"Blood Work 9/10", "Vaccination Summary 9/1", "Physical 8/23"};
//Activity activity = (Activity)getContext();
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.editText);
homeViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
recyclerView_appt = (RecyclerView)root.findViewById(R.id.recycler_appts);
layoutManager_appt = new LinearLayoutManager(this.getActivity());
recyclerView_appt.setLayoutManager(layoutManager_appt);
mAdapter_appt = new RecyclerItem(apptData);
recyclerView_appt.setAdapter(mAdapter_appt);
recyclerView_chat = (RecyclerView)root.findViewById(R.id.recycler_chats);
layoutManager_chat = new LinearLayoutManager(this.getActivity());
recyclerView_chat.setLayoutManager(layoutManager_chat);
mAdapter_chat = new RecyclerItem(chatData);
recyclerView_chat.setAdapter(mAdapter_chat);
recyclerView_report = (RecyclerView)root.findViewById(R.id.recycler_reports);
layoutManager_report = new LinearLayoutManager(this.getActivity());
recyclerView_report.setLayoutManager(layoutManager_report);
mAdapter_report = new RecyclerItem(reportData);
recyclerView_report.setAdapter(mAdapter_report);
return root;
}
}
Here's my HomeViewModel.java
package com.example.telemedicine.ui.home;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
}
public LiveData<String> getText() {
return mText;
}
}
And my activity_main.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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
I don't know what other files to include but hopefully the problem is in those four files. I've tried so many things to get rid of that spacing, but I cannot figure out how to get rid of it.
Thanks in advance!!
In your activity_main.xml file, try removing the android:paddingTop="?attr/actionBarSize" line in the ConstraintLayout since there is no actionBar.
in your activity_main.xml change 'fragment' (nav_host_fragment) layout_width and layout_height to 0dp. that is the correct form of using constraints.
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />

Text view doesn't show after button click - Android

I have a problem with Text View and Button in Android Studio. I would like to update my text view when i clik a button but it doesn't work.
This is my code:
activity_distance_to_run.xml
<?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=".MainActivity"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="#+id/Distancerun2"
android:layout_width="126dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="Distance to ....."
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/distancetorun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="run"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toStartOf="#+id/distancetoride"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/distancetoride"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="32dp"
android:text="ride a bicycle"
app:layout_constraintEnd_toStartOf="#+id/distancetoswim"
app:layout_constraintStart_toEndOf="#+id/distancetorun"
app:layout_constraintTop_toBottomOf="#+id/Distancerun2" />
<Button
android:id="#+id/distancetoswim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="swim"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/distancetoride" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:text="I did it !!"
app:layout_constraintBaseline_toBaselineOf="#+id/no"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="116dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="I don't want to do it !!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="23dp"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="221dp"
android:autoText="true"
android:clickable="true"
android:enabled="false"
android:freezesText="false"
android:text="Elo ziomek"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/no"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
distancetorun.java
package pl.agcode.sqliteexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class distance_to_run extends Activity {
Button distancetorun;
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance_to_run);
distancetorun = (Button) findViewById(R.id.distancetorun);
txtView = (TextView) findViewById(R.id.textView1);
distancetorun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText("NewText");
}
});
}
}
Thanks for help I will try to repair it but don't know how.
This is very important to repair this for me. :)
Looks all good except textview1 color.
Add below line for the textview1 in layout xml
<TextView
android:id="#+id/textView1"
android:textColor="#android:color/black"
After launching the app
After clicking RUN button

Programmatically add textView with constraint

I have a ScrollView defined by the xml. Inside of it I want to have two textView like in this photo :
Doing that in xml is fine. But how to add them programmatically? Here's what the code look like without the two TextView example:
.xml file :
<?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:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
tools:context=".InventoryActivity">
<ImageView
android:id="#+id/imageView9"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/inventorylayoutbg" />
<ImageView
android:id="#+id/imageView33"
android:layout_width="61dp"
android:layout_height="59dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="closeDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.254"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.927"
app:srcCompat="#drawable/blank" />
<ScrollView
android:id="#+id/sv"
android:layout_width="326dp"
android:layout_height="374dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="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_toTopOf="#+id/imageView9">
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
.java file
package com.example.jsciampi.tapper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class InventoryActivity extends AppCompatActivity {
//Form
ScrollView sv;
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = (int)(dm.widthPixels*.9);
int height = (int)(dm.heightPixels*.8);
getWindow().setLayout(width,height);
sv = findViewById(R.id.sv);
ll = findViewById(R.id.ll);
for(int i = 0; i < 5; i++){
Button b = new Button(InventoryActivity.this);
//Add Constraint there
ll.addView(b);
}
}
public void closeDialog(View view) {
finish();
}
}
The second TextView had this xml config:
<TextView
android:id="#+id/textView2"
android:layout_width="324dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/sv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/firstText"
app:layout_constraintVertical_bias="0.0" />
and I really just need to converto from xml to java this part here :
app:layout_constraintBottom_toBottomOf="#+id/sv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/aads"
app:layout_constraintVertical_bias="0.0"
Can anyone help me out?
for (int i = 0; i < 5; i++) {
View row = LayoutInflater.from(this).inflate(R.layout.image_text_row,null,false);
ll.addView(row);
}
add the below layout to file with name image_text_row.xml
<?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="wrap_content"
android:background="#android:color/background_dark"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="#android:drawable/ic_dialog_email" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>

Android Studio error when adding ImageView in fragment

1- I have a fragment which has 2 EditText and 1 Button (below the code is)
<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.junior_yao.fragmentfrombeginning.Top_Fragment"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="information"
android:inputType="text|textShortMessage"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.503" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="information"
android:inputType="textPersonName"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="#+id/editText1"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/editText1"
android:layout_marginTop="23dp"
app:layout_constraintTop_toBottomOf="#+id/editText1"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Info"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="#+id/editText1"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="#+id/editText1"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/editText2"
app:layout_constraintHorizontal_bias="0.495" />
</android.support.constraint.ConstraintLayout>
2- I have another fragment which has one Image View and 2 TextView
<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.junior_yao.fragmentfrombeginning.Down_Fragment"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/androidtest"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:id="#+id/imageView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
android:textSize="25dp"
android:textColor="#7FFF00"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="30dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
android:textSize="25dp"
android:textColor="#7FFF00"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="44dp" />
</android.support.constraint.ConstraintLayout>
On the 3rd part I add those 2 fragment in main activity layout
3 - if I add the first fragment only (I got no Error )
4 - Here the issue
If I add the 2nd fragment the app does not start , please help me to figure it out
<?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="com.example.junior_yao.fragmentfrombeginning.MainActivity">
<fragment
android:id="#+id/fragment"
android:name="com.example.junior_yao.fragmentfrombeginning.Top_Fragment"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<fragment
android:id="#+id/fragment12"
android:name="com.example.junior_yao.fragmentfrombeginning.Down_Fragment"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
You need to add more information so you can be helped, the java code where the fragments are being called and created and the logcat as well to help solve the problem, the XML design code is not helpful.
here the java code for the 1st fragment
package com.example.junior_yao.fragmentfrombeginning;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Top_Fragment extends Fragment {
public Top_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_top, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
The java code for the 2nd fragment
package com.example.junior_yao.fragmentfrombeginning;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Down_Fragment extends Fragment {
public Down_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_down, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Now the java code of the main Activity
package com.example.junior_yao.fragmentfrombeginning;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Categories