I am creating a fragment to display a map. I have done someting like this:-
<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=".MainActivity" >
<Spinner
android:id="#+id/spr_place_type"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/spr_place_type"
android:text="#string/str_btn_find" />
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/spr_place_type"
class="com.google.android.gms.maps.SupportMapFragment"
/>
But in the graphical layout, I can see the following error:-
When I right click and choose the fragment layout I further see the following error:-
how can I resolve the following error.
You cannot put your main activity layout as a fragment inside your main activity layout. That's what the "cyclic include" error means.
You don't seem to have a fragment layout readily available. You can click on the "New Layout..." button to create a new one.
Related
So There was a layout I wanted changed. So I added a linear layout to it and somehow it crashed code that wasn't even slightly related to it. So here Ill post some code.
Here is my main file and Ill point where the crash happens.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.layoutLogin);
gbtnSignUp = FindViewById<Button>(Resource.Id.btnSignUp);
gbtnSignIn = FindViewById<Button>(Resource.Id.btnSignIn);
gbtnSignUp.Click += gbtnSignUp_Click;
gbtnSignIn.Click += gbtnSignIn_Click; // here is where it crashes
}
Now my layout. Ill pinpoint which part I add adds the crash.
<?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:weightSum="100"
android:layout_height="fill_parent">
<ListView
android:layout_weight="80"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/ListViewHighlight"
android:id="#+id/companyListView" />
<ProgressBar
android:layout_weight="10"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/ListViewHighlight"
android:id="#+id/progressBar1" />
<LinearLayout// just from this linear layout existing it causes a crash
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:weightSum="100"
android:id="#+id/linearLayout1">
<TextView
android:text="Text"
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="match_parent"
android:id="#+id/textView1" />
<ImageButton
android:src="#drawable/defaultAdd"
android:layout_width="0dp"
android:layout_weight="90"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:id="#+id/imageButton1" />
</LinearLayout>
</LinearLayout>
This layout isnt even linked to the view that is causing the crash. Heck I've even removed all references to this view from within the project and it still crashed.
here is the error message forcing me to break at the line I specified.
System.NullReferenceException: Object reference not set to an instance of an object
Also I can comment this line out, yet itll make it able to run but ill still be able to press the button which then does some very bizzare behaviour, which I shouldn't be able to press it to begin with.
xml of login button
<Button
android:text="Sign In"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="0dp"
android:id="#+id/btnSignIn"
android:textSize="25sp"
android:background="#drawable/ButtonSignInStyle"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
Your layout does not contain buttons with the ids btnSignUp and btnSignIn. You need to add them or remove the 4 lines in your activity code.
Missing in your layout
<Button
android:id="#+id/btnSignUp"
.../>
<Button
android:id="#+id/btnSignIn"
.../>
Actually this problem was some messed up situation that happened when I exported my program in order to move it to another folder. I copied and pasted my files to my old version in its original folder and it worked perfectly. So if anyone has moved files in a not so great way it may cause unforeseen problems that don't make sense like this one.
I am trying to place 12 buttons in Grid View. Here is my layout XML file.
How could I use RelativeLayout to achieve this? I am new to Android programming.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bAries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aries"
android:drawableTop="#drawable/aries" />
<Button
android:id="#+id/bTauras"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tauras"
android:drawableTop="#drawable/tauras" />
<Button
android:id="#+id/bGemini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gemini"
android:drawableTop="#drawable/gemini" />
According to your question, I assume following are your requirements, hope they are aligned with what you really need:
12 Buttons to be seen as a Grid
how to use RelativeLayout?
Note:
For a simple thing like this, especially where you know you only need to have a definite number of elements(12 buttons) and that number is static, you don't really need to use a complex layout like GridView, where you must have to implement a ListAdapter to provide the dynamically adding buttons. So the most simplest solution you have is as you have also asked, use a RelaiveLayout as I have provided bellow.
I tried something like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.androxp.randika.main.MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/bAquarius"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:layout_marginLeft="5dp"
android:text="Aquarius"/>
<Button
android:id="#+id/bPisces"
android:layout_toRightOf="#+id/bAquarius"
android:layout_height="wrap_content"
android:layout_width="115dp"
android:layout_marginLeft="5dp"
android:layout_marginRight=""
android:text="Pisces"/>
<Button
android:id="#+id/bAries"
android:layout_toRightOf="#+id/bPisces"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:layout_marginRight="5dp"
android:text="Aries"/>
<Button
android:id="#+id/bTaurs"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Taurs"
android:layout_below="#+id/bAquarius"
android:layout_alignLeft="#+id/bAquarius"
android:layout_alignStart="#+id/bAquarius" />
<Button
android:id="#+id/bGemini"
android:layout_height="wrap_content"
android:layout_width="115dp"
android:text="Gemini"
android:layout_alignTop="#+id/bTaurs"
android:layout_alignRight="#+id/bPisces"
android:layout_alignEnd="#+id/bPisces" />
<Button
android:id="#+id/bCancer"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Cancer"
android:layout_below="#+id/bAries"
android:layout_alignRight="#+id/bAries"
android:layout_alignEnd="#+id/bAries" />
<Button
android:id="#+id/bLeo"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Leo"
android:layout_below="#+id/bTaurs"
android:layout_alignLeft="#+id/bTaurs"
android:layout_alignStart="#+id/bTaurs"/>
<Button
android:id="#+id/bVirgo"
android:layout_height="wrap_content"
android:layout_width="115dp"
android:text="Virgo"
android:layout_alignTop="#+id/bLeo"
android:layout_alignLeft="#+id/bGemini"
android:layout_alignStart="#+id/bGemini" />
<Button
android:id="#+id/bLibra"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Libra"
android:layout_below="#+id/bCancer"
android:layout_alignRight="#+id/bCancer"
android:layout_alignEnd="#+id/bCancer"/>
<Button
android:id="#+id/bScorpio"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Scorpio"
android:layout_below="#+id/bLeo"
android:layout_alignLeft="#+id/bLeo"
android:layout_alignStart="#+id/bLeo" />
<Button
android:id="#+id/bSagittarius"
android:layout_height="wrap_content"
android:layout_width="115dp"
android:text="Sagittarius"
android:layout_below="#+id/bVirgo"
android:layout_toLeftOf="#+id/bAries"
android:layout_toStartOf="#+id/bAries" />
<Button
android:id="#+id/bCapricorn"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:text="Capricorn"
android:layout_below="#+id/bLibra"
android:layout_alignRight="#+id/bLibra"
android:layout_alignEnd="#+id/bLibra"/>
</RelativeLayout>
Above layout may render out something similar to the following screen:
Clue:
However, I created this using Android Studio. If you are using Eclipse, I recommend you to start using Android Studio as you are just beginning Android App Development.
For Android RelativeLayouts, please read the following References:
Android official documentation for Relative Layout
An excellently matching Tutorial for your requirement
And you may find ton of tutorials for this purpose just by a single search of Google.
Word of Advice:
Whatever you go through to learn Android development, try to use up-to-date materials.
You should use GridView class for this. Here's an official doc and sample
I developed an app, that uses Fragments as part of a Fragment.
public class LoginFragment extends Fragment
EditTextWithCameraButtonFrag userNameFrag;
EditTextWithCameraButtonFrag passwordFrag;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
userNameFrag = (EditTextWithCameraButtonFrag)getChildFragmentManager()
.findFragmentById(R.id.fragment_username);
passwordFrag = (EditTextWithCameraButtonFrag) getChildFragmentManager()
.findFragmentById(R.id.fragment_password);
EditTextWithCameraButtonFrag is a subtype of Fragment.
This code works like a charm, running on android 5.1.1.
However, running it on Android 4.4.2 returns in userNameFrage and passwordFrag being null. So it seems, that the returned Child FragmentManager does not find the fields.
Is there any thing to consider when using getChildFragmentManager()with 4.4.3?
Thanks in advance!
EDIT:
This is the main fragment's XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/huge"
android:orientation="vertical" >
<TextView
android:id="#+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="#color/white"
android:textSize="40dp" />
<fragment
android:id="#+id/fragment_username"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/fragment_password"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/huge"
android:layout_marginStart="#dimen/huge"
android:layout_marginTop="#dimen/normal"
android:padding="#dimen/half"
android:text="Login" />
</LinearLayout>
And this the sub fragment's:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/editText_with_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/text_input_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.75"
android:background="#drawable/edit_text_login_top"
android:inputType="text"
android:padding="#dimen/normal" />
<Button
android:id="#+id/scan_text_view"
android:layout_width="wrap_content"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/text_input_field"
android:layout_alignTop="#+id/text_input_field"
android:layout_alignBottom="#+id/text_input_field"
/>
</RelativeLayout>
I just solved it- kinda.
After a LOT of reading it seems like it's possible to declare nested fragments via XML, but is not best practice.
So I decided to include FrameLayouts in the XML, and add the nested
Fragments via FragmentTransaction.
<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:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/huge"
android:orientation="vertical" >
<TextView
android:id="#+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="#color/white"
android:textSize="40dp" />
<FrameLayout
android:id="#+id/fragment_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/huge"
android:layout_marginStart="#dimen/huge"
android:layout_marginTop="#dimen/normal"
android:padding="#dimen/half"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
And in the main fragment's onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create userNameFrag
EditTextWithCameraButtonFrag userfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_username, userfragment).commit();
// create passwordFrag
EditTextWithCameraButtonFrag passfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_password, passfragment).commit();
}
This seems to work pretty good and- another plus- I also got rid of the the duplicate id bug.
From the code it is not clear where are these two fragments added:
1) Added in activity (in xml or in onCreate method)
Then this fragments are associated to fragemnt manager which belongs to Activity. You should use getActivity().getFragmentManager().find...
2) Fragemnts added in fragemnt xml layout
then they will be available after calling fragments onViewCreated method
Use getChildFragmentManager() for fragments which are defined in xml layout of this fragment or for fragments which are added by this getChildFragmentManager() fragment manager.
More specifically, I'm using SherlockListFragment inside a ViewPager and the following layout
(full source):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/filter_bar"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
...
/>
<ListView
android:id="#android:id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_above="#+id/filter_bar"
... />
<TextView
android:id="#android:id/empty"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_above="#+id/filter_bar"
android:text="..."
android:visibility="invisible"
.../>
</RelativeLayout>
When I open the application I see this:
That looks fine. Then I connect and open a filter, it's empty for now
Note that ListView gets resized just fine. Now I type in the filter to filter out everything:
Whoops! Action bar disappears from the display! However, if swipe back and forth through the ViewPager and do it again, everything becomes fine (and stays fine):
What could I do to make the empty view behave normally, just like ListView does?
set windowSoftInputMode to the activity declaration in AndroidManifest.xml . Something like this
<activity
android:name=".controll.MyActivity"
android:label="#string/title_activity_my"
android:windowSoftInputMode="adjustResize"/>
I am creating a layout in XML, and I have noticed this very strange occurrence. It has never happened before, but lately it's been getting on my nerves. I kind of want to abandon Eclipse and find a better IDE because of it.
Here is my layout:
<?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" >
<EditText
android:id="#+id/action_search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="kkm" >
</EditText>
<ListView
android:id="#+id/actions_list"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_above="#id/cancel_action_selection"
android:layout_below="#id/action_search_bar" >
</ListView>
<TextView
android:id="#+id/action_preview_pane"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_alignBottom="#id/actions_list"
android:layout_alignTop="#id/actions_list"
android:layout_toRightOf="#id/actions_list"
android:gravity="center_vertical|center_horizontal"
android:text="kk" />
<Button
android:id="#+id/cancel_action_selection"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:text="Cancel" />
<Button
android:id="#+id/define_new_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/cancel_action_selection"
android:text="NEW" />
<Button
android:id="#+id/select_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/define_new_action"
android:text="OK" />
</RelativeLayout>
Here is what I'm trying to achieve:
The problem is, if I try to set the ListView to be above the cancel button (via android:layout_above), it will say that it can't find the id for the cancel button.
Now, what I have noticed is that if I try and specify any ids for layouts BEFORE they are created with #+id, then I will get that strange error. What I mean by this is the following:
In the XML, apparently IDs have to be declared in order. Strange, no?
I would have to declare the cancel button's id first with #+id way before I define the ListView and set its layout_above attribute.
If I try to do what I have done here, it will say that it cannot find the id for the cancel button.
I have tried cleaning the project, ending adb.exe, refreshing, etc. and all it does is corrupt my project even more. R is not even generated afterwards. I don't want to have to go in a loop problem after problem because Eclipse can't handle things like this properly.
Does anyone have a solution for this? Or a better IDE?
http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
So you are placing listview relative to the button cancel. So you need to define button with a id. Position the listview relative to the button.
http://developer.android.com/guide/topics/ui/layout/linear.html
There is an example in the links posted both for linearlayout and relative layout have a look at it.
Edit:
If you have errors in your resource files R.java will not be generated. So make sure you do not have errors in your resource files and then clean and build the project.
To clarify the confusion. Note android:layout_above="#+id/button1"
<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=".OtherScreenActivity1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
>
</ListView>
</RelativeLayout>
Same as above
<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=".OtherScreenActivity1" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true" >
</ListView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
</RelativeLayout>
You have this
android:layout_above="#id/cancel_action_selection"
Use
android:layout_above="#+id/cancel_action_selection"
http://developer.android.com/guide/topics/ui/declaring-layout.html.
Look at the attribute id in the above link