Overlapping layouts using fragment transaction android - java

So I am trying load my login fragment from my main activity. I think I have a problem not replacing the whole layout in when I use the fragment transcation replace() method.
Here is my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
#Override
protected void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LoginFragment lf = new LoginFragment();
ft.replace(R.id.mapView, lf);
ft.addToBackStack("map to login");
ft.commit();
}
activity_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:showIn="#layout/activity_maps">
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/activity_maps" />
<Button
android:id="#+id/btn
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LoginActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:inputType="textPersonName"
android:text="#string/Username"
android:ems="10"
android:id="#+id/username"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:inputType="textPersonName"
android:text="#string/Password"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/ForgotPassword"
android:id="#+id/forgotPassword"
android:layout_gravity="center_horizontal"
android:textColor="#197cff" />
<Button
android:id="#+id/GoogleButton"
android:layout_width="215dp"
android:layout_height="wrap_content" />
</LinearLayout>
Any recommendations on how I should restructure my xml files?
Thanks for your time!

Any recommendations on how I should restructure my xml files?
Your problems have nothing to do with the xml layouts. First of all embedding that MapFragment in the activity layout and then replacing it with another fragment as the activity starts(onStart()) doesn't make any sense at all. Secondly, embedded fragments(meaning fragments declared in the xml with the fragment tag) can't be used in fragment transactions, they are meant to be static. So remove the MapFragment from the xml layout and do a simple transaction to add the LoginFragment in a empty ViewGroup from the activity layout.

Related

I'm implementing a marquee effect but its not working on Android Studio

I'm implementing a marquee effect but its not working on Android Studio.
This is the code
This is the .xml file part for the textview:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/logos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:fontFamily="serif-monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="TRAVEL PORTAL"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="62dp" />
This is my main_activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView logos = findViewById(R.id.logos);
logos.setEllipsize(TextUtils.TruncateAt.MARQUEE);
logos.setSelected(true);
}

Fragment replaces entire screen

I have the following structures and fragment replace/add does not only replace its host view but whole screen. What can cause this?
Main Activity layout without data
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
app:labelVisibilityMode="unlabeled"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</LinearLayout>
Fragment layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
tools:context=".fragment.HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:background="#drawable/transparent_button"
android:onClick="#{viewModel::onFloatingButtonClicked}"
android:src="#drawable/ic_baseline_add_24" />
</FrameLayout>
Main activity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this, new ViewModelFactory(getApplication())).get(MainViewModel.class);
activityMainBinding.setViewModel(mainViewModel);
activityMainBinding.executePendingBindings();
fragmentList.put(FragmentsEnum.HOME.getValue(), new HomeFragment());
fragmentList.put(FragmentsEnum.PROFILE.getValue(), new ProfileFragment());
Fragments.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment, fragmentList.get(FragmentsEnum.HOME.getValue()), FragmentsEnum.HOME.getValue()); // DEFAULT FRAGMENT
}
Fragments util
public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().add(containerId, fragment, tag).commit();
}
}
On onview of main activity I am setting Home Fragment active when the activity starts. But it hides my bottom nav bar and replaces entire screen.
I think that this is a problem with the usage of LinearLayout as the root element for the fragment and bottomnav. Try replacing it with either ConstraintLayout or CoordinatorLayout. If none of those work try removing the top padding from the Fragment.
Your FrameLayout will just overlap the BottomNavigationView. Furthermore you should use the FragmentContainerView for your nav host. You'd want to have your FragmentContainerView explicitly above your BottomNavigationView. I changed your LinearLayout to a ConstraintLayout with proper constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

open fragment under parent activity

I want to open a fragment in activity but under objects of Activity.
in my code RelativeLayout is parent and LinearLayout is child
but when open fragment on relativelayout I lose linearlayout :(
this is my code:
Main Activity Java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Button fragment1 , fragment2;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
fragment1 = this.findViewById(R.id.fragment1);
fragment2 = this.findViewById(R.id.fragment2);
relativeLayout = this.findViewById(R.id.relativeLayout);
fragment1.setOnClickListener(this);
fragment2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fragment1:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.relativeLayout , new fragment1());
ft.commit();
break;
case R.id.fragment2:
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.relativeLayout , new fragment2());
ft2.commit();
break;
}
}
}
Main Activity XML:
<?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=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/layout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
fragment1 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">
</android.support.constraint.ConstraintLayout>
fragment2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F">
</android.support.constraint.ConstraintLayout>
This is Main Activity: enter image description here
and when click on fragment1: enter image description here
Plz Help Me :)
TNX
Add a framelayout in relativelayout before the linearlayout and inflate the fragment in the framelayout.
<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=".MainActivity"
android:id="#+id/relativeLayout">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
I think you should change your Fragment1 XML and Fragment2 XML from Constraint Layout to LinearLayout. It should work else initiate your LineaLayout in your MainActivity.
You are inflating fragments on Parent Layout. Make two child of Relativelayout and inflate fragment one of them like this:
<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=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
<!-- inflate fragments on FrameLayout -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout">
</FrameLayout>
</RelativeLayout>
Inflate your fragments on this FrameLayout
....Hope this helps :)

