I am trying to understand how to use fragments but they are not working for some reason. Can you please explain how to use them or at least tell me what is wrong with my code.
Main activity:
package test.testing;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import layout.BlankFragment;
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.main, fragment);
fragmentTransaction.commit();
}
}
Mainactivity 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="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"
android:orientation="vertical"
tools:context="test.testing.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
</LinearLayout>
BlankFragment:
package layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import test.testing.R;
/**
* A simple {#link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
BlankFragment XML:
<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:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingBottom="20dp"
android:background="#000000"
tools:context="layout.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/graycircle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:text="Event 1"/>
</LinearLayout>
</LinearLayout>
Thank you
In your mainactivity.xml
Change this :
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
to
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"/>
This will work because when you are using fragment tag then you need to set the class there in xml itself but when you are creating a fragment programatically like you are doing in this case, XML can have only a container in which fragment is inflated.
-
Update
Your code in MainActivity for adding fragment is incorrect. You are using support fragment hence you should use getSupportFragmentManager()
Make the following changes in your code.
Change in import statement of MainActivity.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
Change in code for adding fragment:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment done = new BlankFragment();
fragmentTransaction.add(R.id.main, done, "frag");
fragmentTransaction.commit();
I have modified your code in here.
http://pastebin.com/BEqExkbN
Related
I have this problem:
I'm trying to create an App with 2 buttons on a Layout and this buttons open 2 different Fragments, but the problem is that when I open the app it crashes.
Here's my code:
MainActivity:
package com.example.akroma.feina_final;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button boton1,boton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton1 = (Button) findViewById(R.id.button2);
boton1.setOnClickListener(this);
boton2 = (Button) findViewById(R.id.button);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Fragment fragment;
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.button:
fragment = new FragmentOne();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
case R.id.button2:
fragment = new FragmentTwo();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
}
}
}
I created the fragments standard (Blank).
Name of fragments:
FragmentOne
FragmentTwo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
tools:context="com.example.akroma.feina_final.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mapa" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Negocis" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Name of fragments on the tree:
FragmentOne
FragmentTwo
Edit: error: Error inflating class fragment
Solution to the inflating:
Thanks to: #svkaka
Change the fragment for:
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
New error:
must implement OnFragmentInteractionListener
Any ideas? I don't know what I'm doing bad.
Thanks in advance
Replace this
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
with this
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
U are mixing dynamic and static fragments
I want to add 2 fragments in a single layout like this:
I was able to do it using static fragment method but not able to do it dynamically. I want to target API 14 and above.
Here is the code:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/new_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/my_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
fragment1.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"
android:background="#00ff00"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment first"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
</LinearLayout>
fragment2.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"
android:background="#0000ff">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment second"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
</LinearLayout>
fragment1.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Rahul on 07-08-2017.
*/
public class fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1,container, false);
}
}
fragment2.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Rahul on 07-08-2017.
*/
public class fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2,container, false);
}
}
MainActivity.java:
package org.hinduismfacts.www.dynamicfragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Create new fragment and transaction
Fragment newFragment = new fragment1();
Fragment myFragment = new fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.add(R.id.new_placeholder, newFragment);
transaction.add(R.id.my_placeholder, myFragment);
transaction.commit();
}
}
In the output I am getting only first fragment displayed. The second fragment is not getting added. Output:
Can you please tell me what is going wrong?
please added android:layout_weight="1"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/new_placeholder"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:id="#+id/my_placeholder"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
>
</LinearLayout>
</LinearLayout>
You need layout weight android:layout_weight for both your frame layout
try below XML changes
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/new_placeholder"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/my_placeholder"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
The problem is in your activity_main.xml.
Use android:layout_weight="1" for each of your fragments container to show both of them on the screen.
Also fill_parent is deprecated. Consider using match_parent instead
Error inflating class fragment
I am trying to add fragment in activity at runtime dynamically.
After running the app crashes.
File names are
There is a main activity(MainActivity.java), its xml(activity_main.xml)
A fragment (MainFragment.java), its xml (fragment_main.xml)
Following is Code for app.
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="119dp"
android:layout_marginStart="119dp"
android:layout_marginTop="63dp"
android:text="Button" />
</RelativeLayout>
MainFragment.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
}
activity_main.xml
<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="com.example.vikashyadav.myquiz.MainActivity">
<fragment
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
MainActivity.java
package com.example.vikashyadav.myquiz;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new MainFragment());
ft.commit();
}
}
i am not able to find why this crashes.
Remove fragment layout by other like FrameLayout..
<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="com.example.vikashyadav.myquiz.MainActivity">
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addFragment(R.id.fragment_holder,new MainFragment(), "MainFragment");
}
protected void addFragment(#IdRes int containerViewId, #NonNull Fragment fragment,#NonNull String fragmentTag) {
getSupportFragmentManager()
.beginTransaction()
.add(containerViewId, fragment, fragmentTag)
.disallowAddToBackStack()
.commit();
}
}
I am simply writing code for transaction of fragment. But I am getting this error. I am new to android, I am unable to resolve this error.
However it was working with my old android studio. I think problem with my imports. Could you please help me? I don't think there is need of my fragment files. it's simply contaion onCreateView method only.
It's not replacing fragment.
Here my
MainActivity.java
package com.example.pkkeshar.tallymaster;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView transaction;
TextView summary;
TextView details;
FragmentManager fm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
transaction = (TextView) findViewById(R.id.transaction);
summary = (TextView) findViewById(R.id.details);
details = (TextView) findViewById(R.id.summary);
transaction.setOnClickListener(this);
summary.setOnClickListener(this);
details.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==findViewById(R.id.details)){
Fragment fragment = new details();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
if(v==findViewById(R.id.summary)){
Fragment fragment = new summary();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
if(v==findViewById(R.id.transaction)){
Fragment fragment = new transaction();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.linearLayout1, fragment);
transaction.commit();
}
}
}
Here activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.pkkeshar.tallymaster.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:paddingBottom="5dp"
android:id="#+id/linearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Transaction"
android:id="#+id/transaction"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Details"
android:id="#+id/details"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Summary"
android:id="#+id/summary"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout"
android:id="#+id/linearLayout1">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.pkkeshar.tallymaster.transaction"
android:id="#+id/fragment"
tools:layout="#layout/fragment_transaction" />
</LinearLayout>
I'm trying to implement the sliding animation to transit from fragment1 to fragment2.
I'm using setCustomAnimations method. And I understand I need to use the frame method in order to replace fragments.
My code:
package com.example.fragmentss;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity implements MyListFragment.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_rsslist_overview);
}
// if the wizard generated an onCreateOptionsMenu you can delete
// it, not needed for this tutorial
#Override
public void onRssItemSelected(String link) {
}
}
activity_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="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.fragmentss.MyListFragment" ></fragment>
<FrameLayout android:id="#+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
MyListFragment class
package com.example.fragmentss;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview,
container, false);
return view;
}
public void onClick2(View view) {
switch (view.getId()) {
case R.id.button2:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
DetailFragment newFragment = DetailFragment.newInstance();
ft.replace(R.id.details, newFragment, "mylistFragment");
// Start the animated transition.
ft.commit();
break;
}
}
} ~
fragment_rssitem_detail.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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto 2"
android:onClick="onClick1"/>
<TextView
android:id="#+id/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Frag 1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</LinearLayout>
fragment_rsslist_overview.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goto 1"
android:onClick="onClick2" />
<TextView
android:id="#+id/textView1"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:text="Frag 2"
android:textSize="30dip"/>
</LinearLayout>
I guess what you are trying to implement can very easily be implemented by a ViewPager
ViewPager is a custom component that was introduced to implement easy and smooth switching between fragments . Android Documentation has a very good and easy to understand example to understand it's use Take a look at http://developer.android.com/training/animation/screen-slide.html .You can also see similar questions that have been answered
Example/Tutorial for ViewPager and Fragments and
How to use Android ViewPager?