Replacing multiple fragmentclass using one fragment view - java

i am having some trouble understanding and using fragments i have 3 buttons and 1 fragment views .using the buttons as Tabs i am trying to give the fragment a specific class when clicking on a button what i did is this :
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1" >
<Button
android:id="#+id/Tab1"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 1" android:background="#drawable/btn"/>
<Button
android:id="#+id/Tab2"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 2" android:onClick="switchToTab2" />
<Button
android:id="#+id/Tab3"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<fragment
android:id="#+id/fragment1"
android:name="com.example.newproject.fragmentclass"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
java class :
fragmentclass frt1;
fragmentclass2 frt2;
fragment3class frt3;
FragmentTransaction ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tabs_exp);
ft = getSupportFragmentManager().beginTransaction();
frt1 = new fragmentclass();
frt2 = new fragmentclass2();
frt3 = new fragment3class();
//ft.add(R.id.fragment1,frt1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu);
return true;
}
public void switchToTab2 (View v){
ft.replace(R.id.fragment1, frt2).commit();
}
public void switchToTab3 (View v) {
ft.replace(R.id.fragment1, frt3).commit();
}
when i click on button2 the fragment class2 is successfully visible except that the default class of the fragment remain visible as well is there anyway to hide it and make the new fragment class active

Adding fragments in your xml file will make is static through out your application and you can not change it dynamically.
If you want to add Fragments dynamically then you need to add FrameLayout in your xml file and load all your fragments dynamically in that layout and replace it one another while loading fragments one after another.
Do below change in your layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<FrameLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>

Remove fragment1 from your XML file (so container is empty by default on layout creation time) and add it from code (i.e. in onCreateView() of parent fragment or in onCreate() of Activity), as fragments cannot be removed out of hierarchy if they are part of XML layout file.
I'd also change container to be FrameLayout instead of LinearLayout if you plan to have just one child in it.

Related

Fragment transaction from main activity doesn't work

I'm writing my first android app using fragments but I can't figure out why it doesn't change view from main activity to fragment.
This is my mainActivity.java:
public class MainActivity extends AppCompatActivity {
Button fragment1_BTN, fragment2_BTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1_BTN = findViewById(R.id.frag1_btn);
fragment2_BTN = findViewById(R.id.frag2_btn);
fragment1_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
replaceFragment(new Frag1());
}
});
fragment2_BTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
replaceFragment(new Frag2());
}
});
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}}
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/therapists_btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:backgroundTint="#color/teal_700"
android:text="button 1"
android:layout_marginRight="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/patients_btn" />
<Button
android:id="#+id/patients_btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:backgroundTint="#color/teal_700"
android:text="button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/therapists_btn"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>
The problem is that when I click on button 1 I see the view of first fragment and also of main activity.
How can I see only the fragment view?
your frameLayout container for Fragments is placed before LinearLayout, Fragments View is loaded into it and is visible, but also rest of Views is drawn after that (as order in XML) on top of it. just setVisibility(View.GONE) for this LinearLayout when you are loading Fragment (set for it some id and use findViewById as for buttons). another approach would be to put your LinearLayout inside container - you are using replace method so Fragment will remove your buttons, but in that case these are gone forever then and you don't have an easy option for bringing them back (with your current code - only whole Activity restart)
it would be way better (by design) if your Activity wouldn't have these buttons at all, only container, and on start would check if container has anything loaded (e.g. getChildCount()==0), if not then load some initial Fragment for preventing displaying empty Activity

How to remove the title from toolbar menu in Kotlin?

i have this toolbar
i want to remove the title of the fragment so i can display more items
is it from the xml file or the activity?
because i tried to add this to my main activity:
getActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolbar).setTitle("");
toolbar.setTitle(null)
but none of them worked
any suggestion?
this is MainActiviy code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
setupBottomNavMenu(navController)
setupSideNavigationMenu(navController)
setupActionBar(navController)
Since you are using a Navigation component you can use the addOnDestinationChangedListener to set your custom title after your setup method.
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.xxxx) {
//If in your setup you are using a Toolbar
toolbar.title = ""
//supportActionBar?.title = "" //If you are using setSupportActionBar()
} else {
//
}
}
You could create your own tool bar for each fragment and customize it as per your requirement. I have made a common toolbar xml file and reusing it in every fragment and customizing it. Look :
common_toolbar.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/const_item"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/margin_normal"
android:gravity="center"
android:padding="#dimen/padding_five"
android:text="Back"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:drawableStartCompat="#drawable/ic_action_chevron_left"
app:drawableTint="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title_toolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/menu"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
And then adding it to fragments's UI. Let's say, adding to fragment_user_details.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar_common"
android:id="#+id/toolbar_user_details"/>
<!--
Other View widgets
-->
</androidx.constraintlayout.widget.ConstraintLayout>
As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement. Make sure Activity has Theme.MaterialComponents.Light.NoActionBar theme.
Menu Items can also be added like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