Android Studio Change Fragment on Button Click

Basically I have two fragments with two classes for each and one main class and main activity. I want to set one of the fragment as the home screen and when I press a button it shows the other fragment and hides the previous one.
Problems:
My first problem is that the application is not even opening, its saying,"Unfortunately Application stopped working".
Next, when I click on my button both the fragments are merging and the last fragment is not disappearing.
My main class:
public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UpcomingProject Upcoming = new UpcomingProject();
fragmentTransaction.replace(android.R.id.content, Upcoming);
fragmentTransaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
}
My Main Activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/upcomingproject"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/createproject"/>
</LinearLayout>
My Home page fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
>
<Button
android:id="#+id/button_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="#string/button_create"
android:layout_margin="15sp"
android:background="#drawable/drawable_buttoncreate"
android:clickable="true" />
</LinearLayout>
My home page fragment java class:
public class UpcomingProject extends Fragment implements View.OnClickListener {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.upcomingproject, container, false);
Button CreateP = (Button)rootView.findViewById(R.id.button_create);
CreateP.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
CreateProject Create = new CreateProject();
fragmentTransaction.replace(android.R.id.content, Create);
fragmentTransaction.commit();
}
}
My second fragment, the one I want to switch too on the click of a button:
public class CreateProject extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.createproject, container, false);
return rootView;
}
}
And the second fragment's xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView android:id="#+id/name_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_input"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_message"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/contact_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/contact_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="number"
android:hint="#string/contact_description"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/email_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/email_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="textEmailAddress"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/category_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton android:id="#+id/radio_individual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_individual"
/>
<RadioButton android:id="#+id/radio_NPO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NPO"
/>
<RadioButton android:id="#+id/radio_NGO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NGO"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/title_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/title_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="text" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/description_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_description" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:gravity="center"
android:layout_marginBottom="10dp">
<Button android:id="#+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_register"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
<Button android:id="#+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reset"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Try to use getSupportFragmentManager() in your Main Activity to replace first fragment. And getChildFragmentManager() when you replace second fragment
This is a very late reply, but I'll give it in case anyone else hits the same issue.
The reason for this problem was that you have explicitly defined the fragment in your xml file. If you want to swap the fragment it's best to define it as a FrameLayout and then swap it in your onCreate method using e.g.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
fts.add(R.id.content, UpcomingProject.newInstance());
fts.commit();
setContentView(R.layout.activity_main);
}
This is all explained very well and in much more detail in the link in hrskrs's comment above (https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments)

Height MapFragment Layout not work

I'm build an app that contain two tabhost (top and botton) and in one tab, I want to put a Google Map through the Google API. All works well but the map does not display correctly. You can see better in this picture:
My code is:
fragment_mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MapaFragment.java
public class MapaFragment extends Fragment {
public MapaFragment(){}
public GoogleMap mapa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);
if (container != null) {
container.removeAllViews();
}
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
((MainActivity) getActivity()).setBar();
//Asigno el nombre de la vista e integro tabhost-sliding
((MainActivity) getActivity()).setTitle("Mapa");
((MainActivity) getActivity()).integrationMenu();
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
Log.d("DESTROY", "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
}
And my activity_main.xml that contain the two tabhost and a framelayout that call the fragments:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Estructura de los dos tabhost -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TABHOST SUPERIOR -->
<RelativeLayout
android:id="#+id/l1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:layout_above="#+id/l2"
android:layout_alignParentTop="true">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost_sup"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_weight="0"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"/>
</ScrollView>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
<!-- TABHOST INFERIOR -->
<RelativeLayout
android:id="#+id/l2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1"
android:gravity="right"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/RelativeLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="0dp"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
</RelativeLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
Removing scrollview is not convinient solution.Instead of it try android:FillViewPort="True" for Scrollview in your layout solve your problem for all screen where you may require scroll for your activity content.Hope this helps you!!
SOLUTION (by Hardik):
Remove the scrollview in my activity_main.xml and it works well.
Use hierarchiviewer of android tools to check layout values.
For the future:
If you have a ScrollView then elements must specify their height (width for horizontal scroll view) otherwise what height should they have, infinite?
I also had an issue where the exact thing happened when placing the map fragment inside a Relative Layout that had height = Wrap_Content, with minimum height.
Setting a constant height resolved the issue, same idea.
came across this same issue but my map fragment is inside a scrollview and it needs to have a scrollview since there are also other views aside from the map. if it only contains the map, then yes scrollview is not needed.

Categories

Resources