Customize the ListView in a Fragment-based Master-Detail Flow? - java

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" />

Related

Using Butterknife with BaseActivity/Child Activities and MaterialDrawer

I'm facing issues implementing MaterialDrawer (library MikePenz) using Multiple Activities ( not using fragments) which is implemented with a BaseActivity. Also, using ButterKnife to bind the views.
Found related issues here in SO and tried it out one by one , but in my case it was not working since most of them using standard Navigation Drawer with Fragments or they are not related with ButterKnife.
Code :
MainActivity class
public class MainActivity extends AppCompatActivity {
#BindView(R.id.toolbar)
Toolbar toolbar;
private List<TestModel> destinations;
private DestinationAdapter mAdapter;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
navManagement();
}
}
activity_main layout
<android.support.constraint.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:id="#+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tvs.ui.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
</android.support.constraint.ConstraintLayout>
StoreActivity
public class StoresActivity extends MainActivity {
#BindView(R.id.searchEditText)
EditText mSearchEditText; // errorRequired view 'searchEditText' with ID 2131558557 for field 'mSearchEditText' was not found
#BindView(R.id.storesRecyclerView)
RecyclerView recyclerView; // here too
private List<TestModel> stores;
private StoreAdapter mAdapter;
//endregion
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stores);
ButterKnife.bind(this);
loadRecyclerView();
testData();
}
}
activity_store layout
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/storesRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
I understand that cause of error is that the ButterKnife.Bind in MainActivity is called when the Layout is inflated but the Views are not inflated in the inherited Activities . The Navigation drawer loads but on clicking item it fails as obvious reason. I cannot use a FrameLayout since I'm using MaterialDrawer library which doesn't ( or I'm not aware) have views to inflate.
I tried several methods extensively searching SO like SO1SO2 Git Git2
Link but was not successful .
Appreciate any help are deeply appreciated . Thanks in advance.
Thanks #mikepenz for the response.
Posting it since it might help somebody
The issue was fixed by creating an abstract method in BaseActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
ButterKnife.bind(this);
setSupportActionBar(toolbar);
navManagement();
}
protected abstract int getLayoutResourceId();
Concrete Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_stores); not required
ButterKnife.bind(this);
loadRecyclerView();
testData();
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_stores;
}
Please note that I was not using ButterKnife.Bind in the Concrete Activity and used #Nullable fields. But currently Binding in both Base and Implementing Activities.
The MaterialDrawer will inflate views to your layout. By default it will add a DrawerLayout into the root of your activity, and add the other content (which is your main layout) as child to it again.
This decision was made to make it super simple to implement a Drawer into your application without needing to alter anything special.
Butterknife will bind the view's at the time you call the bind method.
All views of the MaterialDrawer, are bound at the time of you calling .build(), but as the views of the MaterialDrawer are bound internally you shouldn't have to worry about these.
To complete #user2695433 's answer, you can go a little futher : handle all butterknife init and close in the BaseActivity so you can't forget to release ressources ! Very useful if some new dev come in your team and dont know how butterknife work. Here it is :
--------In BaseActivity-------
protected Unbinder binder;
protected abstract int getLayoutResourceId();
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
binder = ButterKnife.bind(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
binder.unbind(); //release ressource
}
----- In ConcreteActivity------------
#BindView(R.id.title)
TextView title; // butterknife stuff
#Override
protected int getLayoutResourceId() { //define which Id
return R.layout.activity_main;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

Setup the ListFragment to list files from a directory?

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!

In Android SDK/Java, how do I manipulate XML Views(imagebuttons, textviews) from a method in a fragment?

Solved! Thanks everyone for your help.
I am struggling with Fragments as a concept, and especially seem stuck on this one thing. What I'm trying to do is, using my fragment, manipulate its own layout's data. Every time I try to access an ImageButton from within the Fragment, it crashes the application. It works fine from the activity. Am I just misunderstanding Fragments fundamentally?
Code(cut down for size)-
This is the beginning of the activity my fragment is called from:
Display.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragmentClass MyFragment = new MyFragmentClass();
fragmentTransaction.add(R.id.fragment_container, MyFragment);
fragmentTransaction.commit();
The XML for that Activity:
activity_display.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="com.mycompanyname.myprojectname.Display"
android:id="#+id/display_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</RelativeLayout>
The Fragment
MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_screen, container, false);;
}
public void MethodTest()
{
}
}
The Fragment's XML file:
fragment_layout_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/fragment_layout_screen"
tools:context="com.mycompanyname.myprojectname.MyFragmentClass">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_button"
android:clickable="true"
android:onClick="buttonPress"
android:src="#drawable/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Basically I had all of this originally in Display.java, but wanted to add fragments, so I'm trying to move stuff out and into fragments, but I still need the ability to manipulate the xml info, I just can't.
From inside the Display.java activity I can easily call the ImageButton like this:
ImageButton myButton = (ImageButton) findViewById(R.id.my_button)
but if I do the same from MethodTest in the fragment, the app crashes.
I've searched many suggestions on here, trying various solutions from here: findViewById in Fragment but none of those seemed to work.
I've been reading through this for the setup: http://developer.android.com/guide/components/fragments.html and can't seem to find what I'm doing wrong.
Any help would be appreciated, and if you have any questions about my setup, please ask.
declare on fragment private View rootView;
On the onCreateView(...) set rootView = inflater.Inflate(..); and then access the imagebutton as ImageButton mButton =(ImageButton) rootView.findViewById(..);
Do like this in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview, container, false);
GridView gridview = (GridView) view.findViewById(R.id.grid);
return view ;

