I am working on a sound recorder app. I have made the app so that it properly records mp3 files and saves them. Now I want to make it to be able to display the recordings and after they are clicked to start up a custom dialog in which the user can play the file, pause, or move the progress bar...
This is what I basically want to achieve here:
This is what I have at the moment:
I would like to populate the ListFragment with mp3 files from a specific directory. So basically list files from SDCard in ListFragment.
public class TwoFragment extends ListFragment implements AdapterView.OnItemClickListener{
public TwoFragment() {
// 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
return inflater.inflate(R.layout.fragment_two, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.heroes, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
Toast.makeText(getActivity(), "Item " + i, Toast.LENGTH_SHORT).show();
}
}
And this is the xml file for FragmentTwo
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
I am kinda lost at the moment, I have tried googling, haven't really made any progress as I don't know how to properly "link"(?) the files from sdcard to display in ListView. I should probably do something with the Adapter but I'm not sure. Thanks for any help!
Related
I have a webview set up but when I click on the item, an empty fragment shows up where the actual web item is supposed to be. I have tried loading my urls on mobile browser and they work fine. I have an internet permission in android manifest.
web view layout file
<?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.HomeActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
webview fragment
public class WebViewDetailsFragment extends Fragment {
private WebView detailsWebView;
private String articleSource;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_web_view_details, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
detailsWebView = view.findViewById(R.id.webView);
detailsWebView.getSettings().setLoadsImagesAutomatically(true);
detailsWebView.getSettings().setJavaScriptEnabled(true);
detailsWebView.getSettings().setUseWideViewPort(true);
detailsWebView.getSettings().setLoadWithOverviewMode(true);
detailsWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Configure the client to use when opening URLs
detailsWebView.setWebViewClient(new WebViewClient());
setItems();
detailsWebView.loadUrl(articleSource);
}
private void setItems(){
NewsArticle newsArticle = NewsFragment.news.get(NewsAdapter.pos);
articleSource = "https://www.google.com";
}
}
I too tried your Code. it is working fine and good.There may be a chances of issue in fragmentManager. Did you checked the view is visible or not
I have a Android fragment containing a ListView, and I have attached an ArrayAdapter to the ListView, but when I insert items into the ArrayAdapter, the ListView only shows the first item.
onCreateView iterates over my Technologies, if they're unlocked, it adds them to the ArrayList shownTechs, but only the first item ever appears
public class TabTechnologyList extends Fragment {
private static final String TAB_NAME = "tab_name";
private ListView techList;
private ArrayList<Technology> shownTechs = new ArrayList<>();
ArrayAdapter<Technology> techListAdapter;
public TabTechnologyList() { }
public static TabTechnologyList newInstance() {
TabTechnologyList fragment = new TabTechnologyList();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabData = Globals.getTabData();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
techList = (ListView) view.findViewById(R.id.tabTechListList);
techListAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.layout_techlist_element, shownTechs);
techList.setAdapter(techListAdapter);
for(Technology tech: tabData.getTechnologies()) {
shownTechs.add(tech);
}
//Only the first one is shown on the screen
return view;
}
}
}
When I google for similar issues, people seem to be recreating the ArrayAdapter instead of editing the existing one, but I'm not doing that, and cant' figure out what problem remains. Any suggestions?
fragment_technology_list.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"
tools:context="com.tbohne.kittens.front.TabTechnologyList"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabTechListList" />
</RelativeLayout>
layout_techlist_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
Addition
I have just realized that this fragment is not filling the parent height, and is only tall enough to show the one entry, as if the fragment height were wrap_content. (I can't scroll it, so it appears as if there's only the one). This makes me think the nesting of fragments may be related, so here's the code that creates the fragment:
public class TabTechnology extends Fragment {
private TabTechnologyList table;
private OnFragmentInteractionListener mListener;
public TabTechnology() {}
public static TabTechnology newInstance(String tabName) {[SNIP]}
#Override public static onAttach(Activity activity) {[SNIP]}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_technology, container, false);
table = TabTechnologyList.newInstance(tabName);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.view_table, table);
transaction.commit();
return view;
}
}
fragment_technology.xml
<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="com.tbohne.kittens.front.TabTechnology"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_table"
android:layout_above="#+id/view_details" />
<View android:layout_width="match_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_details"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true" />
</LinearLayout>
Putting the Fragment inside the ScrollView was the source of the problem. Simply replaced those with FrameLayout, and suddenly everything magically works. Very strange.
I suspect the problem has something to do with nesting scrollable objects.
I have a listfragment class:
public class activityfeedScreen extends ListFragment implements OnItemClickListener {
private ListView activityfeed_feedList_listView;
private ActivityFeedBaseAdapter adapter;
public static final activityfeedScreen newInstance()
{
activityfeedScreen fragment = new activityfeedScreen();
Bundle bdl = new Bundle(1);
fragment.setArguments(bdl);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onItemClick(AdapterView<?> activityfeedlistview, View view, int position, long itemID) {
Intent intent_activityfeed_showitemdescription = new Intent(view.getContext(), activityfeedItemScreen.class);
startActivity(intent_activityfeed_showitemdescription);
}
}
In this class I am getting an error at the line:
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
I initially thought it was a problem due to the getView() being null so I moved that from the onCreate method to the onCreateView() method. But I am getting the runtime error as stated in the title.
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#E5E5E5"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/activityfeed_profilePicture_imageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/default_profile"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B9B9B9"
android:layout_marginTop="20dp">
<ListView
android:id="#+id/activityfeed_feedList_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
I understand that I must change the listview id to
android:id="#android:id/list"
but I am not sure then how to call that in my onCreateView method.
Thanks for all your help!
Yes. First of all you do have to change the ID of your ListView like so:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
In your onCreateView you can reference the ID by android.R.id.list. This is because Android built-in resource reside in android.R. You can also find there drawables, styles, etc. (you can call them in a similar maneer).
Taking the above into consideration, your onCreateView method should look like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(android.R.id.list);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
That's all ;)
In onCreateView() you need to provide a layout that has a ListView with and id of #android:id/list.
ListFragment provides some helper methods (e.g setListAdapter()) that require the layout to have a ListView with that id so it can reference it and set the adapter for you.
New to android programming. For the past 3 days I've been going through tutorials and feel like I get the major parts of it. Every time I try to run the following code, android causes it to force quit before even opening up. I've gotten other more simpler fragments to work with little effort, but this is confusing me. There is no error being printed at all, probably because it shuts down immediately. Any help would be much aprreciated.
EDIT: Using the debugger I was able to get an
"InflateException: Binary XML file #9: Error inflating class fragment.
It stops after the follwing code:
lv = (ListView) view.findViewById(R.id.lvMain);
Frag_MainList.java
public class Frag_MainList extends Fragment implements OnItemClickListener, OnItemLongClickListener{
ListView lv;
ImageView ivAddNote;
Communicator communicator;
Adapter_SQL database = new Adapter_SQL(getActivity());
ArrayList<Element> list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_mainlist, container, false);
lv = (ListView) view.findViewById(R.id.lvMain);
database.open();
list = database.getChildrenLess();
database.close();
Adapter_List adapter = new Adapter_List(getActivity(), list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
communicator.SendID(pos);
return true;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
database.open();
database.switchComplete(list.get(pos).id);
database.close();
}
public void setCommunicator(Communicator communicator){
this.communicator = communicator;
}
public interface Communicator{
public void SendID(int index);
}
}
Activity_Main.java
public class Activity_Main extends Activity implements Frag_MainList.Communicator{
Frag_MainList frag1;
Frag_ListChildren frag_children;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
frag1 = (Frag_MainList) manager.findFragmentById(R.id.fMainList);
frag1.setCommunicator(this);
}
#Override
public void SendID(int index) {
frag_children = (Frag_ListChildren) manager.findFragmentById(R.id.fListChildren);
if (frag_children != null && frag_children.isVisible()){
frag_children.displayChildren(index);
}else{
Intent intent = new Intent("com.likwid.wishlist.LISTCHILDREN");
intent.putExtra("id", index);
startActivity(intent);
}
}
}
frag_mainlist.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" >
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
activity_main.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="wrap_content"
android:background="#00BBFF"
android:orientation="horizontal"
android:id="#+id/main_layout" >
<fragment
android:id="#+id/fMainList"
android:name="com.likwid.wishlist.Frag_MainList"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
I figured it out. It seems the problem was with the database. getActivity in Frag_MainList would be called before the activity was actually created. Putting getActivity in the the "onActivityCreated" method allowed for getActivity to be properly called and seemed to take away all the problems it was causing. I think the reason it was showing a fragment error is because the fragment was faulty, making inflating in it impossible. Thanks everyone!
I found the template that ADT generates for a Master/Detail Flow Activity to be rather obtuse, and I don't like the fact that the ListView is not declared in a layout file which forces me to work with it programatically. For example, instead of just setting android:background on the ListView in a layout file, I'm forced to find the ListView via findViewById in the fragment's onCreateView(...) method, then call setBackground(...). For maintainability and general readability, I'd like to do the same thing via a layout file.
Instead, I'm trying to inflate a custom layout in the onCreateView method of the "master" fragment:
public class InboxFragment extends ListFragment {
public InboxFragment() {}
private ListView listView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
listView = (ListView) inflater.inflate(R.layout.inbox_fragment_layout,
null);
return listView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// the following doesn't work
listView.setAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
// nor does this
//setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
// android.R.layout.simple_list_item_activated_1,
// android.R.id.text1,
// DummyContent.ITEMS));
}
}
However, calling setListAdapter (or even setAdapter) doesn't seem to do anything, even though I've given the ListView an id of android:id="#id/android:list" in R.layout.inbox_fragment_layout:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#D3D3D3" />
My "master" activity just calls a fragment layout:
public class InboxActivity extends FragmentActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_phone_layout);
}
}
inbox_phone_layout.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentPhoneInbox"
android:name="com.justin.inbox.InboxFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
Instead, I'm greeted with a blank page and it doesn't look like the ListView is loaded at all. What am I doing wrong, here? Sample project on GitHub.
I fixed your issue by changing inbox_phone_layout.xml to the following:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentPhoneInbox"
android:name="com.justin.inbox.InboxFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />