java - Overlapping fragments in activity -
i have been learning android app development couple of days using 'developer.android.com' tutorial. m learning concept of fragments , frame-sets. have written hello world program using fragments. prob fragment layout gets inserted in fragment container twice , result 2 overlapped fragments in frame-set. can u please tell me why happens , wrong code?
below home_page.java (the activity contains fragment_container)
package com.technology.computer.mit.ctechmit; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class home_page extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_page); // create new fragment placed in activity layout home_pagefragment firstfragment = new home_pagefragment(); // in case activity started special instructions // intent, pass intent's extras fragment arguments firstfragment.setarguments(getintent().getextras()); // add fragment 'fragment_container' framelayout getsupportfragmentmanager().begintransaction() .add(r.id.fragment_container, firstfragment).commit(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_home_page, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } below home_pagefragment.java (the fragment supposed inserted in container)
package com.technology.computer.mit.ctechmit; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; /** * placeholder fragment containing simple view. */ public class home_pagefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_home_page, container, false); } } below activity_home_page.xml (fragment_container layout)
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment_container" android:name="com.technology.computer.mit.ctechmit.home_pagefragment" tools:layout="@layout/fragment_home_page" android:layout_width="match_parent" android:layout_height="match_parent" /> below fragment_home_page.xml (fragment layout)
<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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:orientation="horizontal" tools:context=".home_pagefragment"> <textview android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" /> </linearlayout> can u please me error? why fragment getting loaded twice in home activity? getting overlapped hello world text.
it's simple. layout inserting fragment not view using container. in fact, it's fragment itself.
so either use framelayout fragment container, or keep fragment in xml , not call fragment manager insert fragment. suggest first one.
<framelayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
Comments
Post a Comment