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.
Related
I made a fragment_console.xml and ConsoleFragment.java
In MainActivity.java I create object console_fragment = new ConsoleFragment();
Then OnClickListener runs console_fragment.method();
But it gives error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
on line
sendButton = v.findViewById(R.id.fragment_console_send_button);
ConsoleFragment.java
public class ConsoleFragment extends Fragment {
View v;
private boolean isVisible;
private Button sendButton;
private TextView consoleText;
private EditText commandText;
public ConsoleFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_console, container, false);
showConsole(v);
return v;
}
public void showConsole(View v){
sendButton = (Button) v.findViewById(R.id.fragment_console_send_button);
consoleText = (TextView) v.findViewById(R.id.fragment_console_log_text);
commandText = (EditText) v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ConsoleFragment console_fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
console_fragment = new ConsoleFragment();
}
public void triggerConsole(View view) { //buton on click
console_fragment.showConsole(view);
}
}
fragment_console.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
tools:context=".activities.ConsoleFragment">
<TextView
android:id="#+id/fragment_console_log_text"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent">
<EditText
android:id="#+id/fragment_console_command_text"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.75"
android:layout_marginBottom="2dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/console_command"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/fragment_console_send_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.25"
android:text="#string/console_send_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".activities.MainActivity">
<include
android:id="#+id/include"
layout="#layout/activity_top_bar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="com.wordfall.wordfallcontroll.activities.ConsoleFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/include2"
app:layout_constraintStart_toStartOf="parent" />
</FrameLayout>
<include
android:id="#+id/include2"
layout="#layout/activity_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
How to get layout object (buttons, tv etc) to get control on it in fragment class?
public class XFragment extends Fragment {
View v;
private boolean isVisible;
private Button sendButton;
private TextView consoleText;
private EditText commandText;
public XFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_x, container, false);
showConsole(v);
return v;
}
public void showConsole(View v){
sendButton = v.findViewById(R.id.fragment_console_send_button);
consoleText = v.findViewById(R.id.fragment_console_log_text);
commandText = v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
}
In your activity_main.xml file you need to create a container for holding fragments like:
activity_main.xml
<LinearLayout
......
......
....
>
.........
........
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
In your MainActivity.java file inside onCreate method you can do:
MainActivity.java
#Override
protected void onCreate(...){
.....
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new ConsoleFragment(), "ConsoleFragment").addToBackStack(null).commit();
}
What view you pass into triggerConsole method from outside fragment? The problem may be in this, you'll try to find controls in layout, which is not part of your fragment. View of your fragment is assigned to global variable v, but now you try to get it from method parameter named v. Try this code.
public void showConsole(){
sendButton = (Button) v.findViewById(R.id.fragment_console_send_button);
consoleText = (TextView) v.findViewById(R.id.fragment_console_log_text);
commandText = (EditText) v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
If you want pass different layouts into same fragment, do it via constructor, not via method parameter, and each time make new instance of fragment.
how to open and close fragment while clicking on same button . Just like amazon filter button.
when we click first time fragment should apper and then we click on that button again fragment should disapper.
business activity java file
public class business extends AppCompatActivity {
TextView t1, t2;
Button hello;
boolean flag = false;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
hello = findViewById(R.id.hello);
t1=findViewById(R.id.text1);
t2=findViewById(R.id.textt2);
}
#Override
protected void onStart() {
super.onStart();
hello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
BusinessSort businessSort = new BusinessSort();
fragmentTransaction = manager.beginTransaction();
if (flag) {
fragmentTransaction.remove(businessSort);
flag=false;
} else {
fragmentTransaction.add(R.id.frgmCont, businessSort);
flag=true;
}
fragmentTransaction.commit();
}
});
}
public void f1(String s1, String s2) {
t1.setText(s1);
t2.setText(s2);
}
}
activity_business.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".business"
android:orientation="vertical"
android:id="#+id/businessLinearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:id="#+id/hello"
/>
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit2"
android:id="#+id/textt2"/>
<FrameLayout
android:id="#+id/frgmCont"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Fagment file BusinessSort.java
public class BusinessSort extends Fragment {
EditText editText1,editText2;
Button submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_business_sort, container, false);
editText1=view.findViewById(R.id.edit1);
editText2=view.findViewById(R.id.edit2);
submit=view.findViewById(R.id.businessfragment_submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = editText1.getText().toString();
String s2 = editText2.getText().toString();
business business = (com.example.project.business) getActivity();
business.f1(s1,s2);
}
});
return view;
}
fragment_business_sort.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BusinessSort"
android:orientation="vertical">
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<Button
android:id="#+id/businessfragment_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
</LinearLayout>
Try replacing the fragment.
fragmentTransaction.replace(R.id.frgmCont, businessSort);
flag = true;
I hope the code is mostly similar to my approach so let me know if the problem still persists so that I will look into it deeper.
public static boolean fragmentState = false;
onClick(){
if(fragmentState){
fragmentState = false;
getActivity().getFragmentManager().beginTransaction().
remove(SomeFragment.this).commit();
}
else{
fragmentState = true;
Fragment someFragment = new SomeFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment );
transaction.addToBackStack(null);
transaction.commit();
}
}
Please make sure that your button is not a part of that fragment.
You need to save the state of the fragment.
I have several fragments. Some fragments have the same buttons at the top and the bottom of the fragment, but the content is different.
Therefore I'm using nested fragments for the top and the buttom part. This is my java class: LogFragment.java. I insert two fragments for the top navigation and the footer at the bottom.
public class LogFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
return rootview;
}
}
This is the layout file: fragment_log.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="#+id/NavigationFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="#+id/button1"/>
</RelativeLayout>
<FrameLayout android:id="#+id/FooterFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The nested NavigationFragment and FooterFragment contains some buttons, images or textfields. For example: fragment_footer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/logInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOG" />
</RelativeLayout>
My question:
How can I access/manipulate/fill in some data of the elements in my nested fragments by the LogFragment.java class?
For example:
I want to access the textview with the id logInfo of the nested fragment_footer.xmlby the LogFragment.java.
Just use [child fragment].getView().findViewById(R.id.[elemnet id]) to get the elements in nested fragments. For example :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
final Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
Button btn1 = (Button)rootview.findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v) {
TextView txv = (TextView)footerFragment.getView().findViewById(R.id.logInfo);
txv.setText("Updated log of Footer");
}
});
return rootview;
}
By the way,
the child fragment is available to access only after onCreateView(),
so you need to declare the child fragment as final or member data of parent.
I have some fragments that are being added programatically with differing tags. Each holds a button with an xml onClick that gets passed a view. How do I identify which fragment was clicked from that view? Code is below.
NewFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.name);
Bundle bundle = this.getArguments();
int index = bundle.getInt("index");
tv.setText(Integer.toString(index));
return view;
}
Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
for (int loop = 0; loop < 4; loop++) {
NewFragment fragment = new NewFragment();
Bundle args = new Bundle();
args.putInt("index", loop);
fragment.setArguments(args);
//tag set to value of loop here
ft.add(R.id.new_fragment, fragment, Integer.toString(loop));
}
ft.commit();
}
public void toggleEdit(View view) {
//How do I find clicked fragment?
}
new_fragment.xml
<?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="wrap_content"
android:id="#+id/test">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_marginLeft="5dp"
android:padding="5dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:text="Test"
/>
</LinearLayout>
<!-- Button in question is below -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/edit"
android:onClick="toggleEdit"
android:id="#+id/edit_button"
/>
</LinearLayout>
Have your Fragment define an interface which your activity implements. Give that interface a method with a string parameter, and onClick, pass in the current Fragment's tag.
See this guide on Fragment communication for more detail http://developer.android.com/training/basics/fragments/communicating.html
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!