TabHost inside Fragment - java

I'm trying to create a tabHost inside a Fragment, so I can create a tabs inside that fragment I've tried changing the extends to android.support.v4.app.Fragment but then the complete app won't build. This issue I have is the following:
wrong 2nd argument type. found'android.app.fragment' required 'android.support.v4.app.fragment'
This is the code I wrote:
public class TimeTableFragment extends Fragment {
FragmentNameFactory nameFactory = new FragmentNameFactory();
String body;
String major;
String status;
private FragmentTabHost mTabHost;
public TimeTableFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStatus){
((MainActivity) getActivity()).setFragmentName(nameFactory.getNameNL(7));
View view = inflater.inflate(R.layout.ftimetable, container, false);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"));
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"));
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"));
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstance){
super.onViewCreated(view, savedInstance);
}
#Override
public void onActivityCreated(Bundle savedInstance){
super.onActivityCreated(savedInstance);
}}
The .xml (ftimetables):
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent">
</FrameLayout>
</LinearLayout>
</TabHost>
I hope someone can help me fix this issue!

change your TimeTableFragment import code
import android.app.fragment(x)
import android.support.v4.app.fragment(o)

Related

Android RecyclerView Adapter in a fragment not working

I am trying to use a androidx.recyclerview.widget.RecyclerView inside a Fragment and having a tough time trying to add basic items to it via an adapter. Following is my Fragment:
public class TimelineFragment extends Fragment {
private DataBoundAdapter timelineAdapter;
private View rootView;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_timeline_home, container, false);
initStuff(container);
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void initStuff(ViewGroup container) {
ArrayList<FernPost> list = {...};
timelineAdapter = new DataBoundAdapter(list);
RecyclerView recyclerView = rootView.findViewById(R.id.scroll_timeline);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
recyclerView.setAdapter(timelineAdapter);
timelineAdapter.notifyDataSetChanged();
}
}
This is the fragment 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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:text="hi there bullshit!!!"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll_timeline"
android:scrollbars="vertical"
android:visibility="visible">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
The Main activity layout xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_input_add" />
<fragment
android:id="#+id/timeline_fragment"
android:name="com.blinkfast.fern.TimelineFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
here is the adapter code for the recycler view:
public class DataBoundAdapter extends RecyclerView.Adapter<DataBoundViewHolder> {
private List<FernPost> fernPosts;
public DataBoundAdapter(List<FernPost> fernPosts) {
this.fernPosts = fernPosts;
}
#NonNull
#Override
public DataBoundViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fernpost, null);
return new DataBoundViewHolder(itemLayoutView);
}
#Override
public void onBindViewHolder(#NonNull DataBoundViewHolder holder, int position) {
holder.bind(fernPosts.get(position));
}
#Override
public int getItemCount() {
return fernPosts.size()
}
}
And the view holder:
public class DataBoundViewHolder extends RecyclerView.ViewHolder {
private FernPost post;
private View fernPostView;
public DataBoundViewHolder(#NonNull View itemView) {
super(itemView);
this.fernPostView = itemView;
Log.i(DataBoundViewHolder.class.getName(), "");
}
public void bind(FernPost fernPost){
Log.i(DataBoundViewHolder.class.getName(), "binding");
this.post = fernPost;
}
}
I have spent entire day trying various thing making it work, but nothing has so far. The onCreateViewHolder / onBindViewHolder methods are just not getting called. Not sure what i am doing wrong.
Assuming your fragment (the one holding RecyclerView) is displayed and your list has items before passed to DataBoundAdapter, please use different inflation inside your adapter's onCreateViewHolder:
LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false)

I am trying open a Fragment From onOptionsItemSelected Method

I am trying to call a fragment from an activity. But unable. This is what I tried.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId()==R.id.action_search){
SearchFragment searchFragment=new SearchFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container,searchFragment,null).commit();
}
return true;
}
And My main_activity.xml
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
tools:context=".Controllers.MainActivity">
After trying the app does crash but its also not opening the fragment which I want.
The fragment xml
<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"
android:id="#+id/searchFragment"
tools:context=".SearchFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewSearchFrag"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
And Fragment java is
public class SearchFragment extends Fragment {
private RecyclerView mRecyclerViewSearch;
private RecyclerViewAdpater mAdpater;
public SearchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_search, container, false);
mRecyclerViewSearch=view.findViewById(R.id.recyclerViewSearchFrag);
mRecyclerViewSearch.setLayoutManager(new LinearLayoutManager(getContext()));
mAdpater=new RecyclerViewAdpater(DataSevices.mChineseColors,getActivity());
return view;
}
}
Please correct me if I am doing anything wrong
You can use Framlayout instead of Linear-layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
if you want some other components in your MainActivity , create a fragment for that.
and replace it.
in activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
in your fragment 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=".SearchFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewSearchFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
create new fragment fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
</LinearLayout>
and MainFragment.java
public class MainFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
//...
return view;
}
}

How should I correctly put some Fragment to FrameLayout in mainActivity?

