Amir Ehsani
Amir Ehsani

Reputation: 159

Fragment components are invisible in actual running app

I wanna create an activity that consist of 3 fragments. Problem is after adding views to fragment, in xml design section it shows normally but after running in actual device or emulator, views go invisible.I can click on (for example) EditText and keyboard shows itself, but i can not see that EditText!

fragment.xml 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=".ProfileTab">


<EditText
    android:id="@+id/edtProfileName"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="65dp"
    android:layout_marginEnd="20dp"
    android:background="@drawable/et_profiletab_custom"
    android:ems="10"
    android:hint="Profile Name"
    android:inputType="textPersonName"
    android:padding="15dp"
    android:textSize="15sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    .
    .
    .

activity.xml 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"
android:id="@+id/fragment_holder"
tools:context=".SocialMediaActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <include
        android:id="@+id/myToolbar"
        layout="@layout/my_toolbar" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_500"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabSelectedTextColor="#AEA0A0"
        app:tabTextColor="#fff" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/appBarLayout"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
   </androidx.constraintlayout.widget.ConstraintLayout>

fragment.java code:

public class ProfileTab extends Fragment {
private EditText edtProfileName;

public ProfileTab() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_profile_tab, container, false);
    edtProfileName = view.findViewById(R.id.edtProfileName);
//....
return view;

activity.java

public class SocialMediaActivity extends AppCompatActivity {

private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private TabAdapter tabAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_social_media);

    setTitle("Social Media App!!!");
    toolbar = findViewById(R.id.myToolbar);
    setSupportActionBar(toolbar);

    viewPager = findViewById(R.id.viewPager);
    tabAdapter = new TabAdapter(getSupportFragmentManager());
    //get the data from tab that we have created from fragments and put it to viewPager
    viewPager.setAdapter(tabAdapter);

    tabLayout = findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager, false);
}

TabAdapter.java code: // java says that FragmentPagerAapter is deprecetad

public class TabAdapter extends FragmentPagerAdapter {

public TabAdapter(@NonNull FragmentManager fm) {
    super(fm);
}

@NonNull
@Override
public Fragment getItem(int tabPosition) {
    switch (tabPosition){
        case 0:
            return new ProfileTab();
        .
        .
        .
    }

}

@Override
public int getCount() {
    return 3;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    switch (position){
        case 0:
            return "Profile";
        .
        .
        .
    }
}

}

I try fragment.xml root layout with RelativeLayout or disable actionbar replacement with toolbar or change theme of tablayout and toolbar but problem is exist. How can i solve the problem?

Upvotes: 0

Views: 198

Answers (1)

mmdreza baqalpour
mmdreza baqalpour

Reputation: 1413

change these lines in XML activity layout

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />

and also in AppBarLayout set layout height to:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

Upvotes: 1

Related Questions