Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment behaving weird

The activity_main.xml is like this

<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" >

<Button 
    android:id="@+id/button_one_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="First Button"
    />

<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first_fragment" />    

    <Button 
    android:id="@+id/button_two_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Second Button"
    />        
</LinearLayout>

The main activity class is like this

package com.example.testfragmentshoneycomb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

first_fragment.xml is like this

<?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:orientation="vertical"
android:background="@color/grey" >"

<TextView
    android:id="@+id/text_view_one_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View one" />

<TextView
    android:id="@+id/text_view_two_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View two" />

<TextView
    android:id="@+id/text_view_three_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View three" />

</LinearLayout>

FirstFragment class is like this

package fragments;


import com.example.testfragmentshoneycomb.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends Fragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.first_fragment, container, false);
    return view;
}

}

It shows only first button and nothing else on the screen. If I remove the first button from activity_main.xml it shows the fragment but doesn't show the second button.

Min SDK version is 11 and build target is android 4.1

like image 617
Atinder Avatar asked Feb 18 '23 00:02

Atinder


2 Answers

set android:orientation="vertical" in your activity layout.

like image 133
Bhavdip Sagar Avatar answered Mar 05 '23 01:03

Bhavdip Sagar


Its because by default LinearLayout's orientation is horizontal. Therefore the whole screens width is captured by the First Button and Fragment.

Are you sure you want to see it like?

First_Button              Fragment            Second_Button

If Yes the use layout_weight. If No then give orientation=vertical to LinearLayout which will show your layout output as

First_Button              
Fragment
Second_Button
like image 32
Lalit Poptani Avatar answered Mar 04 '23 23:03

Lalit Poptani