Same fragments in one activity problem - android

I'm coding my first android app with fragments and here is my problem. I've got main activity and 3 fragments in it. All 3 fragments are the same class so all 3 fragments has the same layout. But I need every fragment to has different title in that layout. But I can't select the TextView wich I need because all these fragmtents TextViews has the same ID because of the same layout. Is there some easy way to do this.
Thanx

Given your Fragments f1, f2, f3, you could try f1.getView().findViewById(id), f2.getView().findViewById(id), f3.getView().findViewById(id) to reduce the scope of the search to only that particular fragment each time.
However, I think the point of using Fragments is to improve modularity and to avoid things like having to expose all of the views to the Activity. It can be controlled by the Fragment instead. You could have the views be resolved inside the Fragment and then give a setTitle() method on the Fragment for the Activity to use.

The ID should not matter since each class will be inflating its own instance of the layout. Try it using the same ID.

If under 'fragment' you mean an UI component you should specify different IDs for each and every component in your layout!
<LinearLayout
android:id="#+id/LinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff0000"
android:text="Header"
/>
<TextView
android:id="#+id/body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00"
android:text="Body"
/>
<TextView
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:text="Footer"
/>
</LinearLayout>
If you mean other xml fragments inside a main xml layout, you can use same ID, but in different xml fragments!

Related

Which layout type to use for creating this interface?

