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>
Related
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" />
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>
How could be done such thing, like "watermark" on top of any other Activities on Android?
In detail: I need to show some message on top of everything what is running on my device. No matter if this is game, movie, app.
Is possible to achive something on Android? My phone is rooted.
Is possible to use some OpenGL features?
Use this code.
At first, I am using here FrameLayout, that is transparent and you can see "watermarked" text on image.
At second, I am using android:background="#99 000000" on my button and you also can see it on the image. First 2 digits, as i know, named alpha channel and you can set them from 0 to ff; 0 is fully transparent, and ff is non-transparent, as usual.
I think you get the idea or may be you can draw your custom image on canvas, put on your framelayout and set 50% trasparent-background. And you have to do this 50% transparent layers onto all your layouts and so, all activities will have watermark.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/ic_launcher" />
</FrameLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#99000000" />
</LinearLayout>
</FrameLayout>
Use a master activity which have layout according to you. And then use this master activity in other activity by extending to child activity.
Its simple.
Yes, it is possible to run a base activity and over that some intended activity to perform the action you want.
Now, you can use the concept of sub-activities and activities in android to ensure this type of result.
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!
Do you know how to achieve the same effect as winamp for android ? I want to do the similar thing. That is when I click on listview, sliding drawer popup. But so far I only can show the sliding drawer in new activity not in the same.
How can I achieve in overlap view. That is when I close the drawer, the layout is show at the front, and the sliding handle is on the layout, when I open the drawer, it covers the main layout.
Any ideas about that ?
Thanks !!
Step #1: Create a RelativeLayout
Step #2: Put the rest of your UI in the RelativeLayout
Step #3: Put the SlidingDrawer in the RelativeLayout as a later child then the rest of the UI (e.g., in layout XML, have it as the last child element of the RelativeLayout)
Children of RelativeLayout (and FrameLayout) stack on top of one another on the Z-axis (i.e., out the face of the screen). Hence, later children will overlap earlier ones. By putting your SlidingDrawer last, it will overlap everything else when opened.
Thank you CommonsWare you helped me,I do not have much reputation to vote up. Here is my sample layout...
I used it.sephiroth.slider.widget.MultiDirectionSlidingDrawer as the SlidingDrawer
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginLeft="82dp"
android:layout_alignTop="#+id/button_open">
<Button
android:id="#+id/button_open"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/open"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New EditText"
android:id="#+id/editText" android:layout_alignParentLeft="true" android:layout_marginLeft="114dp"
android:layout_alignParentTop="true" android:layout_marginTop="6dp"/>
</RelativeLayout>
<it.sephiroth.demo.slider.widget.MultiDirectionSlidingDrawer
xmlns:panel="http://schemas.android.com/apk/res/it.sephiroth.demo.slider"
android:id="#+id/drawer"
panel:direction="topToBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
panel:handle="#+id/handle"
panel:content="#+id/content">
<include
android:id="#id/content"
layout="#layout/pen_content" />
<ImageView
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="40px"
android:src="#drawable/sliding_drawer_handle_bottom" />
</it.sephiroth.demo.slider.widget.MultiDirectionSlidingDrawer>
</RelativeLayout>