Button OnClick from Fragment to MainActivity

I have 3 Fragments inside my MainActivity and in one of my Fragments I have 3 ImageView and each of image view performs same action based on which ImageView was selected, now What I want to achieve is that instead of using OnClickListener for each of Views separately, call same method with switch-case or if-else inside. But the problem is those ImageViews inside fragment cannot call Functions implemented inside MainActivity. for instance. here's the code to understand what I want to say:
MAIN ACTIVITY
public void imageClicked(View v){
if(v.getID() == findViewById(R.id.id_of_imageView_1).getID()){
myTextView.setText("ImageView 1 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_2).getID()){
myTextView.setText("ImageView 2 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_3).getID()){
myTextView.setText("ImageView 3 is Clicked")
}
}
XML (fragment_images.xml) WHERE IMAGES EXIST
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/id_of_imageView_1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/laptop" />
<ImageView
android:id="#+id/id_of_imageView_2"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/memory" />
<ImageView
android:id="#+id/id_of_imageView_3"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/screen" />
</LinearLayout>
</ScrollView>
XML (activity_main.xml) HERE I Link my Fragment and TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
//CHANGE THIS TEXT VIEW WHEN IMAGE IS CLICKED
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="Select Product"
android:textSize="20sp"
android:textStyle="bold" />
<fragment
android:id="#+id/fragment_1"
android:name="com.salmanibrahim.calssifiedelectronics.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_list" />
<fragment
android:id="#+id/fragment_2"
android:name="com.salmanibrahim.calssifiedelectronics.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_detail" />
//THIS IS THE FRAGMENT
<fragment
android:id="#+id/fragment_images"
android:name="com.salmanibrahim.calssifiedelectronics.AddProduct"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/fragment_images" />
</LinearLayout>
How do I call imageClicked() defined inside MainActivity using onClick on ImageView
In your fragment you can call imageClicked() in the MainActivity by using requireActivity() or getActivity() and do the necessary cast
In fragment:
public View onCreateView(...) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
MainActivity mActivity = (MainActivity) requireActivity();
imageView1 = view.findViewById(...);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mActivity.imageClicked(imageView1);
}
});
...}
repeat the same for imageView2, 3
Also the condition in imageClicked() seems not right as you findViewById which will point to your MainActivity layout, not the fragment.
So you need to change this
if (v.getID() == findViewById(R.id.id_of_imageView_1).getID())
To
if (v.getID() == R.id.id_of_imageView_1)
And do the same for other images.
I understand what you trying to do but android:onClick parameter only links for the context declared in tools:context field on the parent tag of your layout file.
So it is not possible to reference your imageClicked() function found in your MainActivity from your fragment's ImageView because they are in different context. Check this for more info.
You are doing it in wrong way.
You can do this instead.
imageView_1.setOnClickListener(this);
then implement activity from View.onClickListener
then get view by id on the method that's been implemented.
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.id_of_imageView_1:
// do your things here
break;
}
}

java.lang.IllegalArgumentException: No view found for id?

