need help regarding ActionBar And Fragmentation - android

I am using actionbarsherlock library to have action-bar in my application,
I am also using view-pager to switch between the page,
Now the problem is i want only one tab with two fragment to display data regarding each other.
What could be the possible way to do this?
I have been going through like the one below but unsuccessful
In main fragment i am inflating a xml like this
<code>
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.album_view_main, container, false);
return fragView;
}
</code>
xml fie is look like this
<code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/fragment_album_view"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="3"
class="com.nix.music.activity.fragments.AlbumFragMent" />
<FrameLayout
android:id="#+id/frame_list_song"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
</code>
but the problem is i cannot find "frame_list_song" in AlbumFragment class it appears null
what could be the way around ?
thank you

This layout should be set in the parent activity using setContentView(). Then, you would be able to do something like getActivity().findViewById(R.id.frame_list_song) from your AlbumFragment. The better way would be to use an interface and transfer control to the main activity, and not access other fragments directly.

Related

PreferenceFragment faults on id.list but not on id.content

What I really want to do is add a preference screen to an existing layout with other items in it (such as a button) without using deprecated approaches. I have looked at the 'add button to preference screen' and I get that to semi-work using deprecated approaches.
I have two xml layouts. First the preference screen (at the moment the PreferenceCategory is superfluous) with 12 other CheckBoxPreferences omitted for clarity:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="#string/specialization_prefs"
android:title="#string/select_specializations">
<CheckBoxPreference
android:key="#string/pulse_ox_key"
android:title="#string/pulse_ox"
android:defaultValue="true" />
<CheckBoxPreference
android:key="#string/bp_key"
android:title="#string/bp"
android:defaultValue="true" />
</PreferenceCategory>
Then I have the activity layout (PreferenceActivity or Activity; does not seem to matter):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/specializations_layout" >
<Button
android:id="#+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Exit and Save" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
The ListView was meant to be a dummy to be replaced with the PreferenceFragment which I got from a Stackoverflow suggestion. The Button I want to keep.
My PreferenceFragment is as follows:
public class SpecializationsFragment extends PreferenceFragment
{
#Override
public void onAttach(Activity specializationsActivity)
{
super.onAttach(specializationsActivity);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.specializations);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Finally my PreferenceActivity is as follows:
public class SpecializationsActivity extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.specializations_layout);
getFragmentManager().beginTransaction()
.replace(android.R.id.list, new SpecializationsFragment())
.commit();
}
}
If I replace the 'android.R.id.list' with 'android.R.id.content' it sort of works. I see my button but my preference screen scrolls right over it. This is what I get when I follow the deprecated button solution. If I use the 'android.R.id.list' instead, the application crashes saying "addView(View) is not supported in AdapterView.
I have searched on that problem as well. What am I missing? This code currently does nothing but a GUI display. What do I need to add to get the button to display and the preference screen to scroll in the space below it as it is supposed to do in its own 'fragment'? I thought that was the whole idea behind fragments. (Yes they actually work quite well as long as preference screens are not present.) It also makes no difference using the 'add' method versus the 'replace' method.
For anyone who cares I found a solution. Not what I expected but it worked. First I needed to nest a layout within my layout. Then I needed to align this nested layout below the button. I also needed to have the dummy ListView in there but I do not know why. So the xml appears as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Exit and Save" />
<RelativeLayout
android:layout_below="#+id/btn_done"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/specializations_layout" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
The code now has the replace method (using 'add' or 'replace' made no difference) using the id added to the nested relative layout as follows:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.specializations_layout);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.specializations_layout, new SpecializationsFragment())
.commit();
findViewById(android.R.id.list).setBackgroundColor(Color.argb(0, 0, 0, 0));
}
I don't understand what is going on under the hood here, especially with the need for the dummy element and its android:id/list attribute. I would like to have seen it work without the ListView element as I believe would be the case if this was an ordinary fragment and not a preference screen. I would be grateful to anyone who can explain this to me!

Custom ListView in ListFragment or Fragment?

