When I type R.layout. and press CTRL+SPACE, it shows me only activity_main layout, but I need fragment_settings layout that exist. And it doesn't see any of new layouts. How can I make this see this layout?
package com.example.navdrawer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class SettingsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout./*fragment_settings*/, container, false);
}
}
fragment_settings.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:background="#android:color/holo_red_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
My layouts
what it offers me
So, it's an android studio IDE bug, try to fix it by the following sequence:
Build -> Make Project
Build -> Rebuild Project
File -> Invalidate caches / Restart
Related
i know i can make textviews clickable so i can let the user call a number with an intent. but how do i achieve this when inside a fragment. i have multiple fragments and telephone numbers in my string.xml. every fragment has a telephone number, for example the code for one of my fragments:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.simmering_layout, container, false);
}
}
and here the xml for this class (fragment):
<FrameLayout 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="at.co.ccc.mondel.Simmering" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="#string/add_sim" />
<TextView
android:id="#+id/call_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:clickable="true"
android:text="#string/tel_sim" />
</FrameLayout>
please help me, thank you!
I am assuming you are having trouble finding the Text View in that case you can try the following way.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.simmering_layout, container, false);
// Here you can access the edit text like this
mTextView = (EditText) rootView.findViewById(R.id.call_sim);
return rootView;
}
}
I am hardly trying to create a simple application with a top menu and a changeable view below (by pressing the buttons in the menu fragment we change the view of the fragment below).
So, I have 2 fragments inside the main view but when trying to run the application in the emulator I get an error like:
Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors):
Trying to instantiate a class com.example.android.topmenu that is not a fragment
So, these are my XML layouts:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical" >
<fragment
android:id="#+id/menuFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.android.topmenu" >
</fragment>
<fragment
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.android.bottomcontent" >
</fragment>
</LinearLayout>
topmenu.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="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
bottom_content.xml
<?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:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+string/content_text" />
</LinearLayout>
and these are the classes for the main activity and the fragments
main_activity
package com.example.android;
import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class OLife extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// The activity is being created
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
super.onDestroy();
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
}
topmenu
package com.example.android;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMenu extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topmenu, container, false);
return view;
}
}
bottomcontent
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMain extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_content, container, false);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
You should be using FragmentActivity instead of Activity that is because you are using support Fragments and multiple Fragments in your activity main
Edit
You can now use the Appcompat support library and extends AppCompatActivity to support toolbar and fragment for lower api.
In my case it turned out, I was doing stuff in onCreate in the wrong order:
setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);
instead of
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);
As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.
I know its probably something simple I am missing here, but for some reason I can't seem to get my layout to display when I run my app using the emulator. Any feedback would be great. Thanks
my fragment
package com.pctoolman.planme.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class PlanMeMainFragment extends Fragment {
private Button mNewButton, mExistingButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.planme_main_fragment, container, false);
mNewButton = (Button)v.findViewById(R.id.new_event_button);
mNewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
getActivity().startActivity(myIntent);
}
});
return v;
}
}
my layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ff3a0b">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/plan_me"
android:textSize="36dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center|top"
android:textAlignment="center"
android:textColor="#2c58ff"
android:id="#+id/textView">
</TextView>
</LinearLayout>
<Button
android:id="#+id/new_event_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:padding="5dp"
android:text="#string/new_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
<Button
android:id="#+id/existing_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="#string/existing_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
</RelativeLayout>
here is the activity file
package com.pctoolman.planme.app;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class PlanMeMainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.planme_main_activity);
}
}
You should make a transatcion via FragmentManager to show your fragment in the activity. Suppose you have container (FrameLayout, for example) with id = fragment_container in your planme_main_activity.xml. If so, you should add the following after setContentView(R.layout.planme_main_activity);:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new PlanMeMainFragment())
.commit();
But if you declared fragment directly in XML, please post your planme_main_activity.xml
Maybe it's because you haven't positioned the buttons properly. It's easy to move things around in a relative layout. You don't need to use gravity. You can use:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btn"
android:layout_below="#+id/btn"
I'm assuming you cut out the closing tag for the RL.
I do not understand why I get this error only (for now) on aG alaxy Tab2 (Android 4.1.2 original).
On the Galaxy S3 (same version of Android) and PC simulation do not receive the error.
This is my code:
activity_dialogscore.xml (where I will put the two fragment):
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/LayoutPaperScore"
android:layout_gravity="center"
android:clickable="true"
android:orientation="vertical"
android:id="#+id/viewflipper_1">
<fragment
android:id="#+id/fragment_1"
android:name="com.didymond.baskstat.FragmentScore"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_score" />
<fragment
android:id="#+id/fragment_2"
android:name="com.didymond.baskstat.FragmentStatScore"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_statscore" />
</ViewFlipper>
FragmentScore.java
package com.didymond.baskstat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentScore extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_score, container, false);
}
}
FragmentStatScore.java
package com.didymond.baskstat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentStatScore extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_statscore, container, false);
}
}
And this is the main activity (only part of it), which uses the two fragment:
MainFragment.java
public class MainFragment extends FragmentActivity implements AlertDialogChangePlayer.OnDialogDismissListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogscore);
[other_code]
}
[other_code]
}
Why do I get the error only on the Galaxy Tab 2 and not in Galaxy S3 if running on same Android versions?
Any other pieces of code will be inserted if required ...
Do you have a layout in a res/layout-* folder? like res/layout-large?
If you have a separate layout for tablets, the error would only crop up on tablet devices.
Maybe I have found the error after trying again and carefully reading the error of LogCat.
The real problem is that I use style for setting button ecc...But folder for tablet missing ...For now I have these directory:
values
values-hdpi
values-large-mdpi
values-ldpi
values-mdpi
values-small-ldpi
values-v11
values-v14
values-xhdpi
values-xlarge-mdpi
To fix the problem I had to add the folder sw600dp-values.
Now... what other folders should be removed/added?
Ps: the layout folder is unique, in the various values dir there is the file style.xml which contains various sizes, etc....
I'm trying to get my head around fragments in order to have my apps prepped for ICS.
I have the following files to just get the most basic fragment app you can have. It should have this when launched: One Fragment Layout with a text view "Fragment 1" and next to it another Fragment Layout with "Fragment2".
My package name is com.mwerner.fragments
My files are:
FragmentsActivity.java
ExamplesFragment.java
ExamplesFragment2.java
examples_fragment.xml
examples_fragment2.xml
main.xml
The code for FragmentsActivity.java is:
package com.mwerner.fragments;
import android.app.Activity;
import android.os.Bundle;
public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
ExamplesFragment.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment, container, false);
}
}
ExamplesFragment2.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment2, container, false);
}
}
The examples_fragment.xml files just have a linear layout with a textview in it...
Here is the code for the main.xml:
<?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="vertical" >
<fragment
class="com.mwerner.fragments$ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments$ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
The app crashes on startup with the error
11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
Can you please tell me what is wrong here? I pretty much copied / pasted the code from the google developers page for fragments.
You defined the path to your fragments incorrectly in your layout xml. Correct the class attributes. Try this:
<?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="vertical" >
<fragment
class="com.mwerner.fragments.ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments.ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
Try extending FragmentActivity
public class FragmentsActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
When I was writing my first program on Fragments using I incurred same error. Instead of extending "Activity" extend "FragmentActivity" in your launcher activity.