I am implementing an app which has a gridview of images in one activity and one fragment for each image which contains the image in full screen. When i click on any of the images in the grid, it should open up the corresponding fragment. However, we cannot use intent to do this.
here is my code
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(position==0)
{
Intent i=new Intent(Gallery.this,ImageFrag1.class);
startActivity(i);
}
and the fragment is
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ImageFrag1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.imagefrag1, container, false);
}
}
This fragment is bound to an activity ImagesSwipe. SO how do i achieve the transition between grid view item and its corresponding fragment.
Thanks
You don't need one Fragment for one Image. Just reuse one Fragment with an ImageView in it's layout for every Image.
Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for. Think about them as a reusable UI-Modul for an Activity. To add a Fragment to an Activity you have to use the FragmentManager and the FragmentTransaction classes, which provide all interactions with Fragments.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(YourFragment.newInstance(), null);
ft.commit();
Have a look at this guide from the Google Docs where the basic things about GridViews are described. In Addition you should read about Fragments. And here is a Tutorial about your approach.
You may want to check out DialogFrament, here is an example.
Instead of using intent you use FramentManager:
if(position==0)
{
FragmentManager fm = getFragmentManager();
ImageFrag1 imageDialog = new ImageFrag1()
ImageFrag1.show(fm, "image_title");
}
And your dialogFrament becomes:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ImageFrag1 extends DialogFragment {
public ImageFrag1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
}
Related
I am developing an Android app that has a little bit confusing navigation structure which leads me to my problem. There are two ways to navigate through the app. First is the BottomNav and second is a TabMenu. I thought about working with fragments that replace each other, so I came up with the following structure:
1. BottomNav#1
- TabMenu#1
- Tabmenu#2
- TabMenu#3
- ...
2. BottomNav#2
- TabMenu#4
- Tabmenu#5
- TabMenu#6
- ...
... and so on.
The problem I am facing is that when I navigate from BottomNav#1 to BottomNav#2 and back again there is a blank screen that doesn't show the content of the actual fragment:
When I open the app
After clicking on BottomNav#2 and then back to BottomNav#1 the fragment seems to be blank
My guess is that I somehow have a problem with my fragmentTransaction.replace(); as it seems like the fragment doesn't get loaded again? I am kind of new to this and really tried to find an answer online but this is a bit specific why I guess I didn't find anything.
This is my MainActivity:
package com.example.XXXX;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private FrameLayout mMainFrame;
private AusweisFragment ausweisFragment;
private SpendenFragment spendenFragment;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ausweisFragment = new AusweisFragment();
spendenFragment = new SpendenFragment();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_ausweis:
setFragment(ausweisFragment);
return true;
case R.id.navigation_spenden:
setFragment(spendenFragment);
return true;
}
return false;
}
};
private void setFragment(Fragment fragment)
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
This should lead to e.g. the fragment called "ausweis" by clicking on on BottomNav#1 (switch case):
package com.example.XXXXXXX;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AusweisFragment extends Fragment {
private View rootView;
private AusweisPageAdapter ausweisPageAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
public AusweisFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return rootView;
}
}
After that I would like to call another fragment called "spenden" which is exactly the same as the fragment called "ausweis" but with different named tabs, so I think it is not bother you with more code.
edit: I missed about writing where the "content" I wrote about gets from. For a first try and proof that the fragments change, I hardcoded a phrase like "Ausweis" into the XML which is connected to my fragment java class.
Maybe one of you has an idea to that problem. I think it has something to do with my onCreateView in one of the fragments, but I have no close clue.
Hopefuly I didn't miss an important detail. I am very grateful for any kind of help. Thanks a lot in advance.
The answer to my problem was to use a child-parent relation between the different fragments. With the click on BottomNav#1 I am inflating an fragment which inflates a new fragment inside itself. That was the key problem. In my code I handled this as a second "normal" getFragmentManager();.
The answer is to use getChildFragmentManager(); for the nested fragment instead. Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout1 = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getChildFragmentManager(), tabLayout1.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
...
...
Hopefuly this will help some people when they face the same problem with nested fragments.
I want to display custom listview in navigation drawer's fragment page. However, in my fragment class, I'm getting an error which seems I cannot set a custom adapter.
package android_gcm_client.mynavigation;
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;
import android.widget.ListView;
import java.util.ArrayList;
public class List_Fragment extends Fragment {
View rootview;
ArrayList prgmName;
public static int [] prgmImages= {R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.custom_layout,container,false);
CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);
ListView listview=(ListView) getView().findViewById(R.id.listView);
listview.setAdapter(ca);
return rootview;
}
}
It seems the error happens in line below (custom adapter can not be applied).
CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);
In MainActivity I call the fragment as follows:
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment objFragment=null;
switch(position) {
case 0:
objFragment=new ListFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}
I tried different ways to display custom listview for navigation item selected method.
I tried to directly call activity instead of fragment but problem was navigation drawer not visible for all activity. So I tried to call CustomAdapter in activity as I'm doing in fragment.
I have struggle to solve this error. (Sorry for bad English).
You are using this in custom adapter. Usually adapters wants a Context in constructor call. But this constructor is called inside a fragment and this can't be used.
You can use getActivity() as Context inside a Fragment. But.
Sometimes it can return a null if it is called before onAttach() of this fragment.
CustomAdapter ca = new CustomAdapter(getActivity(), prgmNameList, prgmImages);
Also you can use Application context by creating static variable inside your Application class:
public class Application extends android.app.Application {
public static Context AppContext = null;
#Override
public void onCreate() {
super.onCreate();
AppContext = getApplicationContext();
// You can use this line to solve styling problems
// because Manifest android:theme is not working
AppContext.setTheme(R.style.AppTheme);
}
}
and create adapter like this:
CustomAdapter ca = new CustomAdapter(Application.AppContext, prgmNameList, prgmImages);
I'm currently creating an application that uses navigation drawer and fragments . In one fragment I want to create a tab layout which should be made in a fragment activity . My question is whether there are alternative ways that can be used to implement the tab layout inside fragment ?. Thank you in advance
Take a look at following code :
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment extends Fragment {
FragmentTabHost mTabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.content);
getActivity().getActionBar().setTitle("TabFragment");
mTabHost.addTab(
mTabHost.newTabSpec("First").setIndicator(
"First"), FirstFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Second").setIndicator("Second"),
SecondFragment.class, null);
return mTabHost;
}
#Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
mTabHost = null;
}
}
Here R.id.content is FrameLayout where you showing fragments.(Like a layout named content_frame having only FrameLayout with id content.)
You should be able to create a view with tab layout like in a normal activity.
Then create your class for the tab layout and extend FragmentActivity and implement TabListener. In this fragment you just inflate the tabview into your fragmentcontainer like you do with all fragments and you should be able to do everything you're used to do with a TabLayout.
This links would probably be useful for you :
Android Tab Layout inside Fragment
I have created my view how I want it to look. It has 1 images, an input box, and a button. I will want to load another activity when the button is clicked. I am confused why there are fragments and activities. I am new to the Android world (coming from iOS).
My understanding is that Activities are similar to ViewControllers, but I am not sure I understand what a fragment is.
Where do I put the event handling?
package com.phppointofsale.phppointofsale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class StoreUrlActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_url);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StoreUrlFragement()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.store_url, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class StoreUrlFragement extends Fragment {
public StoreUrlFragement() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_store_url,
container, false);
return rootView;
}
}
}
Firstly I would recommend reading this Fragments . Pay particular attention to the created fragment section, which includes the fragment life-cycle diagram. Second download and compile this Sample App,the effective navigation app will help you understand how different fragments work in tandem, and even implements a action bar.
To answer your question more or less a fragment can be thought of as a separate class. Once you call upon that particular fragment you can call functions from within that class.
Fragment Case-Switch
This is some sample code to show you what I mean.
public Fragment getItem(int i){
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LaunchpadSectionFragment();
case 1:
return new BluetoothClass();
default:
// The GPS section of the app .
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
In this case each fragment for me represented a class, which was implemented in a separate tab and each tab had a separate functionality. One of the key advantages of fragments is you can run separate activities without first letting one activity complete.
Furthermore each fragment is an extension of the java.lang.Object library. So it has all those functions + additional ones. I would read this as well. Lastly it would be a good idea to have separate xml files for each fragment then you can display that separately when a fragment is invoked.
Some more code
Each fragment will/could have this
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
// Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners
View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
});
}
public void onStart(){
super.onStart();
Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
}
public void onResume(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();
}
public void onStop(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
disableBT();
}
Remember these functions are from the fragment life-cycle I mentioned earlier.
Hopefully that gave you some idea on fragments. Also remember to read this as a lot of functionality uses the v7 app compat library. Including the fragment manager.
I have been trying to build a Master-Detail f low in android but wanted to change one of the detail fragments to a different fragment. As this is one of my first Android applications, I was just trying to make a picture appear on this new fragment. For this, I built the following two classes
1) Fragment class ( displays the picture to be displayed )
package com.userName.miscapp;
import com.userName.miscapp.dummy.DummyContent;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PictureFragment extends Fragment {
// Default Copy Constructor for the fragment
public PictureFragment() {
}
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_simple_picture, container, false);
return rootView;
}
}
2) Activity to display the same
package com.userName.miscapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class SimplePicture extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
PictureFragment frag = new PictureFragment();
frag.setArguments(arguments);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.simple_picture_container,frag).commit();
//setContentView(R.layout.activity_simple_picture);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_picture, menu);
return true;
}
}
On compilation, it does not recognize the PictureFragment to be an extension of Fragment class. This is inspite of it being clearly written in the first file. Searching for solutions on Stackoverflow said to extend FragmentActivity instead of Activity and trying to use getSupportFragmentManager() neither of which helped.
PS : Using 11 as the base for the current application.
Any help would be appreciated
Thanks
This is because you are using android.app.Fragment from new API in conjunction with android.support.v4.app.FragmentManager from support library, you should replace import in your PictureFragment from android.app.Fragment to android.support.v4.app.Fragment to make it work.