I am working on a simple app, so that the main activity has two fragments that user can swipe to go to. On one of them, I want to have custom list. For example, something like this https://github.com/JakeWharton/SwipeToDismissNOA, where each item in the list can be deleted by swiping. I was able to get a regular list to work inside the ListFragment, but can't get this custom list to work. As I understand, ListFragment needs to have a simple .xml with one , but the one that I want to use, has a bit more stuff in it. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:listSeparatorTextViewStyle"
android:text="ListView" />
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So when I am trying to getListView() using this .xml, it either returns null or when I am trying to do something like this:
View view = inflater.inflate(R.layout.fragment_section_dummy, null);
ListView ls = (ListView) view.findViewById(android.R.id.list);
It throws this error:
10-15 20:25:28.895: E/AndroidRuntime(8081):
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
Thanks.
For me, is easiest work with my own IDs, so I would change your ListView ID to another one. For my example, I choose android:id="#+id/myList".
In your fragment, in your onCreateView method, try inflating your layout and getting your view by ID.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourDummyLayout, container, false);
//You should get your ListView there
ListView yourListView = rootView.findViewById(R.id.myList);
[......]
return rootView;
}
Try with this ;) Good luck !

Cannot Access Views inside Included layout

I am using an "include" layout that is part of a fragment. Here is where I inflate the layout that includes the "include" (and below it is the layout itself):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hourly_fragment, null);
return v;
}
hourly_fragment.xml
<include
android:id="#+id/visualization"
style="#style/Visualization"
android:layout_width="match_parent"
layout="#layout/visualization_with_spinner" />
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/list_fragment" />
<include
android:id="#+id/hourly_amazon_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/amazon_details" />
I can access the items in the other 2 included layouts, but not from the "amazon_details" layout....any ideas why?
Since a Project > Clean did not solve your problem, try setting the parent to the container currently visible in your Content View:
(As you posted in your comment response:)
inflater.inflate(R.layout.hourly_fragment, container, false);
I'm an idiot! I totally overlooked a layout in the layout-large and I am testing on a tablet. Now the views are not null, I just have to figure how to get it to show under the list. I apologize if I wasted anyone's time...my bad and thanks for all the efforts!

updating listviews android

I am very new to android! I've created a vertical linearlayout with two listviews inside my listfragment and I just want every of this listviews show simple array of strings which I can update during runtime. I it seems very difficult for me to realize how to do this. Here are my files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/trainslist"
android:layout_width="240dp"
android:layout_height="wrap_content" >
</ListView>
<ListView
android:id="#+id/timelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
This is a fragment XML file, and here is a fragment file:
public class ToFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] countries = {
"Japan",
"USA"
};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);
setListAdapter(adapter1);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Look, here I try to make one of listviews show what I need and it works. But I don't understand how to thange it's content during runtime (not from onCreateView?) from Activity. It's also unclear for me what is android.R.layout.simple_list_item_1? Why, when I write it, android sends my array to the first listview, not second?
The second issue is I can't change second listview content even in this way, even not during runtime! Please help me deal with this! I really find it so difficult...

RoboFragments in layout.xml

I've been trying to get Roboguice to work with fragments declared in a <fragment> block in the layout file and then injected into the activity, but, although the fragment exists somewhere off screen (an EditText in the fragment takes focus and fires events), It is not visible. Does RoboGuice support what I'm trying to do here or should I go about it a different way?
code:
<?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" >
<fragment
android:id="#+id/myFragment"
android:name="com.example.MyFragment"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_weight="1" >
<!-- Preview: layout=#layout/my_fragment -->
</fragment>
</LinearLayout>
Java:
#ContentView(R.layout.participant)
public final class Main extends RoboFragmentActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#InjectFragment(R.id.myFragment) private MyFragment myFragment;
}
Solved the issue, but for anyone else looking - The issue at hand was completely unrelated to RoboGuice, which allows fragment injection exactly as shown above. Rather the issue was that both of my layout dimensions for the fragment were set to 0dp, ensuring that my fragment would never be rendered.

Categories

Resources