sorry for the long post, i tried to make it as readable as possible.
I have a decent knowledge of JAVA and trying to understand Android programming. Today i have been working on my knowledge of fragments.
I have read this article and got the basics of working with them.I managed to make fragments and replace them when needed.
I proceeded by taking this project, and thought it might be fun to try and add a fragment to it with a basic tictactoe game i had build for practising purposes.
Now i am having problems with replacing the fragment. Everything works fine until I add the TicTacToe view in the mix, which has a tablelayout. The fragment is not getting replaced, rather is it adding an extra fragment (or so it seems) and uses this to switch fragments in.
BoardField is simply a class I made which extends Button
The layout thats not working for me is this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0fdf">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"
android:onClick="selected"
/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
</TableRow>
</TableLayout>
I omitted the other rows for readability, they are similar.
So my question is, why is replacing the fragments not working when I use TableLayout ??
EDIT:
Eric's solution worked, i hope to find out more about why?
Why parent view is TableLayout? Try to changing it to a RelativeLayout with a child, the TableLayout (just for test purpose)
With a RelativeLayout as parent view, are you able to "replace" instead of "add" the Fragment?
I think your mistake is not here. Perhaps your can look in code where you create new fragment or code where you replace fragment. There is no sense TableLayout make a problem to replace fragment. Eric is right RelativeLayout is better then TableLayout, but this is not the problem. Can you put more of your code?
Related
I saw that this statement used inside an xml layout will automate animations for you. I want to add this to my app, but when I put it in the layout I want
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/arrow_background"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:padding="10dp"
android:animateLayoutChanges="true"
android:visibility="visible"
android:id="#+id/format_help">
And then I change the visibility to gone
view.findViewById(R.id.format_help).setVisibility(View.GONE);
It only just instantly goes away. The information I saw about this was from a few years ago so does anyone know if this is still supposed to work or how to get it to work?
EDIT: Also I added this statement everywhere and it still does not working. If it matters this is inside a fragment/dialog
It works for views inside container which has the property android:animateLayoutChanges="true". If a Linear layout having the above property, whenever a new view added or removed from that container the effect is visible. The animation effect will not be visible to the container itself when container is added or removed. For more information check the below link.usage of animateLayoutChanges
I am trying to call a new activity from a fragment. I have a button at the toolbar, and when it is pressed the following code is being called (this event is inside the fragment):
Intent secondactitvity = new Intent(this.Activity,typeof(Activity_SecondActivity));
StartActivity(secondactitvity );
Now the new activity is being called and here is my code inside OnCreate of the new activity:
RequestWindowFeature(WindowFeatures.ActionBar);
ActionBar.SetIcon(Resource.Drawable.icon_toolbar_back_new);
ActionBar.SetCustomView(Resource.Layout.ActionBar_Custom_Layout);
ActionBar.SetDisplayShowCustomEnabled(true);
base.OnCreate(bundle);
SetContentView(Resource.Layout.SecondActivity_Layout);
Here is the SecondActivity_Layout:
<?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">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1" />
<fragment
class="frag.Fragment_Second"
android:id="#+id/fragment_secondActivity"
android:paddingTop="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is the layout for the fragment:
<?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">
<TextView
android:text="#string/secondFragment_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:paddingLeft="10dp" />
</LinearLayout>
And here is the fragment code:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Fragment_ProjectDetails, container); //view;// base.OnCreateView(inflater, container, savedInstanceState);
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Toast.MakeText(this.Activity, "activity for second fragment created",ToastLength.Short).Show();
base.OnActivityCreated(savedInstanceState);
}
Now the problem is. The second activity remains empty, it is not being filled with the fragment (but there is not an error message). I would understand it if the fragment cannot be found or something like that, but in this case all the functions from the fragment (onCreateView, onActivityCreated) are being called, and the Toast is also being displayed, but the activity is empty. Then I tried to insert a text view at the top of the fragment inside the second activity layout file, and its being shown but, the controls from the fragment remain un-visible. Then I tried to change the fragment associated to the second activity, I changed it to the fragment which is calling the activity, and this one is being properly loaded (I changed it inside the .axml file).
What am I missing here, for me it seems like the second one is not being created property and thus the controls cannot be displayed (but, I may also be wrong)?
Thank You
well after spending far too much hours on this i got the solution. First thing i have noticed is that i cannot acces Views from the second fragment layout (when trying to read them in code i could not compile). Then i created a new axml file, assigned it as the layout for the second fragment, and the controls are being displayed.
So for me it seems like a bug in Xamarin, somehow the first axml has not been registered accordingly (but funny thing is that i could access the axml per se, but not the controls).
I am somebody who is migrating from Java (Android development) to Xamarin, and i have put a lot of hope on it. But I must admit that after few weeks lots of the first enthusiasm is gone. There is a lot missing in Xamarin and as time is passing by I am more thinking about moving back to Java and start learning Objective C for iOS. This project which i am doing now in Xamarin, i would have done it already if I were using Java, but for now I am still in the early phase (of course it cannot be as fast as in Java because i have to google for everything, but ...). Thers is also the issue with the Resource file, sometimes when my project breaks I cannot stop it from executing and i have to kill Visual Studio via Taskbar. In most cases when I do this the Resource file gets "destroyed" and when I open the project again i cannot start it anymore (in most cases I didnt bother because I was practising and I created a new one, but what if it is a bigger project ?)
The next thing is the missing IntelliSense inside the .axml when the designer is open (when I open it with the xml viewer i get the IntelliSense). Do i have to switch always between these views ?
And now this thing with the fragment, how i am supposed to trust the system if such cruical things dont work right.
Since I am still in the trial period I will give this whole thing a chance, but as I sad, the "fun" working with it is decreasing ...
Sorry for the long post, but just had the need to say :)
I had the same problem and nothing I had done solved it, including removing the layout and adding it again even with different name.
I figured out that I had messed up the method and forgot the override keyword. After this fix, it worked just fine.
I am trying to use Chris Banes' library Actionbar-PullToRefresh. It can be found here.
I am using Tabs + ViewPager + Fragments in my app.
The problem I'm facing is that my fragment has a GridView and I cannot figure out how to use this library to work with it.
I read through the sample code. He says that all you have to do is, wrap your refreshable view in a PullToRefreshLayout like this:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Your content, here we're using a ScrollView -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ScrollView>
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
This works great for stuff like ListView, ScrollView, GridView, etc. However, apparently this will not work for Fragments (Tabs & ViewPagers). Now, in the sample code he has wrapped the refreshable fragment with a ScrollView INSTEAD of a PullToRefreshLayout.
I cannot do this because my Fragment 1 (under tab 1) has a GridView. Now I cannot add a GridView to a ScrollView because that just wouldn't make sense.
For example, if I put my GridView inside the ScrollView as shown below, it just doesn't make sense:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbarStyle="outsideInset" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF000" >
<!-- MY GRID VIEW -->
<GridView
...
/>
</RelativeLayout>
</ScrollView>
The above code works. Sort of. (I need to disable scrolling of my GridView and use an ExpandableGridView to get it working properly... which seems like overkill & I'm not even sure if that would work).
If I replace the ScrollView wrapper with anything else like PullToRefreshLayout or any other layout, the refreshing doesn't work.
What to do? How to get my layout to work with this library without wrapping a ScrollView around it?
I hope I was clear enough. Please let me know if you need any more info. I tried to explain it the best I could.
Thanks!
I had a similar issue trying to get it to work with a ListView that I had in one of my tabs.
I solved my issue by using the PullToRefreshAttacher instead of using the layout.
In your Activity that is controlling the ViewPager for the fragments, initialize a PullToRefreshAttacher in onCreate or an init method.
mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
Next make a public method that allows access to the attacher that you just initialized.
public PullToRefreshAttacher getPullToRefreshAttacher() {
return mPullToRefreshAttacher;
}
Then in the fragment you want the refresh functionality.
mPullToRefreshAttacher = ((MainTabActivity) getActivity())
.getPullToRefreshAttacher();
mPullToRefreshAttacher.addRefreshableView(activeListView, this);
Except in your case activeListView would be the reference to your GridView instead of a ListView.
Then make sure your fragment implements OnRefreshListener so you can handle the Refresh.
I have not actually tested this with a GridView so let me know if it works.
Good Luck!
Hi all and thank you in advance and sorry for my english. I have two big doubts
1 - I haven't been too much time programming in android, and i pretty sure there is a lot of things i have made in wrong ways. For example, I have made several apps where in xml definition i include another xml.
For example, imagine 2 activities with a header_section.xml being include in both activities xml definition. That header_section has 5 buttons and more views etc. Ok, in the xml is just make an include and it works......but to implement the buttons.....do i have to REPEAT the code in both activities?? it sounds really bad practice to duplicate code in both activities.....but how can i do, for example this in activities A and B? Do i have to put this code exactly the same in both activities classes????
private View header_section;
private Button bExample;
header_section=findViewById(R.id.header_section);
bExample=(Button)header_section.findViewById(R.id.bExample);
bExample.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Whatwever...call a number, for example
}
});
In main xml something like:
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp" >
<include android:id="#+id/header_section" android:layout_gravity="center" android:gravity="center" layout="#layout/header_section" />
</LinearLayout>
and in header_section.xml something like:
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp" >
<Button android:id="#+id/bExample" />
</LinearLayout>
2 - Imagine you have like 10 activities in your app. If in all of them there are a header section and bottom section with the same functionality, changing just the central area (showing different lists, views etc)......is better have just one activitie in ALL the app, with a viewflipper in the central area? or have 10 activities having, i do not know if can be avoided, asked in point 1, code repeated in all 10 activities for the implementation of the headers and bottoms views, handlers etc?
Thanks and best regards
1) Yes, usually, you should have to use them BUT you can make it simplier...
1.a) bExample=(Button)findViewById(R.id.bExample); //don't need to load the View
1.b) You can shorten a bit how you call the onclick, in your button/clicable element inside the LAYOUT, here's an example:
<!--inside layout -->
<Button android:id="#+id/bExample" android:onClick="aceptar" />
and
//inside the Activity
public void aceptar(View v){
//here the code of the button
}
For the question about implementing the same methods inside all Activities, check this post: Adding the same context menu to multiple activities
2) Depending the application
If you don't do much, you can load all in the same activity and HIDE/SHOW the layout elements you don't want.
But it's better to use different activities, anyway, if the layout is not "heavy" (too many elements/includes inside) you can load the SAME layout for all your activites, and you only need to change the different contents (strings) and/or HIDE/SHOW the different elements.
I have a LinearLayout with fixed view. I dynamically inject images in it (ImageViews) but I dunno in advance how many of them will be inserted. I'd like to have a layout where images wrap and go to a new line authomatically when they exceed the available width of the father (LinearLayout)
how do you recommend I should move?
thanks a lot
Define a Listview within the Linear Layout like below. Wrap your Linear Layout tags around it.
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
/>
Update this list view dynamically as you add images. This will solve your issues.
As I answered on a question asked a couple of hours before yours:
There is no layout manager in Android today that behaves like Swing's FlowLayout, where it wraps widgets onto multiple lines. It is theoretically possible for you to write one yourself, by looking at the open source code for LinearLayout, etc. and following the patterns they establish.
Sorry!