Android Fragment is not displayed

I'm quite new to Android dev and I followed the official Android's "Get started".
The fact is, my fragment is not displayed on the main activity ( it worked well few days ago but I changed some lines, I don't remember which ones ). I think it's a very basic problem as I don't use sophisticated fragments : it's basically one fragment inside an activity.
This is my activity :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mysecond.MainActivity"
tools:ignore="MergeRootFrame" />
My fragment :
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
And the java code for this activity (I have some other activities in the app, based on the same pattern "one fragment inside one activity" and they work well...)
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container1, new PlaceholderFragment()).commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Any ideas ?
Thank you :)
[edit]
so this is my new onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(
R.id.container1, new PlaceholderFragment()).commit();
}
Still not working for this activity (If I add a button in activity_main.xml I'll be able to see it but the I'm not able to see the TextView in the fragment...)
No errors in logcat and yes the activity is launched (I added some Log.e in onCreate and onCreateView and I cas see them)
In your onCreate methode you don't have to check if the savedInstanceState is null but if the the content of the FrameLayout you use is null
Or you simply always replace the fragment with a new one and ommit any checking.
Instead of add, you can use replace.
You can do it like below shown code:
getSupportFragmentManager().beginTransaction().replace(
R.id.container1, new PlaceholderFragment());
For me this work. Implementa a interface FragmentActions with the init() method and use this
private void showFragment(String fragmentTag){
FragmentTransaction trasaction = getSupportFragmentManager().beginTransaction();
FragmentActions fragment = (FragmentActions) getSupportFragmentManager().findFragmentByTag(fragmentTag);
if(fragment==null ){
if(lastFragmentviewed!=null)
trasaction.hide(lastFragmentviewed);
fragment = (FragmentActions) newInstance(fragmentTag);
trasaction.add(R.id.content_frame,(Fragment) fragment,fragmentTag);
}else{
if(lastFragmentviewed!=null && !lastFragmentviewed.equals(fragment))
trasaction.hide(lastFragmentviewed);
if(getSupportFragmentManager().findFragmentByTag(fragmentTag)!=null){
fragment.init();
trasaction.show((Fragment) fragment);
}else
trasaction.add(R.id.content_frame,(Fragment) fragment,fragmentTag);
}
lastFragmentviewed=(Fragment) fragment;
trasaction.commit();
}

Android crashing at getSupportFragmentManager()

I decided to switch from multiple activities to one activity which switches between fragments however the application now crashes.
Here is the activity I am adding the fragment to
public class MainActivity extends SherlockFragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment fragment = new MyFragment();
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment).commit();
}
Here is the fragment its an observer and has functionality but to save space ill just show the creation
public class MyFragment extends SherlockFragment implements Observer{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.my_fragment, container, false);
}
Heres my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
... HAS SOME TEXT VIEWS HERE!
</RelativeLayout>
The crash I get is
02-15 16:17:41.079: E/AndroidRuntime(18668): FATAL EXCEPTION: main
02-15 16:17:41.079: E/AndroidRuntime(18668): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}:
java.lang.IllegalArgumentException:
No view found for id 0x7f040036 for fragment MyFragmentt{41a05910 #0 id=0x7f040036}
Can anyone help me out here? I can't really figure out what is causing this. I know if I comment out getSupportFragmentManager() in main activity (the top code block in this post) it will run just not draw anything in my fragment.
UPDATE
The frame_container which I'm not sure where to place
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So the way you can use Fragments in your applications are two.
First way is if you declare the Fragment in your xml file like this :
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
The second way is if you add / replace your Fragments dynamically to your container which in the most examples is FrameLayout. Here is how you can do that :
In your main FragmentActivity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myFragmentContainer);
}
and in your xml myFragmentContainer.xml is where you place your fragment_container and it looks like :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and you are adding and replacing your Fragments like this :
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// if there are any extras
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
and for the next Fragment which you want to show just do :
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, secondFragment).commit();

Categories