Java activity calling a fragment - java

First le me start by stating I am fairly new to Java and Android Studio, so please bear with me. my background has been in Visual Basic going way back.
I have an activity that tries to verify that what was entered is a valid Item Number by UPC entered. When only one match all is good. My problem starts when the UPC entered brings back more than one matching item. What I am attempting to do (First time using fragments) is Replace part of the screen with a recycler view showing the matching items and descriptions.
When comes backs with multiple items I call an event called setupMulitpleItems with the following code:
public void setupMultiItems(String mScan){
Timber.d("Multiple matching Items");
Bundle bundle = new Bundle();
bundle.putString("valuesArray",mScan);
SelectItemFragment fragobj = new SelectItemFragment();
fragobj.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.include2, fragobj);
fragmentTransaction.commit();
Not sure how many parts of this code will actually work but I am getting underline on the replace line.
"'replace(int, android.app.Fragment)' in 'android.app.FragmentTransaction' cannot be applied to '(int, com.procatdt.stockright.activities.SelectItemFragment)'"
meesage.
I am using the following imports for Fragments.
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.app.FragmentManager;
Figured I would also include the XML for the activity that is currently running when I want to use Fragment.
<?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"
android:background="#color/colorBackgroundGray"
tools:layout="#layout/fragment_select_item"
tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/include2"
layout="#layout/frm1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/layout_numpad5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="220dp"
android:layout_weight="1"
android:layout_gravity="right"
/>
</LinearLayout>
</RelativeLayout>
I am trying to replace include2 with Fragment.
Hope this enough info but if anyone needs to see more let me know and I ill attach code.

You just need to pass the object of your fragment to replace method, there is no need to pass the class declaration and type of class
so just remove
// this is enough
fragmentTransaction.replace(R.id.include2, fragobj);
// remove this
//fragmentTransaction.replace(R.id.include2,android.app.Fragment SelectItemFragment);

Related

layout element defined globally

I'm trying to add a "You are offline" element to the bottom of each layout in my Android app. I would like to define it globally, not to paste the same element to each xml layout file.
I could probably create some ParentActivity and append it programmatically, but is it a good solution?
What is the best way?
Thanks.
Use custom BottomSheetDialog. You do not have to inclue it in layout file. Instead, you will call it programmatically.
BottomMessageDialog:
public class BottomMessageDialog extends BottomSheetDialog {
public BottomMessageDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.dialog_bottom_message);
}
}
dialog_bottom_message.xml:
<?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"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="You are offline"
android:textSize="18sp"
android:gravity="center"/>
</LinearLayout>
Call it:
BottomMessageDialog bottomMessageDialog = new BottomMessageDialog(MainActivity.this);
bottomMessageDialog.show();
Hope it will help.
best way is creating a custom xml file with any name you want and you can use it any number of times you want without any copy and paste.
step 1: creating custom layout named footer_message .
<TextView
android:layout_width="match_content"
android:layout_height="wrap_content"
android:text="You are Offline"/>
step 2: adding that custom layout in another xml which you want that msg.
<include
android:id="#+id/footer_message"
layout="#layout/footer_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
create an xml file
footer.xml
make your layout in it and write this code in the xml files where you want that footer
<include
android:layout_height="wrap_content"
android:layout_width="fill_parent"
layout="#layout/footer"
android:id="#+id/footer"/>

MainPanel input not working in android SlidingPaneLayout

thanks to all, Am using this fragments in SlidingPaneLayout, on opening the application, second fragment was the main and visible first, the issue is when i touch the inputs, it directly focused on the first fragment inputs which is behind the main frament.
<fragment
android:id="#+id/rightpane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.example.incarnus_fragments.SearchReferralOutFragment"/>
<fragment
android:id="#+id/leftpane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
class="com.example.incarnus_fragments.PatientInputDetails"/>

Fragment Confusion

The Android developer documentation for fragments says this:
"Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson."
And partly because of this I got into the habit of always using the fragment manager to add/remove fragments to/from my user interface (even if I did not want to "hot swap" them at runtime). I am 100% certain that when I tried to remove/replace "hard wired" XML fragments at runtime my app crashed with an exception.
I haven't really worked with XML fragments for months, but today, on a lark, I decided to play around and I find that I am able to swap XML fragments for other fragments and...it works? I can't find anything online that discusses a recent change in this behavior. It just works.
My layout code is here:
<?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:id="#+id/fl_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="mobappdev.demo.myapplication.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/frag"
android:name="mobappdev.demo.myapplication.BlankFragment"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/click_me"
android:onClick="clickMe"/>
</LinearLayout>
And here is the code that replaces the fragment:
public void clickMe(View view) {
FragmentManager manager = getSupportFragmentManager();
BlankFragment newFrag = BlankFragment.newInstance();
Fragment oldFrag = manager.findFragmentById(R.id.frag);
Log.i("TESTING", "old frag is null: " + (oldFrag == null));
manager.beginTransaction()
.replace(R.id.fl_frag, newFrag)
.commit();
}
It works without any problems. I've tried variations (putting the XML fragment in a FrameLayout for example) and it all seems to work just fine. I even tried variations such as remove/add and just remove and it all works without a problem.
So what am I missing?
Instead of replacing the fragment itself, you're adding a new fragment on top of that one. It's just a small issue with IDs, but you're replacing using the FragmentManager on the container (LinearLayout) instead of doing it on the fragment itself (R.id.fl_frag instead of R.id.frag)

Android: ListView tells me it's populated but not displaying items

So I've googled, and scoured SO for an answer to what I think is probably a ridiculous oversight, but here goes.
I have a ListView that I'm populating with an ArrayAdapter that I'm building out of a list of objects I'm using elsewhere in my application. I have checked via getCount that there are items in the adapter, both before and after I call .setAdapter(). Nothing is showing up in my application however.
My main layout res/layout/playlistview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playlist_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#80000000"
android:gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/playlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#206"
android:background="#602" >
</ListView>
</LinearLayout>
(I set the colors so I could see what's going on more easily)
the textview for each item res/layout/singlelistitem:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/single_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="5dp"
android:background="#206">
</TextView>
and the code I use to populate it:
private ListView playlistView;
private void buildPlaylistView() {
playlistView = (ListView)findViewById(R.id.playlistview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlelistitem, R.id.single_item, playlist.titlesAsArray());
playlistView.setAdapter(adapter);
playlistView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
((ArrayAdapter<String>)playlistView.getAdapter()).notifyDataSetChanged();
}
playlist.titlesAsArray() does return String[] and it works.
I have both of the .notifyDataSetChanged() in there, since I found that on SO and gave it a try.
When I change the android:layout_height in the ListView object in my XML to wrap_content, I see only the opaque background that is in the LinearLayout that wraps it. When I set the ListView android:layout_height to match_parent, the whole screen is #206 (magentish).
When i check getCount() on the adapter before setAdapter() it says there are items. When I check it after, from the view itself, it says there are the same number of items. I'm totally lost on this one, but nothing is displayed.
Try changing from android:orientation="horizontal" to android:orientation="vertical" this may fix.

Fragments, The method Fragment1() is undefined for the type MainActivity I would like explaining please

I have been following a tutorial on fragments just to get a bit of exposure to them, I've followed the tutorial to the end and Eclipse is throwing the error "The method Fragment1() is undefined for the type MainActivity" now I'm not sure if is to do with the Import.R.Android.* problem I was having earlier and its now not referring to Fragment1.class? Or has something been missed on the tutorial initializing the Fragment?
As I understand from
public void Onclick(View v) {
Fragment newFragment;
if (v == button1) {
newFragment = Fragment1();
}else if (v == button2) {
newFragment = Fragment2();
}else if (v == button3) {
newFragment = Fragment3();
}else {
newFragment = StartFragment();
}
}
It replaces the placeholder Fragment(newFragment) with a fragment based off which button was pressed? Or am I missing something that should obvious? Thank you for any help.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Onclick"
android:text="Frag1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Onclick"
android:text="Frag2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="OnClick"
android:text="Frag3" />
</LinearLayout>
<LinearLayout
android:id="#+id/myFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
TLDR: I know its because the fragment is not defined but since I was following a tutorial I don't know yet how to define one.
The Android developer guides do not help with this particular case as they seem to be implementing them in a different way to this tutorial.
I would suggest finding a nice book or website on how to code in Java before attempting to learn the Android framework, as it seems you have some problems with Java classes and some basic Java syntax.
To Java, newFragment = Fragment1(); is a method call. It has a method name (Fragment), and and is passing in no arguments.
To instantiate an object (such as a Fragment), you need to use the new keyword. For example, Fragment myFragment = new Fragment1();.
Furthermore, it isn't clear from your code snippets, but you must have classes defined of types Fragment1, Fragment2, etc. These should extend the Fragment class.
I highly recommend following the Android developers guide, as the guide you are following doesn't seem to be helping you much, and the official guide provides very nice examples of how Google says you should organize and code your application.

Categories