I am trying to create the left layout from this link
Blog App User Interface
by Thomas Budiman on Dribbble.com
I have followed this https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView tutorial and I am now able to design heterogenous layouts inside RecyclerView
Actually I need help to figure out,
Whether RecyclerView is the right choice to create such layout ? (I'm trying to create a blog app and my activity is supposed to show list of blog posts, in the manner shown in Dribbble screenshot.)
If yes, then what logic can be used to achieve it? (acc to me getItemViewType should be responsible to tell what layout to use. )
Any help would be be appreciated
Thanks!
You can have a LinearLayout to contain this view which has a padding like this:
<LinearLayout
android:orientation="vertical"
android:padding="16dp"
...
>
<ImageView>
<TextVeiw android:id="#+id/title">
<TextView android:id="#+id/body">
<FrameLayout
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/gray"
/>
<LinearLayout
android:orientation="horizontal"
...
>
<ImageView
android:layout_weight="1" />
<FrameLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/gray"
/>
<ImageView
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And if you want it to be repeated just use a RecyclerView. If you have any questions you can ask.

Android: ListView pushes out everything else below it

In my app, there is a tabbed activity with three fragments. The first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. The XML layout of this third fragment is as follows:
<?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:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
As you can see, there is a TextView and a ListView and below that is another LinearLayout. Here is how it should look (the purple bar is the TextView):
And here is how it actually looks:
The TextView shows up above the ListView, but the LinearLayout does not. It is there, though. If I do android:layout_marginBottom="20dp" on the outermost LinearLayout, it does show up, but the ActionBar scrolls up and overlaps with the notification bar so that the title on the ActionBar and the notifications on the notification bar are both visible simultaneously and neither is legible.
I searched a lot and this seemed a common issue, but none of the solutions helped me. Believe me, I tried everything I could find. I tried wrapping the whole thing in a FrameLayout, using a RelativeLayout in place of the outermost LinearLayout, using two LinearLayouts--one to wrap the TextView and the ListView and the other to wrap the EditText and the ImageButton, etc., but nothing was able to show the bottom LinearLayout below the ListView. I even tried to set focus on the EditText when the fragment launches so that the keyboard would show and I can type, but even that doesn't help.
Note: On the other hand, if I use the exact same layout on an activity, the bottom LinearLayout displays exactly as it should.
I am unable to find the bug. Please help!
It looks like your toolbar pushes fragment layout down without decreasing its height. I have no idea why this happens (there are no root layout code here).
As a workaround you can set fragments layout bottom margin to ?attr/actionBarSize
As you have given layout_weight 1 in listview layout, so it occupies the whole space available. In order to get rid of you have to give some static height or use layout_weight in right manner
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:transcriptMode="normal" />
or try like this
<?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:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white">
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
All,
Thank you for all your responses. I found a workaround by trial and error now and thought I should post the answer for others who face the same issue. I set android:layout_marginBottom="50dp" on the inner LinearLayout (the one wrapping the comments bar--EditText and ImageButton). Somehow, this sets the layout correctly and the fragment functions properly in both Lollipop and Jellybean OS's. I haven't tested on other OS versions.
First of all you should read this, what is layout_weight and how it works.
You assigned layout_weight to ListView as 1, it means it covers whole height. just change it to 0.7 or any proportion you want (less than 1) will solve the problem.
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:transcriptMode="normal" />

Common View through Activities

I need to create multiple activities that will have a bar at the bottom of the screen. This bar contains three functions that are common to the activities. Can i keep the bar constantly on screen and switch through activities?
One way could be to load different fragments on the same activity, and let the bar be below the fragments.
But is it possible to switch between activities, without recreating the bar every time a new activity opens?
Possible option:
You have to create bottom layout bottomlayout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/toolBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:text="TOOLBAR TEXT"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</RelativeLayout>
Include bottomlayout in every xml layout of activity.
<include layout="#layout/bottomlayout"/>
Fragments, is one way of doing it. Isn't there any other.

Can and should I use two fragments in a Activitiy

I have little bit experience with Android and I'm not sure how to resolve this design problem. Maybe someone know a good solution. I like to build an Activity which look like this:
http://i.stack.imgur.com/CwQaU.png
The user should have to choice to drag the marker on the map or to enter the adress. I like to define the whole layout in XML without to extend from the map fragment.
Have someone a example how I can split the screen 80/20 with two fragments?
Are fragments are the right choice?
Just use a single fragment in this case. In your XML you'll want a LinearLayout so you can utilize weights to achieve the 80/20 you described above.
<LinearLayout>
<MapFragment/>
<EditText/>
</LinearLayout>
Give the linearLayout a "vertical" orientation and assign a weights of 4 and 1 to the children to split them 80/20%.
If this isn't enough to go off, let me know and I can explain further.
Good Luck!
You can use LinearLayout and play with the layout_weight parameter on the fragments to split the screen that way. Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="name"
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="0sp" />
<fragment
android:name="name"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0sp" />
I don't think you need 2 fragments for this.
You can create one main LinearLayout with vertical orientation and inside set two RelativeLayout. One with map fragment and second with edittext.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="0sp" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0sp"
android:background="#999">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center"/>
</RelativeLayout>
</LinearLayout>

Android Design Principle for most used fragment

I am designing an Android Application in Android where there is one entry point activity which has 3 TABS and I need to allow user to update his status from any of the Tabs. So, I have created a StatusFragment in activity_main.xml. So it is available at the top of each fragment in tabs. I just wanted to ask if there is a better way of doing this. I want to minimize the screen estate used by Status Fragment. Here goes my StatusFragment's layout 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:divider="#android:drawable/divider_horizontal_dark"
android:showDividers="middle"
android:dividerPadding="#dimen/list_view_item_image_padding"
android:orientation="horizontal"
android:padding="#dimen/list_view_item_image_padding">
<TextView
android:id="#+id/tv_status"
android:padding="#dimen/list_view_item_image_padding"
android:layout_margin="#dimen/list_view_item_image_margin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:maxLines="2"
android:ellipsize="end"
android:scrollHorizontally="true"
android:layout_weight="1"
android:text="#string/hello_blank_fragment" />
<ImageButton
android:id="#+id/iv_edit_status"
android:padding="#dimen/list_view_item_image_padding"
android:layout_margin="#dimen/list_view_item_image_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_edit"
android:background="#drawable/list_view_button_selector"
/>
Sory for bad english. If I'm at wrong place please guide me to the right one. Any help provided will be helpful.
I have thought of doing it in a NavigationDrawer and SlidingPaneLayout but I guess these layouts are overkill for this task.
You can have the status as part of the main activity and just change the fragment corresponding to the tab inside of a frame layout:
<RelativeLayout>
<LinearLayout>
<TextView/>
<ImageButton/>
</LinearLayout>
<FrameLayout android:id="#+id/fragment_container"/>
</RelativeLayout>

Categories

Resources