I'm trying to put fragment in MainActivity's FrameLayout by clicking the button in MainActivity. It almost works, but the content of MainActivity's FrameLayout doesn't get replaced by the Fragment content, but added to it. So the question is: how to make it correct?
My MainActivity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.pushMeBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager f_m = getSupportFragmentManager();
FragmentTransaction f_t = f_m.beginTransaction();
f_t.replace(R.id.container_layout, new Fragment1()).addToBackStack(null).commit();
}
});
}
}
My activity_main.xml
<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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pushMeBtn"
android:text="push me"
android:layout_gravity="center"/>
</FrameLayout>
</RelativeLayout>
My Fragment1 class
public class Fragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container,false);
}
}
My fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="339dp"
android:gravity="center"
android:text="fragment1"
android:textSize="25sp"/>
</RelativeLayout>

ArrayAdapter additions not showing in ListView

I have a Android fragment containing a ListView, and I have attached an ArrayAdapter to the ListView, but when I insert items into the ArrayAdapter, the ListView only shows the first item.
onCreateView iterates over my Technologies, if they're unlocked, it adds them to the ArrayList shownTechs, but only the first item ever appears
public class TabTechnologyList extends Fragment {
private static final String TAB_NAME = "tab_name";
private ListView techList;
private ArrayList<Technology> shownTechs = new ArrayList<>();
ArrayAdapter<Technology> techListAdapter;
public TabTechnologyList() { }
public static TabTechnologyList newInstance() {
TabTechnologyList fragment = new TabTechnologyList();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabData = Globals.getTabData();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
techList = (ListView) view.findViewById(R.id.tabTechListList);
techListAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.layout_techlist_element, shownTechs);
techList.setAdapter(techListAdapter);
for(Technology tech: tabData.getTechnologies()) {
shownTechs.add(tech);
}
//Only the first one is shown on the screen
return view;
}
}
}
When I google for similar issues, people seem to be recreating the ArrayAdapter instead of editing the existing one, but I'm not doing that, and cant' figure out what problem remains. Any suggestions?
fragment_technology_list.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.tbohne.kittens.front.TabTechnologyList"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabTechListList" />
</RelativeLayout>
layout_techlist_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
Addition
I have just realized that this fragment is not filling the parent height, and is only tall enough to show the one entry, as if the fragment height were wrap_content. (I can't scroll it, so it appears as if there's only the one). This makes me think the nesting of fragments may be related, so here's the code that creates the fragment:
public class TabTechnology extends Fragment {
private TabTechnologyList table;
private OnFragmentInteractionListener mListener;
public TabTechnology() {}
public static TabTechnology newInstance(String tabName) {[SNIP]}
#Override public static onAttach(Activity activity) {[SNIP]}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_technology, container, false);
table = TabTechnologyList.newInstance(tabName);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.view_table, table);
transaction.commit();
return view;
}
}
fragment_technology.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"
tools:context="com.tbohne.kittens.front.TabTechnology"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_table"
android:layout_above="#+id/view_details" />
<View android:layout_width="match_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_details"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true" />
</LinearLayout>
Putting the Fragment inside the ScrollView was the source of the problem. Simply replaced those with FrameLayout, and suddenly everything magically works. Very strange.
I suspect the problem has something to do with nesting scrollable objects.

ListFragment: Runtime error "Your content must have a ListView whose id attribute is 'android.R.list'

I have a listfragment class:
public class activityfeedScreen extends ListFragment implements OnItemClickListener {
private ListView activityfeed_feedList_listView;
private ActivityFeedBaseAdapter adapter;
public static final activityfeedScreen newInstance()
{
activityfeedScreen fragment = new activityfeedScreen();
Bundle bdl = new Bundle(1);
fragment.setArguments(bdl);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onItemClick(AdapterView<?> activityfeedlistview, View view, int position, long itemID) {
Intent intent_activityfeed_showitemdescription = new Intent(view.getContext(), activityfeedItemScreen.class);
startActivity(intent_activityfeed_showitemdescription);
}
}
In this class I am getting an error at the line:
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
I initially thought it was a problem due to the getView() being null so I moved that from the onCreate method to the onCreateView() method. But I am getting the runtime error as stated in the title.
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#E5E5E5"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/activityfeed_profilePicture_imageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/default_profile"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B9B9B9"
android:layout_marginTop="20dp">
<ListView
android:id="#+id/activityfeed_feedList_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
I understand that I must change the listview id to
android:id="#android:id/list"
but I am not sure then how to call that in my onCreateView method.
Thanks for all your help!
Yes. First of all you do have to change the ID of your ListView like so:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
In your onCreateView you can reference the ID by android.R.id.list. This is because Android built-in resource reside in android.R. You can also find there drawables, styles, etc. (you can call them in a similar maneer).
Taking the above into consideration, your onCreateView method should look like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(android.R.id.list);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
That's all ;)
In onCreateView() you need to provide a layout that has a ListView with and id of #android:id/list.
ListFragment provides some helper methods (e.g setListAdapter()) that require the layout to have a ListView with that id so it can reference it and set the adapter for you.

Categories