Fragment transaction from main activity doesn't work - java

I'm writing my first android app using fragments but I can't figure out why it doesn't change view from main activity to fragment.
This is my mainActivity.java:
public class MainActivity extends AppCompatActivity {
Button fragment1_BTN, fragment2_BTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1_BTN = findViewById(R.id.frag1_btn);
fragment2_BTN = findViewById(R.id.frag2_btn);
fragment1_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
replaceFragment(new Frag1());
}
});
fragment2_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
replaceFragment(new Frag2());
}
});
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}}
This is my 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=".MainActivity">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/therapists_btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:backgroundTint="#color/teal_700"
android:text="button 1"
android:layout_marginRight="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/patients_btn" />
<Button
android:id="#+id/patients_btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:backgroundTint="#color/teal_700"
android:text="button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/therapists_btn"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>
The problem is that when I click on button 1 I see the view of first fragment and also of main activity.
How can I see only the fragment view?

your frameLayout container for Fragments is placed before LinearLayout, Fragments View is loaded into it and is visible, but also rest of Views is drawn after that (as order in XML) on top of it. just setVisibility(View.GONE) for this LinearLayout when you are loading Fragment (set for it some id and use findViewById as for buttons). another approach would be to put your LinearLayout inside container - you are using replace method so Fragment will remove your buttons, but in that case these are gone forever then and you don't have an easy option for bringing them back (with your current code - only whole Activity restart)
it would be way better (by design) if your Activity wouldn't have these buttons at all, only container, and on start would check if container has anything loaded (e.g. getChildCount()==0), if not then load some initial Fragment for preventing displaying empty Activity

Related

Button OnClick from Fragment to MainActivity

I have 3 Fragments inside my MainActivity and in one of my Fragments I have 3 ImageView and each of image view performs same action based on which ImageView was selected, now What I want to achieve is that instead of using OnClickListener for each of Views separately, call same method with switch-case or if-else inside. But the problem is those ImageViews inside fragment cannot call Functions implemented inside MainActivity. for instance. here's the code to understand what I want to say:
MAIN ACTIVITY
public void imageClicked(View v){
if(v.getID() == findViewById(R.id.id_of_imageView_1).getID()){
myTextView.setText("ImageView 1 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_2).getID()){
myTextView.setText("ImageView 2 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_3).getID()){
myTextView.setText("ImageView 3 is Clicked")
}
}
XML (fragment_images.xml) WHERE IMAGES EXIST
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/id_of_imageView_1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/laptop" />
<ImageView
android:id="#+id/id_of_imageView_2"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/memory" />
<ImageView
android:id="#+id/id_of_imageView_3"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/screen" />
</LinearLayout>
</ScrollView>
XML (activity_main.xml) HERE I Link my Fragment and TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
//CHANGE THIS TEXT VIEW WHEN IMAGE IS CLICKED
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="Select Product"
android:textSize="20sp"
android:textStyle="bold" />
<fragment
android:id="#+id/fragment_1"
android:name="com.salmanibrahim.calssifiedelectronics.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_list" />
<fragment
android:id="#+id/fragment_2"
android:name="com.salmanibrahim.calssifiedelectronics.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_detail" />
//THIS IS THE FRAGMENT
<fragment
android:id="#+id/fragment_images"
android:name="com.salmanibrahim.calssifiedelectronics.AddProduct"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_images" />
</LinearLayout>
How do I call imageClicked() defined inside MainActivity using onClick on ImageView
In your fragment you can call imageClicked() in the MainActivity by using requireActivity() or getActivity() and do the necessary cast
In fragment:
public View onCreateView(...) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
MainActivity mActivity = (MainActivity) requireActivity();
imageView1 = view.findViewById(...);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mActivity.imageClicked(imageView1);
}
});
...}
repeat the same for imageView2, 3
Also the condition in imageClicked() seems not right as you findViewById which will point to your MainActivity layout, not the fragment.
So you need to change this
if (v.getID() == findViewById(R.id.id_of_imageView_1).getID())
To
if (v.getID() == R.id.id_of_imageView_1)
And do the same for other images.
I understand what you trying to do but android:onClick parameter only links for the context declared in tools:context field on the parent tag of your layout file.
So it is not possible to reference your imageClicked() function found in your MainActivity from your fragment's ImageView because they are in different context. Check this for more info.
You are doing it in wrong way.
You can do this instead.
imageView_1.setOnClickListener(this);
then implement activity from View.onClickListener
then get view by id on the method that's been implemented.
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.id_of_imageView_1:
// do your things here
break;
}
}

How do I hide or show layout elements when an Android Activity goes out of focus?

