I am trying to replace a fragment with a button. Basically I have this form (which is all the input views on helloFragment) and I want to click submit and show the form information on the next fragment (showFormFragment).
It renders the first fragment on my device, but the button won't replace it. It doesn't crash however.
activity_main.xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/hello"
android:name="teaminfamous.com.fragments.helloFragment"
tools:layout="#layout/fragment_hello">
</FrameLayout>
</RelativeLayout>
My onCreate and Button listner:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = fm.beginTransaction();
helloFragment hf = new helloFragment();
FormShow fs = new FormShow();
ft.replace(android.R.id.content, hf);
Button submitbtn = (Button) findViewById(R.id.submitButton);
submitbtn.setOnClickListener(submitForm);
}
EDIT:
In Fragment Class helloFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_form_show, container, false);
Button submitbtn = (Button) view.findViewById(R.id.submitButton);
submitbtn.setOnClickListener(submitForm);
// Inflate the layout for this fragment
return view;
}
private View.OnClickListener submitForm = new View.OnClickListener() {
public void onClick(View v) {
//do handling here
final EditText nameField = (EditText) getView().findViewById(R.id.nameEdit);
final EditText originField = (EditText) getView().findViewById(R.id.originEdit);
String name = nameField.getText().toString();
String origin = nameField.getText().toString();
FragmentTransaction ft = fm.beginTransaction();
FormShow show = new FormShow();
ft.replace(R.id.hello, show, show.toString() ).addToBackStack(null).commit();
//final TextView nameText = (TextView) findViewById(R.id.nameShow);
// nameText.setText(name);
}
};
I hit submit and the onClickListner gets called and it shows a blank screen now
I'm pretty new to Android and a novice in Java, so an explanation for the usual pattern for doing something like this would also be helpful.
In order to replace fragment dynamically you have to add it at runtime. To do this you need a container that will host your fragment e.g. FrameLayout:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then you need an access the FragmentManager to begin transaction:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
String tag = fragment.getClass().getSimpleName();
transaction.replace( R.id.fragment_container, new YourFragmentHere(), tag);
transaction.commit();
You should try to change your
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/hello"
android:name="teaminfamous.com.fragments.helloFragment"
tools:layout="#layout/fragment_hello">
</fragment>
to <FrameLayout>
Then, instead of this code:
ft.replace(android.R.id.content, hf);
use
ft.replace(R.id.hello, hf).commit();
and it should work
Related
How do I use the bottomNavigationView variable outside of the MainActivity?
In MainActivity, I would use this to set and reove a badge
// To Add
BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
badge.setVisible(true);
// To remove
bottomNavigationView.removeBadge(menuItem.getItemId());
However it will return null in a fragment if I try the same method
public BottomNavigationView bottomNavigationView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MainActivity main = new MainActivity();
main.bottomNavigationView.removeBadge(2131231026);
I have also tried this in the fragment, but nothing occurs
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_chat, container, false);
final View nav = inflater.inflate(R.layout.activity_main, container, false);
bottomNavigationView = nav.findViewById(R.id.bottomNav);
BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(2131231026);
badge.setVisible(false);
I don't know if you have this in Java (requireActivity) and if you have only one Activity in your project, but It can be the solution :
(requireActivity() as MainActivity).findViewById(R.id.bottomNav)
Also you can add function in your main Activity like addBadge / removeBadge.
But I think you can't get bottomNavigationView outside the main activity because it's part of UI mainActivity and not others fragments
PS : Your line " MainActivity main = new MainActivity();" isn't good because you instanciate an activity and not get actual instance so bottomNavigationView is null. May be use this.getActivity() ( this = fragment instance)
If you want to use BottmNavigationView outside of MainActivity first you should create a separate activity like MenuActivity and suppose you want to use 5 items in BottmNavigationView, so:
public class MenuActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
final Fragment fragment1 = new DiscoverFragment();
final Fragment fragment2 = new CreateFragment();
final Fragment fragment3 = new AnalyticsFragment();
final Fragment fragment4 = new MoreFragment();
final FragmentManager fm = getSupportFragmentManager();
final Fragment[] active = {fragment1};
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();
bottomNavigationView=findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
fm.beginTransaction().hide(active[0]).show(fragment1).commit();
active[0] = fragment1;
return true;
case R.id.add:
fm.beginTransaction().hide(active[0]).show(fragment2).commit();
active[0] = fragment2;
return true;
case R.id.chart:
fm.beginTransaction().hide(active[0]).show(fragment3).commit();
active[0] = fragment3;
return true;
case R.id.more:
fm.beginTransaction().hide(active[0]).show(fragment4).commit();
active[0] = fragment4;
break;
}
return true;
}
});
}
}
XML view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
<FrameLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="MainActivity"
tools:showIn="#layout/activity_main"
android:padding="1dp"
android:id="#+id/main_container"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
app:itemIconSize="26dp"
app:itemTextAppearanceActive="#style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="#style/BottomNavigationView"
app:itemIconTint="#drawable/navigation_view_colored"
app:itemTextColor="#drawable/navigation_view_colored"
app:labelVisibilityMode="labeled"
android:layout_gravity="bottom"
android:background="#android:color/white"
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/bottom_navigation_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
be sure you created 5 fragments like DiscoverFragment, CreateFragment, and so on ...
and XML view of them.
I'm trying to have a fragment loaded as the default view for the activity, but I just get a blank screen and huge memory leaks.
Activity file onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getFragmentManager();
int fragmentTransaction = fragmentManager.beginTransaction()
.add(R.id.login_screen_fragment, new FragmentLogin())
.commit();
}
}
The XML for the activity:
<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=".LoginActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_screen_fragment"
class="com.test.project.FragmentLogin"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/forgotten_password_fragment"
class="com.test.project.FragmentForgottenPassword"
/>
</FrameLayout>
And the Fragment class (relevant parts):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
final Activity activity = getActivity();
... code ...
return view;
}
I followed instructions from a tutorial someone suggested earlier from a different question, but I must be doing something horribly wrong. Any ideas?
xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/changeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
</FrameLayout>
your activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentLogin f1= new FragmentLogin();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.changeFragment, f1);
fragmentTransaction.commit();
}
}
I'm new to Android and I ran into the same problem. Here's how I fixed mine.
So in your onCreate method, use getSupportFragmentManager() from the FragmentManager class then declare FragmentTransaction separately. See below. I did this for my case and it worked.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.login_screen_fragment, new FragmentLogin());
fragmentTransaction.commit();
}
Hope this helps solve your problem as well.
Sincerely,
sylvery
Just replace the fragment element with FrameLayout, then you can see your fragment.
If you wanted to add the Fragment programatically, then remove the <Fragment> in your Framelayout and add some ID unto it.
It should look like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/login_screen_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity" />
So I've searched for hours trying to find an answer to this specific problem I'm having with no luck. Would be great if someone can help me out.
So essentially I have a MainActivity.java, FragmentA.java, activity_main.xml, and fragment.xml. So what fragment.xml has is just a container that have 3 buttons in a box that when pressed Start showing a number starting from 3 as the buttons text. So when you click the button again, it changes the button text to 2 and then 1 and then 0 and then back to 3. All the logic that decrements the numbers and changes the button text to that number is in the FragmentA.java class. All that's logic is working, I've tested it. Finally I create and add FragmentA dynamically to MainActivity's activity_main.xml LinearLayout when an add button in actionbar is clicked.
So now when you press the add button in the actionbar in MainActivity, the fragment.xml view is correctly appended to activity_main.xml's LinearLayout. The problem is, only the first fragment that's appended in the LinearLayout is working (numbers decrement) when button is clicked. The other fragment copies are not working or doing anything, but just showing. So the buttons decrement logic is only working for the first added view. Why is that? I thought the point of fragments was to elimanate duplication. I'm guessing this is some ID issue with the same fragment being created multiple times? Any idea on how to fix this?
FragmentA.java
Button b1, b2, b3;
int[] mCounter = {3, 3, 3};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b1 = (Button) getActivity().findViewById(R.id.button);
b1.setOnClickListener(this);
b2 = (Button) getActivity().findViewById(R.id.button2);
b2.setOnClickListener(this);
b3 = (Button) getActivity().findViewById(R.id.button3);
b3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button:
b1.setText(Integer.toString(mCounter[0]));
decrementCounterByButtonPosition(0);
break;
case R.id.button2:
b2.setText(Integer.toString(mCounter[1]));
decrementCounterByButtonPosition(1);
break;
case R.id.button3:
b3.setText(Integer.toString(mCounter[2]));
decrementCounterByButtonPosition(2);
break;
}
}
public void decrementCounterByButtonPosition(int counterId) {
if (mCounter[counterId] != 0) {
mCounter[counterId]--;
} else {
mCounter[counterId] = 3;
}
}
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_add_exercise) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA ContainerFragment = new FragmentA();
fragmentTransaction.add(R.id.container, ContainerFragment, "HELLO");
fragmentTransaction.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml
<ScrollView
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"
android:background="#1FDA9A">
<LinearLayout
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" tools:context=".MainActivity"
android:orientation="vertical"
android:id="#+id/container">
</LinearLayout>
fragment.xml
<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="wrap_content"
android:paddingTop="0dp"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/globalContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#fff"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:backgroundTint="#333"
android:gravity="center_vertical|center_horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:maxWidth="10dp"
android:maxHeight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:maxWidth="10dp"
android:maxHeight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:maxWidth="10dp"
android:maxHeight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
Any help appreciated. Thanks :D
In addition to Abdullah Asendar's answer, you have another thing to fix.
The problem is with the way you are getting your views.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b1 = (Button) getActivity().findViewById(R.id.button);
b1.setOnClickListener(this);
b2 = (Button) getActivity().findViewById(R.id.button2);
b2.setOnClickListener(this);
b3 = (Button) getActivity().findViewById(R.id.button3);
b3.setOnClickListener(this);
}
This finds views from the Activity, not from your fragment.
The correct way to do this is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
b1 = (Button) v.findViewById(R.id.button);
b1.setOnClickListener(this);
b2 = (Button) v.findViewById(R.id.button2);
b2.setOnClickListener(this);
b3 = (Button) v.findViewById(R.id.button3);
b3.setOnClickListener(this);
return v;
}
and get rid of your onActivityCreated override because it no longer does anything special.
notice the subtle change View v = inflater.inflate(R.layout.fragment, container, false);
I am not quite sure but I think the problem is in the ContainerFragment Object
FragmentA ContainerFragment = new Fragment();
try to change it to :
FragmentA ContainerFragment = new FragmentA();
if you can't pass the object fragmentTransaction.add
do this:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
I hope this helps.
I have a ViewPager in which I am able to view between 4 different tabs (which are fragments) I created. In one of my fragments, I want to have a button that will, after I click a button on that page, replace that view with another fragment. Now I don't want to swipe to another tab, I just merely want to replace the view of the current fragment when I press that button. Here is my attempt, but I'm not sure why when I click my button, nothing happens. If anyone could help me that would be great. Thanks!
This is the Fragment I call to create the view and the button I want to press.
ManualSelection.java
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
}
}
And here is the Fragment I want to create
ManualSelectionListView.java
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ManualSelectionListView extends Fragment {
static final String[] MOBILE_OS =
new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.listview, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.listview);
lv.setAdapter(new MobileArrayAdapter(getActivity(),MOBILE_OS));
return rootView;
}
}
And here are my XML files:
activity_manual_selection.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
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"
tools:context="com.ecs160.homestay.ManualSelection"
android:id="#+id/fragmentTest">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="#+id/manual_generate"
android:layout_centerHorizontal="true"
android:onClick="generateListView"/>
And here is the XML file called listview.xml that contains R.id.listView_manual
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView_manual">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I get nothing displayed when I hit "Generate." Any ideas? Thanks!
In your Activity you should create this:
public void generateListView(View v){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
The problem is that you are trying to get the Button id from your main activity which will return null
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
Button manual_generate is located in your activity_manual_selection layout not in your Main activity's layout you cant get view from different layout or else it will return null
solution:
Get the Button's id from your fragment's layout
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
Button listViewGenerated = (Button) rootView.findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
return rootView;
}
remove onActivityCreated
So I am very new to the Android DK, and was sailing pretty fine until I started trying to mess with Fragments.
At the core of it, what I'm trying to do is have a series of buttons on my app's screen, and whichever button the user presses will change what text/spinners/buttons display on the screen.
How I decided to implement that was via Fragments. I can't figure out why my Fragment isn't displaying however, this should be a relatively simple example.
I had a activity_main.xml that is relatively simple. I've changed variable names, but see below:
<LinearLayout android:id="#+id/mainActivity"
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:animateLayoutChanges="true" >
<Button
android:id="#+id/buttonD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/d_name"
android:onClick="dMenuButton" />
<RelativeLayout
android:id="#+id/fragment_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button
android:id="#+id/buttonR"
android:layout_below="#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/r_name"
android:onClick="rButton" />
</LinearLayout>
And the corresponding MainActivity.java
public void dMenuButton(View view){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.show(dFragment);
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.main, menu);
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
dFragment = new DFragment();
fragmentTransaction.add(R.id.fragment_container, dFragment);
fragmentTransaction.commit();
}
}
And the fragment_d.xml
<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" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/text1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/numD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/text1"
android:layout_toRightOf="#id/text1" />
</RelativeLayout>
With its respective DFragment.java
public class DFragment extends Fragment {
static Spinner dSpinner;
ArrayAdapter<CharSequence> dAdapter;
RelativeLayout view;
public static DFragment newInstance() {
DFragment dFragment = new DFragment();
return dFragment ;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = (RelativeLayout) inflater.inflate(R.layout.fragment_d,
container, false);
createSpinners();
return view;
}
private void createSpinners() {
dSpinner = (Spinner) view.findViewById(R.id.numD);
dAdapter= ArrayAdapter.createFromResource(getActivity().getBaseContext(),
R.array.numD,
android.R.layout.simple_spinner_item);
}
}
Currently, the fragment layout is covering up the buttons beneath it. Any help in forcing it to push down the components below would be great.
Thanks in advance.
You are definitely on the right track with the visibility of the fragment_container. You could leave that as visible constantly, then allow the fragment transactions to change what is being displayed in that container.
Depending on your requirements, you could simply perform a .replace(R.layout.fragment_container, anotherFragment) to change which fragment is being displayed (code is pseudo only).
As for the buttons, are you adding them to the activity_main.xml or are they being added in the fragment's layout? As long as you add the new buttons/text after the fragment_container layout in the main activity layout, you should be fine. Let me know exactly what you are trying to achieve with the buttons/text!
Good luck!