I have a search bar inside my 'main' layout, i call this layout by returning it with fragment acitivity after calling by my "MainActivity.java".
My question is, how can i add function for the searchbar so i will be able to search from my database? i can only display the layout and i don't know where do i need to put my function for searching data from my database.here is my code from my 'main.java'
public class Main extends Fragment{
public Main(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
return rootView;
}
}
Thanks a lot!
Can you try to extends ActionBarActivity and add the search as one the action item in the Actionbar , or toolbar in new V7 widget to take advantage of new material design ?
use the searchview widget, google recommend that you insert the search widget as an action view in the Action Bar
Related
Ok so i tried to learn of all These other questions here, but i am not getting the hang of it, so i decided to ask by myself.
I got a Main Activity with different Fragments for changing the views (used the Android Standard sidebar activity template). From there the user creates Tasks via a separate Acitivty which Returns the values. In the Main Activity the Tasks are getting stored into the SQLite-database. So far it works.
Now i have the Fragment TaskList with the corresponding layout TaskList_main.xml. In this XML file i got a simple ListView which i want to fill with the values from the database. My Problem is where to write that method and when to Access it. the method would be
public void showAllListEntries() {
List<TaskData> TaskList = datasource.getAllTasks();
//Daten werden im ArrayAdapter gespeichert
ArrayAdapter<TaskData> TaskListArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, TaskList);
ListView TaskDataListView = (ListView) findViewById(R.id.TaskListView);
TaskDataListView.setAdapter(TaskListArrayAdapter);
}
My Fragment is empty like this atm
public class TaskList extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
return view;
}
}
I can also send the main Activity if you like but it's a bit messy
So can someone tell me how i get this to work? And did i explain my Problem clearly?
You can do it in onCreateView. But you should make an async call to get the tasks to display and have your fragment as a listener. When you get the tasks you can create your adapter and attach it to the ListView.
And you should have a ProgressBar in TaskList_main.xml (which should be renamed to task_list_fragment.xml, I don't think there is a naming convention for layouts, but this is quite used) and hide it when you receive the data.
As the question reads, Fragments in android are View or ViewGroup. Can anyone explain
Here is the onCreateView method of Fragment from docs
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
The container parameter passed to onCreateView() is the parent
ViewGroup (from the activity's layout) in which your fragment layout
will be inserted
And
To draw a UI for your fragment, you must return a View from this
method that is the root of your fragment's layout.
You cannot define fragments as views or viewgroups.Fragments are more than that.The easiest way to visualize fragment is to think of them as chunks of an activity having own xml appearance,own behaviour with own life cycle callbacks.They always run on top of an activity allowing you to perform operations such as add,replace etc on them at run time.This way you can switch between your layouts effectively and efficiently.
To understand onCreateView method,consider the following explanation:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view =inflater.inflate(R.layout.example_fragment, container, false);
return view;
}
this returns a single View object, always a ViewGroup, with the set of View object that defines the Fragment's UI. The Activity calls this event handler when it is time for the Fragment to provide its UI for display.
Neither. Fragment is a base class.
From https://developer.android.com/guide/components/fragments.html
A Fragment represents a behavior or a portion of user interface in
an Activity
Fragment contains a field:
// The View generated for this fragment.
View mView;
that is generated in onCreateView which has the implementation:
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
Bundle savedInstanceState) {
return null;
}
So if you want a Fragment with a View then #Override that method. And then the Fragment can be shown to the user if you use the appropriate fragment transaction from an Activity or nested Fragment.
fragment is a wrapper over a custom view thats tied to host activity lifecycle.
Fragments are a kind of Activity that don't take full screen,it can just split the screen into many fragments,so it is well used for phone and tablet .
i think is no,activity(fragment) control what the view shows,viewGroup extends view and implements ViewManger.Only Activity loads related Views can the phone show the layout.Activity is a Group,view can work normally in this container
my name is Augusto, I'm developing a Messaging/Social app for Android on Android Studio and I implemented SlidingTabLayout (com.google.samples.apps.iosched.ui.widget) with 2 tabs (Chats and Friends ).
I wish to know how to access the views inside the Tabs' xml files on my activity.
I have:
MainActivity.class with activity_main.xml as contentview and the tabs's layouts(friendsview.xml and chatsview.xml) for the tabs.
I want to access tabs' layouts view (e.g: EditText, ImageView) from MainActivity.class, How do I "import" them to the activity?
Thanks!
SlidingTabLayout it's only a layout. To manage content use fragments with FragmentPagerAdapter for your tabs.
Here (link) is nice tutorial for you.
If you follow this example tutorial, you will have
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
and to access EditText and other widget inside tab, you need to have reference from view, so change above code to:
public class Tab1 extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.chatsview,container,false);
editText = v.findViewById(R.id.YOUR_EDIT_TEXT_ID);
return v;
}
}
I have two fragments that contains ViewPager. The first fragment that contain viewPager works very well. As I come to second fragment that contain ViewPager, I start to get FragmentStatePagerAdapter NullException. I knew what is the problem, I need to implement onDetach function to get rid this problem. I search around the internet, there is a lots of solutions. And I'm currently using API 19. So maybe this solution very suitable for me. But I don't know how to use the solution.
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
What is exactly mFragment1 and mFragment2 and R.id.main ?
Fragment 1
public class fragment1 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_restaurantprocess, container, false);
adapter = new RestaurantProcessPageAdapter(getActivity().getSupportFragmentManager());
...
}
}
Fragment 2
public class fragment2 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_feedpage, container, false);
adapter = new RestaurantFeedPageFragment(getActivity().getSupportFragmentManager());
...
}
}
I'm using slide animation to fragment
public void changeFragment_toRight(Fragment frag) /* Slide to right screen */ {
if (getSupportFragmentManager().findFragmentByTag("frag")!=null)
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.right_slide_in, R.anim.right_slide_out).replace(R.id.container, frag, "frag").commit();
else
getSupportFragmentManager().beginTransaction().add(R.id.container, frag, "frag").commit();
}
changeFragment_toRight(new fragment1());
changeFragment_toRight(new fragment2());
Source: Click here to view the source
It looks like the problem is that you are manually using a FragmentTransaction to change fragments. A ViewPager will do this automatically when you supply it with an adapter, so when you change fragments, you are trying to remove a fragment that has already been removed.
I have a dialog which extends DialogFragment:
public class MyCustomDialogFragment extends DialogFragment {
static MyCustomDialogFragment newInstance() {
return new MyCustomDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.institutional_info_custom_list, container, false);
return view;
}
}
And this is where I use this dialog (it is inside an onclick listener):
MyCustomDialogFragment newFragment = MyCustomDialogFragment.newInstance();
View DialogView = newFragment.getView();
DetailListView = (ListView) DialogView.findViewById(R.id.custom_dialog_list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(ComeHaInvestito.this, MasterAndDetailstatisticsInfoList);
DetailListView.setAdapter(adapter);
newFragment.show(getFragmentManager(), "master_detail_dialog");
MasterAndDetailstatitiscsInfoList is a list I am using with my custom ArrayAdapter, but the problem is with the View of the DialogFragment: it is null! Why? I read the android documentation and they say that getView() returns the View of the dialog which is the one returned by the onCreateView() overriden method.
So why I get a null pointer exception when trying to revocer the ListView which is in the dialogFragments layout???
Sorry but this does not make sense to me. An explanation will be appreciated.
Thanks!
Use setArguments() to provide the dataset for the Adapter to the DialogFragmet. In DialogFragment override onViewCreated(), retrieve the ListView, create the Adapter and set it to the ListView