I am trying to add a fragment from an activity using the add(R.id.containerId, fragment);. However the log gives the java.lang.IllegalArgumentException: No view found for id error.
The fragment layout contains an id in the root element. I know that placing the container id in a child element is supposed to fix things, but this has made no difference for me.
Here is the root element in the fragment layout xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical"
tools:context=".view.fragment.Fragment">
Here is the onCreate() method in my activity:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment);
currentFragment = R.id.fragment_container;
transaction.commit();
Here is the onCreateView() method in the fragment:
return inflater.inflate(R.layout.fragment, container, false);
Here is the entire activity code (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
public class MainActivity extends AppCompatActivity {
private int currentFragmentId;
private FeedFragment feedFragment = new FeedFragment();
private EventsFragment eventsFragment = new EventsFragment();
private SearchFragment searchFragment = new SearchFragment();
private MoreFragment moreFragment = new MoreFragment();
// Add other fragments
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_feed_container, feedFragment);
currentFragmentId = R.id.fragment_feed_container;
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_feed:
// Add the feed fragment
replaceFragment(feedFragment, currentFragmentId);
currentFragmentId = R.id.fragment_feed_container;
return true;
case R.id.action_events:
// Add the events fragment
replaceFragment(eventsFragment, currentFragmentId);
currentFragmentId = R.id.fragment_events_container;
return true;
case R.id.action_search:
// Add the search fragment
replaceFragment(searchFragment, currentFragmentId);
currentFragmentId = R.id.fragment_search_container;
return true;
case R.id.action_more:
// Add the more fragment
replaceFragment(moreFragment, currentFragmentId);
currentFragmentId = R.id.fragment_more_container;
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
/**
* Replaces the current fragment with a new fragment
*
* #param newFragment
* #param currentFragmentId
*/
private void replaceFragment(Fragment newFragment, int currentFragmentId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(currentFragmentId, newFragment);
transaction.commit();
}
}
Here is the full fragment class (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
public class FeedFragment extends Fragment {
public View mView;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_feed, container, false);
return mView;
}
}
Here is the code for the full fragment_feed.xml file (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_feed_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical"
tools:context=".view.fragment.FeedFragment">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/top_card_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/card_profile_image_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
android:id="#+id/card_info_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="start"
tools:text="Julien Durrand invited you" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
tools:text="3 months" />
</LinearLayout>
<ImageView
android:id="#+id/card_main_image_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/top_card_linear_layout"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_main_image_view"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="start"
tools:text="30K" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
 
Here is the main parts of the logcat stacktrace (the naming and ids are different because I changed them in the snippets above, imports statements are also missing for brevity):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomfinet.magpie/com.tomfinet.magpie.view.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}
Stuff that I have checked:
Fragment layout is correct in the onCreateView() method in the fragment.
Fragment layout container id in child of the fragment xml file.
How do I fix this error?
Thank you.

Adding two ListViews to a Fragment

I am doing a time management app for android for my project in school. So there is a class called Activity which represents the activities done by the user through the day(Like going to school,listening to music,eating etc). Activity objects have an instant variable called done which is type of boolean and another insant variable Tags which is a Tag array. There is need for boolean done because user can enter activities for the future and later he/she can mark it as done. Tag class is for the user to label their activities(For example an activity can have tags: sport and hobby at the same time).These activities entered by the user to the program are shown in a fragment called Activities fragment. There will be 2 different Listviews in this fragment one for done and the other for undone activities. So I wrote my xml code so that it would have 2 Listviews on top of each other and added this listviews to my fragment. However, when I run my code I only can see one of the Listviews, the other one does not show up.This is how my xml file look like:(here is the picture of the design:http://postimg.org/image/a7u1ynfzb/)
<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:orientation="vertical"
tools:context="com.beter.timehole.fragment.ActivitiesFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/listView1"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/listView2"
/>
</LinearLayout>
Here is my code for fragment class:
public class ActivitiesFragment extends Fragment {
public ActivitiesFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayList<Tag> tags1= new ArrayList<Tag>();
tags1.add(new Tag("Tag1"));
ArrayList<Tag> tags2= new ArrayList<Tag>();
tags2.add(new Tag("Tag2"));
com.beter.timehole.core.Activity doneSample = new com.beter.timehole.core.Activity("Work",true,70,"16 50","17 00",tags1,"did it");
com.beter.timehole.core.Activity undoneSample = new com.beter.timehole.core.Activity("Fun",false,30,"13 30","14 00",tags2,"will do it");
View rootView = inflater.inflate(R.layout.activity_fragment, container, false);
ArrayList<com.beter.timehole.core.Activity> doneActivities = new ArrayList<com.beter.timehole.core.Activity>();
doneActivities.add(doneSample);
ListView doneList = (ListView) rootView.findViewById(R.id.listView1);
doneList.setAdapter(new ArrayAdapter<com.beter.timehole.core.Activity>(getActivity(), R.layout.support_simple_spinner_dropdown_item, doneActivities));
ArrayList<com.beter.timehole.core.Activity> undoneActivities = new ArrayList<com.beter.timehole.core.Activity>();
undoneActivities.add(undoneSample);
ListView undoneList = (ListView) rootView.findViewById(R.id.listView2);
undoneList.setAdapter(new ArrayAdapter<com.beter.timehole.core.Activity>(getActivity(),R.layout.support_simple_spinner_dropdown_item,undoneActivities));
return rootView;
}
}
Try to Change layout_height="0dp" .
Try this layout code.
`
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<ListView
android:id="#+id/listView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
`
If this is not working try to check your code or put your complete code here.
Please try this
<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:orientation="vertical"
tools:context="com.beter.timehole.fragment.ActivitiesFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/listView1"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/listView2"
/></LinearLayout>

Categories

Resources