I am trying to hide certain elements in a layout when Android goes out of focus, such that when viewed in the "View recent applications" display, the layout elements will not be visible. Upon selecting the application and going into focus, these elements will be visible again.
My implementation below attempts to show the "content" layout when the application is in focus, and show the "overlay" layout when out of focus, via onWindowFocusChanged().
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the content page"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the overlay"/>
</RelativeLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
FrameLayout layoutContainer;
RelativeLayout layoutContent;
RelativeLayout layoutOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutContainer = (FrameLayout) findViewById(R.id.container);
layoutContent = (RelativeLayout) findViewById(R.id.content);
layoutOverlay = (RelativeLayout) findViewById(R.id.overlay);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
layoutContent.setVisibility(View.VISIBLE);
layoutOverlay.setVisibility(View.GONE);
} else {
layoutOverlay.setVisibility(View.VISIBLE);
layoutContent.setVisibility(View.GONE);
}
}
}
enter code here
However, my implementation does not work. Anyone with any suggestion as to solve this? Thanks!
If you want to hide views in your layout in recent apps try like this
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);

Reason for a fragment to not show up

so I know there are a few topics around here that ask about this but none seemed to be the cause of my issue. I have an ActionBarActivity that is going to be switching between a few different fragments, but I'm having a problem getting one of the fragments to show up. The activity does show a fragment when it is first loaded and does that fine. Here is the code that is setting up the initial fragment with the method to switch between fragments that I'm using.
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
final ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
if (savedInstanceState == null) navigateTo(START_SCREEN);
}
public void navigateTo(int sectionNumber, Serializable objectParam) {
Fragment fragment = null;
switch (sectionNumber) {
case START_SCREEN:
fragment = createStartScreenFragment(); //returns an instance of the class for this fragment
break;
case SCREEN_TWO:
fragment = createSecondScreenFragment(); //returns an instance of the class for this fragment
break;
}
if (fragment == null) return;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction trans = fm.beginTransaction();
trans.add(R.id.activity_container, fragment, fragment.getTag());
trans.commit();
trans.show(fragment);
}
activity_containers.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="#+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
The first fragment that loads successfuly
<GridLayout
android:id="#+id/fragment_start_screen"
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="com.android.Activity">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
/>
...
</GridLayout>
And here is the fragment xml that isn't showing up
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
And here is the misbehaving fragments relevant code:
public class SecondFragment extends Fragment {
public final static int SECTION_NUMBER = 1;
private String mTag = "TAG";
private ParentActivity parentActivity;
public SecondFragment() {
}
public static fragment newInstance() {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
parentActivity = (ParentActivity) getActivity();
View rootView = inflater.inflate(R.layout.fragment_second_fragment, container, false);
ListView list = (ListView) rootView.findViewById(R.id.list);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
The biggest problem I'm having here is that there is no error message shown. The text view in the first fragment is supposed to trigger the second fragment to show up on the screen. This used to work when I had that fragment as a DialogFragment and just called .show() on an instance of it. I need it to be a regular fragment now and it doesn't show up. There's no error message that I can see, though debugging it does show it running through all of my code successfully and it looks like its properly inflating the view.
So what could I be missing here that is preventing the fragment from displaying?
Your layout
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
Should be within a ViewGroup
<?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="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
</LinearLayout>
And you should return rootView in onCreateView().
Also, you might need to use replace method of Fragment Manager to replace one fragment with another, instead of add.

Replacing multiple fragmentclass using one fragment view

i am having some trouble understanding and using fragments i have 3 buttons and 1 fragment views .using the buttons as Tabs i am trying to give the fragment a specific class when clicking on a button what i did is this :
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1" >
<Button
android:id="#+id/Tab1"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 1" android:background="#drawable/btn"/>
<Button
android:id="#+id/Tab2"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 2" android:onClick="switchToTab2" />
<Button
android:id="#+id/Tab3"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<fragment
android:id="#+id/fragment1"
android:name="com.example.newproject.fragmentclass"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
java class :
fragmentclass frt1;
fragmentclass2 frt2;
fragment3class frt3;
FragmentTransaction ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tabs_exp);
ft = getSupportFragmentManager().beginTransaction();
frt1 = new fragmentclass();
frt2 = new fragmentclass2();
frt3 = new fragment3class();
//ft.add(R.id.fragment1,frt1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu);
return true;
}
public void switchToTab2 (View v){
ft.replace(R.id.fragment1, frt2).commit();
}
public void switchToTab3 (View v) {
ft.replace(R.id.fragment1, frt3).commit();
}
when i click on button2 the fragment class2 is successfully visible except that the default class of the fragment remain visible as well is there anyway to hide it and make the new fragment class active
Adding fragments in your xml file will make is static through out your application and you can not change it dynamically.
If you want to add Fragments dynamically then you need to add FrameLayout in your xml file and load all your fragments dynamically in that layout and replace it one another while loading fragments one after another.
Do below change in your layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<FrameLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
Remove fragment1 from your XML file (so container is empty by default on layout creation time) and add it from code (i.e. in onCreateView() of parent fragment or in onCreate() of Activity), as fragments cannot be removed out of hierarchy if they are part of XML layout file.
I'd also change container to be FrameLayout instead of LinearLayout if you plan to have just one child in it.

Android DK Trying to show/hide part of the UI via Fragments

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!

Categories

Resources