I'm trying to create layout with two ViewPager inside ScrollView. First one has set height to desired size and second should match to rest of layout. Problem is that second ViewPager is not visible. What am I doing wrong? This is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
</FrameLayout>
I'm using second ViewPager to show Fragments (using FragmentPagerAdapter), this is my FragmentPagerAdapter code:
public class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
default:
return new FirstFragment();
}
}
#Override
public int getCount() {
return 2;
}
}
Code of FirstFragment:
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_step, container, false);
}
}
first_step.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/holo_blue_light" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#android:color/holo_green_light" />
</LinearLayout>
if you are using layout weight. you have to set width or height layout 0dp accordingly to your use
this gives equal space to both pagers
give weight in ratio i.e 2:1 or 2:1 etc
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
you have to define
android:layout_height="200dp"
remove
android:layout_weight="1"
after both viewpager is visible not to set android:layout_height="wrap_content"
Change
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
to
<android.support.v4.view.ViewPager
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" />
and also
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
to
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Set fillViewport=true in ScollView.
I think ScollView is useless, if the second ViewPager have not fixed height over screen.
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
case ViewPager is a puzzle view,you must set the height for it,then it can
appear in you UI Screen,gallery dont have this problem;
why use ScrollView in this layout, i dont understand.
but,if you use linearlayout
so you can do like this,background is just for test,and you should get destiny in your project to match screen, not just 200 *2:
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/content1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/darker_gray"
android:orientation="vertical" />
<android.support.v4.view.ViewPager
android:id="#+id/content2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ff0000"
android:orientation="vertical" />
</LinearLayout>
activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpage01);
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
ViewPager content = (ViewPager) findViewById(R.id.content2);
content.getLayoutParams().height = screenHeight - 200 * 2;
content.requestLayout();
}
Related
I have a viewpager and inside of it a textview.
For filling pages of viewpager I should measure textview line height with a textpaint.
When viewpager adapter is set, my problem is that in 2 difference page line number is equal but page textview height is different and I want textview fit to page,It is happen in android 4.4.2 and Below, how can I solve this problem.
Notice that I can't use maxLine ellipsize or other solution like them because textview lines number is uncertain.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pages"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="1dp"></LinearLayout>
<SeekBar
android:id="#+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0"/>
<SeekBar
android:id="#+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0"/>
<SeekBar
android:id="#+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0"/>
</LinearLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="left" >
</TextView>
PageFragment :
public class PageFragment extends Fragment {
private final String PAGE_TEXT = "PAGE_TEXT";
private final String TEXT_SIZE = "TEXT_SIZE";
private final String LINE_SPACE = "LINE_SPACE";
public PageFragment newInstance(CharSequence pageText, float textSize, float lineSpace) {
PageFragment frag = new PageFragment();
Bundle args = new Bundle();
args.putCharSequence(PAGE_TEXT, pageText);
args.putFloat(TEXT_SIZE, textSize);
args.putFloat(LINE_SPACE, lineSpace);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CharSequence text = getArguments().getCharSequence(PAGE_TEXT);
float textSize = getArguments().getFloat(TEXT_SIZE);
float lineSpace = getArguments().getFloat(LINE_SPACE);
TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, UnitConverter.spToPixels(getContext(), textSize));
pageView.setText(text);
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "Roboto_Medium.ttf");
pageView.setTypeface(plain);
pageView.setLineSpacing(UnitConverter.spToPixels(getActivity(),lineSpace), 1f);
pageView.setMovementMethod(LinkMovementMethod.getInstance());
return pageView;
}
}
Definitely, Its device specific issue.
For better output in all devices you should use ScrollView as a container of your TextView.
In addition, If you want to add some extra space between text lines then you can use textView.setLineSpacing() programmatically or from XML you can use attribute android:lineSpacingMultiplier="1.3" to TextView for extra spacing.
Hope this will help~
you have change in your base layout LinearLayout to RelativeLayout try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:layout_above="#+id/obttom_layout"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/obttom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"></LinearLayout>
<SeekBar
android:id="#+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0" />
<SeekBar
android:id="#+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0" />
<SeekBar
android:id="#+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0" />
</LinearLayout>
</RelativeLayout>
Page Xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pages_txt"
android:ellipsize="end"
android:gravity="left" >
</TextView>
</LinearLayout>
</ScrollView>
I'm trying to use 2 recycler views in 1 linear layout.
A recycler view will generate its own scroll but other data remains intact on the screen, so how do I combine everything into 1 scrolling area?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="100">
<LinearLayout
android:id="#+id/sliderhome"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="70">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="vlabs.chorbazaar.HomeFragment"
android:background="#color/white"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
I'm trying to make my slider and recycler view to scroll as one, but the slider is stuck at a constant place while the recycler view scrolls as usual.
change xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="100">
<LinearLayout
android:id="#+id/sliderhome"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="70">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
viewpager adapter:
private class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList<String> arrayList
public MyPagerAdapter(FragmentManager fm, ArrayList<String> arrayList ) {
super(fm);
this.arrayList = arrayList;
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance(arraylist);
case 1: return SecondFragment.newInstance(arraylist);
}
#Override
public int getCount() {
return 2;
}
}
}
in activty:
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), arrayList));
fragment in xml file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Many examples with ScrollView, but I don't have any idea with RelativeLayout
In RelativeLayout, some views will cover another view.
I can check it in xml, but how can check it with java code?
Use isShown(), willNotDraw(), hasWindowFocus(), getLocalVisibleRect(), those are not working.
Case1: Container layout is really on screen
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--container layout is really on screen-->
<fragment
android:id="#+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Case2: Container layout is not really on screen
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
<!--container layout is not really on screen-->
<fragment
android:id="#+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
try using this
if (myView.isShown()) {
// Its visible
} else {
// Either gone or invisible
}
Change in Case2: Container layout is not really on screen
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<!--container layout is not really on screen-->
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- fragment -->
</FrameLayout>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private FrameLayout frameLayout;
private LinearLayout container;
private RelativeLayout parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
container = (LinearLayout) findViewById(R.id.container);
parent = (RelativeLayout) findViewById(R.id.parent);
//container.bringToFront();
Log.e(TAG, "container: " + parent.indexOfChild(container));
Log.e(TAG, "frameLayout: " + parent.indexOfChild(frameLayout));
if (parent.indexOfChild(container) == parent.getChildCount() - 1) {
Log.e(TAG, "container is visible");
} else {
Log.e(TAG, "container is not visible");
}
}
}
If container index is equal to child count in parent then it is front of other view and is visible. otherwise container is overlapped behind other layout.
I hope it is helpful to you.
With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another layout...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
I'm newbie to Fragments and I'm trying to work on it. Below is my code. For it. when i run my application I don't have any error message but I'm not getting the output
MainActivity.java
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MyFragments.java
public class MyFragments extends Fragment {
public MyFragments() {
// TODO Auto-generated constructor stub
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
fragment_one.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_One"
android:name="com.benchmark.fragmentsdemo.MyFragments"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment_two"
android:name="com.benchmark.fragmentsdemo.MyFragments"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
please anybody help me
Your layout is not correctly implemented.
Try the following layout.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_One"
android:name="com.benchmark.fragmentsdemo.MyFragments"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<fragment
android:id="#+id/fragment_two"
android:name="com.benchmark.fragmentsdemo.MyFragments"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
</LinearLayout>
Orientation of parent LinearLayout was vertical and you are were making
widths of fragment 0dp, instead of height
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
Hey check out this tutorial about Fragments there is a step by step procedure to creating it, So it may be useful to you Click here and also check the documentation which is provided by developer.android.