0

I have an Activity which I use as a Dialog in my project. I want to add a ViewPager to it. I want my Dialog's height to be wrap_content. But when i use ViewPager, my Dialog's height looks like match_parent. why this happens? Thanks.

DialogActivity.java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.hadi.android.dm.ui.FirstFragment;
import com.hadi.android.dm.ui.ViewPagerAdapter;

public class DialogActivity extends AppCompatActivity {

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);

    ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),new Fragment[]{new FirstFragment(), new FirstFragment()}));

  }

}

FirstFragment.java:

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;

import com.hadi.android.dm.R;

public class FirstFragment extends Fragment {

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment, null);
    return view;
  }

  @Override
  public void onResume() {
    super.onResume();
  }
}

activity_dialog.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.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v4.view.ViewPager>

</LinearLayout>

fragment.xml:

<?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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layoutDirection="rtl"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:gravity="center"
                android:tag="@string/typeface_bold"
                android:text="17 GB"
                android:textColor="@color/colorSecondaryDark"
                android:textSize="15dp" />

        </LinearLayout>

</LinearLayout>

AndroidManfest:

        <activity
            android:name=".DialogActivity"
            android:theme="@style/AppTheme.DialogActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

styles.xml:

 <style name="AppTheme.DialogActivity" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@style/DialogAnimationStyle</item>
    </style>

enter image description here

Hadi
  • 544
  • 1
  • 8
  • 28
  • [Possible duplicate](https://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content) – MidasLefko Mar 21 '19 at 19:06

1 Answers1

0

In your activity_dialog.xml it looks like you are using android:layout_height="match_parent", but that got edited out before i could answer. Anyway i would request you to use the Layout Inspector, something is using match_parent still. If that isn't the case then some content is over-sized in the view-hierarchy. But a quick look in to the layout inspector will show otherwise.

enter image description here

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39
  • I tried Layout Inspector. `@+id/root` is using all the screen height; why? what can i do to solve that? – Hadi Mar 21 '19 at 18:33
  • Strange, is @+id/root set to "match_parent " in your layout inspector? What is forcing that. Must be style or the content in it. What is the size you are aiming for? – Haroun Hajem Mar 21 '19 at 19:08