I have the following implementation which runs with no hassle. When user clicks on a button then list view shows. However, list view shows up in the middle of screen. I want it to show up just beneath of the clicked button. Here is my implementation:
MainActivity
namespace DialogExample
{
[Activity (Label = "DialogExample", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private Button mClickBtn;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
mClickBtn = FindViewById<Button> (Resource.Id.myButton);
mClickBtn.Click += MClickBtn_Click;
}
void MClickBtn_Click (object sender, EventArgs e)
{
FragmentTransaction transaction = FragmentManager.BeginTransaction ();
Dialog_Form dialogList = new Dialog_Form ();
dialogList.Show (transaction, "Dialog fragment");
}
}
}
Diaolog_Form
namespace DialogExample
{
public class Dialog_Form:DialogFragment
{
private ListView myListView;
private List<string> myList=new List<string> ();
public Dialog_Form ()
{
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
myList.Add ("Jason");
myList.Add ("Kenny");
myList.Add ("Tedd");
var view = inflater.Inflate (Resource.Layout.dialog_form, container, false);
myListView = view.FindViewById<ListView> (Resource.Id.myListView);
ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleListItem1, myList);
myListView.Adapter = adapter;
return view;
}
public override void OnActivityCreated (Bundle savedInstanceState)
{
Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
base.OnActivityCreated (savedInstanceState);
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here" />
</LinearLayout>
dialog_form.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/myListView" />
<Button
android:text="Project Download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/downloadButton" />
</LinearLayout>
This should work:
public class Dialog_Form:DialogFragment
{
private ListView myListView;
private List<string> myList=new List<string> ();
public Dialog_Form ()
{
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
myList.Add ("Jason");
myList.Add ("Kenny");
myList.Add ("Tedd");
var view = inflater.Inflate (Resource.Layout.dialog_form, container, false);
myListView = view.FindViewById<ListView> (Resource.Id.myListView);
ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleListItem1, myList);
myListView.Adapter = adapter;
// Dialog.Window.SetGravity (GravityFlags.CenterHorizontal | GravityFlags.Top);
var layoutParam = Dialog.Window.Attributes;
layoutParam.Width = ViewGroup.LayoutParams.MatchParent;
layoutParam.X = 80;
layoutParam.Y = 200;
Dialog.Window.Attributes = layoutParam;
return view;
}
public override void OnActivityCreated (Bundle savedInstanceState)
{
Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
base.OnActivityCreated (savedInstanceState);
}
}
Specify the x & y position by setting them in the oncreateview. The x & y position should be calculated by the x & y position from your button + the height & width so it will show it above the button.
Related
In menu/ Genre.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_48dp"
android:title="Home" />
<item
android:id="#+id/nav_genre"
android:icon="#drawable/ic_toc_black_48dp"
android:title="Genres" />
<item
android:id="#+id/nav_download"
android:icon="#drawable/ic_get_app_black_48dp"
android:title="Download" />
</group>
<item android:title="Account">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_lock_open_black_48dp"
android:title="About"/>
<item
android:id="#+id/nav_signout"
android:icon="#drawable/ic_perm_identity_black_48dp"
android:title="Sign out"/>
</group>
</menu>
</item>
</menu>
In layout/GenreFragment.axml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lst_genre"
android:layout_height="match_parent"
android:layout_width="match_parent" />
In viewModel/ GenreFragmentVM:
public class GenerFragmentVM : Fragment
{
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Create your fragment here
}
ListView lst;
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
lst.SetAdapter(new ArrayAdapter<string> (this,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
//Event click on listview
lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {
};
}
}
Error: (19,19): Error CS1502: The best overloaded method match for
`Android.Widget.ArrayAdapter.ArrayAdapter(Android.Content.Context,
int, int)' has some invalid arguments (CS1502) (SoundCloud)
(45,45): Error CS1503: Argument #1' cannot convert
SoundCloud.GenerFragment' expression to type
`Android.Content.Context' (CS1503) (SoundCloud)
In Mainactivity.cs
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Set our view from the "main" layout resource
//ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView (Resource.Layout.Main);
var mToolbar= FindViewById<Toolbar> (Resource.Id.toolbar);
//Toolbar will now take on default action bar chacracteritics
SetActionBar(mToolbar);
ActionBar.Title = "home";
//Enable suppport action bar to display hamburger
//ActionBar.SetHomeAsUpIndicator(Resource.Drawable.icon_hambuger);
//ActionBar.SetDisplayHomeAsUpEnabled (true);
//Set menu hambuger
ActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu_white_24dp);
ActionBar.SetDisplayHomeAsUpEnabled (true);
drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);
//Event click on navigatonView
//
CreateFragments();
LoadInditialFragment ();
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
}
void NavigationView_NavigationItemSelected (object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
e.MenuItem.SetChecked (true);
if (e.MenuItem.ItemId != currentFragmentId) {
SwitchFragment (e.MenuItem.ItemId);
}
drawerLayout.CloseDrawers ();
}
// Khoi tao gia tri cho fragment layout
private void CreateFragments()
{
explorFragment= new ExploreFragment();
genreFragment = new GenerFragment ();
}
//
private void LoadInditialFragment ()
{
var transaction = FragmentManager.BeginTransaction ();
transaction.Add (Resource.Id.fragmentContainer, explorFragment).Hide (explorFragment);
transaction.Add (Resource.Id.fragmentContainer, genreFragment);
transaction.Commit ();
}
private void SwitchFragment(int FragmentId)
{
var transaction = FragmentManager.BeginTransaction ();
switch (currentFragmentId) {
case Resource.Id.nav_home:
transaction.Hide (explorFragment).Commit ();
break;
case Resource.Id.nav_genre:
transaction.Hide (genreFragment).Commit ();
break;
}
transaction = FragmentManager.BeginTransaction ();
switch (FragmentId) {
case Resource.Id.nav_home:
transaction.Show (explorFragment);
transaction.Commit ();
break;
case Resource.Id.nav_genre:
transaction.Show (genreFragment);
transaction.Commit ();
break;
}
currentFragmentId = FragmentId;
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- your content layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:titleTextColor="#android:color/background_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/navmenu" />
</android.support.v4.widget.DrawerLayout>
Reason for your first Build Error is as explained by SushiHangover, i.e You should be passing an instance of the parent Activity (activity which hosts this fragment) as the first parameter to ArrayAdapter Constructor.
Reason for your run-time (Object reference not set to an instance of an object) Exception is because "Activity" property is null. "Activity" property will have a value only when the Fragment attachment to parent Activity is complete. So essentially, you should not be accessing Activity property from OnCreateView Method. You can initialize your listview either in OnAttach or OnCreate callbacks. I am modifying your code accordingly.
`public class GenerFragmentVM : Fragment
{
ListView lst;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
//Event click on listview
lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {
};
return view;
}
}
Passing the Activity of your Fragment as the first arg of the Widget's ArrayAdapter
Added a return of your new view;
Code:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
//Event click on listview
lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {
};
return view;
}
Here is my code:
DetailsFragment fragment = new DetailsFragment();
Transition changeTransform = TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform);
Transition explodeTransform = TransitionInflater.from(getActivity()).
inflateTransition(android.R.transition.explode);
setSharedElementReturnTransition(changeTransform);
setExitTransition(explodeTransform);
// Setup enter transition on second fragment
fragment.setSharedElementEnterTransition(changeTransform);
fragment.setEnterTransition(explodeTransform);
FragmentManager manager = ((Activity)mContext).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layoutView, fragment).addToBackStack(fragment.getClass().getSimpleName())
.addSharedElement(view.findViewById(R.id.itemView), "view");
fragment.commit();
In DetailsFragment giving the same transition name("view" as Assign a Common Transition Name) in Layout for ImageView but I not able to see any transition on Android Lollipop.
I have already declared the manifests file style for (Enable Window Content Transitions) -
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowContentTransitions">true</item>
</style>
I have searched a lot but didn't get answer.
First need to Create
transition directory under project res directory.
Create change_image_trans.xml under transition directory.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeTransform/>
<changeBounds/>
</transitionSet>
Activity like this -
public class DemoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
ListFragment listFragment = new ListFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, listFragment)
.commit();
}
}
My Activity fragment like this -
activity_fragment.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">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
here is my fragment to open -
ListFragment.java
public class ListFragment extends Fragment implements AbsListView.OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
String [] strings = {"First Element", "Second Element", "Third Element"};
MyListAdapter myListAdapter = new MyListAdapter(getActivity(), R.layout.list_item, strings);
listView.setAdapter(myListAdapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String imageTransitionName = "";
String textTransitionName = "";
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
ImageView staticImage = (ImageView) getView().findViewById(R.id.imageView);
LastFragment lastFragment = new LastFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementReturnTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
setExitTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
lastFragment.setSharedElementEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
lastFragment.setEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
imageTransitionName = imageView.getTransitionName();
textTransitionName = textView.getTransitionName();
}
Bundle bundle = new Bundle();
bundle.putString("TRANS_NAME", imageTransitionName);
bundle.putString("ACTION", textView.getText().toString());
bundle.putString("TRANS_TEXT", textTransitionName);
bundle.putParcelable("IMAGE", ((BitmapDrawable) imageView.getDrawable()).getBitmap());
lastFragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, lastFragment)
.addToBackStack("Payment")
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.addSharedElement(staticImage, getString(R.string.fragment_image_trans))
.commit();
}
}
//MyListAdapter for creating list item
class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item, null);
}
String listItem = getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(listItem);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.logo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setTransitionName("trans_text" + position);
imageView.setTransitionName("trans_image" + position);
}
return view;
}
}
fragment_list.xml
<?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:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="#drawable/logo"
android:transitionName="#string/fragment_image_trans"/>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/imageView">
</ListView>
</RelativeLayout>
LastFragment.java
public class LastFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
String actionTitle = "DUMMY ACTION";
Bitmap imageBitmap = null;
String transText = "";
String transitionName = "";
if (bundle != null) {
transitionName = bundle.getString("TRANS_NAME");
actionTitle = bundle.getString("ACTION");
imageBitmap = bundle.getParcelable("IMAGE");
transText = bundle.getString("TRANS_TEXT");
}
getActivity().setTitle(actionTitle);
View view = inflater.inflate(R.layout.fragment_last, container, false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.findViewById(R.id.listImage).setTransitionName(transitionName);
view.findViewById(R.id.textView).setTransitionName(transText);
}
((ImageView) view.findViewById(R.id.listImage)).setImageBitmap(imageBitmap);
((TextView) view.findViewById(R.id.textView)).setText(actionTitle);
return view;
}
}
R.layout.fragment_last
<?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:padding="24dp"
android:orientation="vertical">
<ImageView
android:id="#+id/listImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#drawable/logo"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Sample"
android:textSize="24sp"/>
<ImageView
android:id="#+id/otherImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="24dp"
android:src="#drawable/logo"
android:transitionName="#string/fragment_image_trans"/>
</RelativeLayout>
I am using the following code for using a listview in a fragment. I don't know why it is not working?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
SharedPreferences allcontactssharedpreferences = getActivity().getSharedPreferences("ALL_LIST_FOR_CONTROL", Context.MODE_PRIVATE);
Map<String,?> keys = allcontactssharedpreferences.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Toast.makeText(getActivity(),entry.getKey(),Toast.LENGTH_LONG).show();
if ( entry.getKey() != null)
AllContacts.add(entry.getKey());
}
listView = (ListView)view.findViewById(R.id.theListOfHistoryControl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,AllContacts);
listView.setAdapter(adapter);
return view;
}
I tried the follwing solving, but it didn't work either: List View in Fragment not working
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
SharedPreferences allcontactssharedpreferences = getActivity().getSharedPreferences("ALL_LIST_FOR_CONTROL", Context.MODE_PRIVATE);
Map<String,?> keys = allcontactssharedpreferences.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Toast.makeText(getActivity(),entry.getKey(),Toast.LENGTH_LONG).show();
if ( entry.getKey() != null)
AllContacts.add(entry.getKey());
}
// listView = (ListView)view.findViewById(R.id.theListOfHistoryControl);
//
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,AllContacts);
//
// listView.setAdapter(adapter);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
listView = (ListView)view.findViewById(R.id.theListOfHistoryControl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,AllContacts);
listView.setAdapter(adapter);
}
Please help me.
Edit 1:fragment_home.xml is:
<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"
android:orientation="vertical"
tools:context="com.example.shiza.callhistorycontrol.Home">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/theListOfHistoryControl" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/theListOfHistoryControl">
</ListView>
</LinearLayout>
I have a ListView in my fist screen that shows some information. When I click on each item, I have another listView with new information and textview in the same page, I want to show the pervious list view row in text view,
would you please let me know how can I implement this,
Thanks in advance!
here is my xml file
<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="######">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone"
android:text="test" />
<TextView
android:id="#+id/list_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:text="#string/no_message"
android:textColor="#000000"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:gravity="center"></TextView>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
Here is my java file that shows the listview and
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView)view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getActivity().getActionBar().setTitle(R.string.title_forms);
TextView details = (TextView)view.findViewById(R.id.detail_header);
details.setVisibility(View.VISIBLE);
return view;
}
Here is one of my fragment that has onItemClick Listener
public class ListFragment extends Fragment implements AdapterView.OnItemClickListener {
public static final String TAG = "ListFragment";
private ListView listView;
private ArrayList<String> testIds;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
tripIds = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
registerForContextMenu(listView);
return view;
}
Here is my onItemClick :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
chooseTest(testIds.get(position));
String selectedTestId = testIds.get(position);
Helpers.saveToSharedPreferences(getActivity(),
Constants_Prefs.SELECTED_TOP_LEVEL_RECORD,
Constants_Keys.SELECTED_TEST_ID,
selectedTestId);
I don't know how can I get that information
FormTypeListFragment passDataTo = new FormTypeListFragment();
Bundle bundle = new Bundle();
bundle.putString("DATA", selectedTestId);
passDataTo.setArguments(bundle);
}
Before launching your fragment to display selected item use setArguments() method to add key value pair of string in your case, Then in your fragment on createView use fragments method getArguments() to retain your passed string
Pass the value in bundle as :
Set :
YourFragment fragmnt = new YourFragment ();
Bundle bundle = new Bundle();
bundle.putString("item","name");
fragmnt.setArgument(bundle);
Get :
Then in your Fragment, retrieve the data (e.g. in onCreate()) with:
Bundle bundle = this.getArguments();
String myValue = bundle.getInt("item", "");
I want to show a Button and a PieChart below in a tab.
Thats the code I worked with: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/fragments/src/com/actionbarsherlock/sample/fragments/FragmentTabs.java
Instead of the CountingFragment etc. I implemented my own.
I'm not sure what to
My xml:
fragment_graph_categories.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_graph_categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_filter" />
</LinearLayout>
And that's my "Tab" attached to the TabManger
public class GraphCategories extends SherlockFragmentActivity {
GraphCategoriesFragment mCategoriesFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_graph_categories);
if (savedInstanceState == null) {
// Do first time initialization -- add initial fragment.
mCategoriesFragment = GraphCategoriesFragment.newInstance(1);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_graph_categories, mCategoriesFragment).commit();
} else {
//mStackLevel = savedInstanceState.getInt("level");
}
}
#Override
protected void onResume() {
if(mCategoriesFragment != null) {
//mCategoriesFragment.onResumeRedraw();
}
}
//notice Inner class
public static class GraphCategoriesFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//View mChartView = inflater.inflate(R.layout.hello_world, container, false);
View rlayout = inflater.inflate(R.layout.fragment_graph_categories, container, false);
//? rlayout = new LinearLayout(getActivity());
return rlayout;
}
}
}
But im not sure what to inflate at the fragments onCreateView or if I need a FrameLayout in my xml?
I tried some options but don't figured it out.