Cause Layout get focus before its views - android

I am developing an application for Android TV.
I have a layout which have horizontal LinearLayout (custom) which is used for navigation and below it a container for fragments.
What I want is when a user is focused on any item in the fragment (assuming that the fragment has only 1 row of views), when he clicks on UP on the D-Pad, the LinearLayout above it will get the focus and not the closest.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.myApp.tv.view.categories.CategoriesLayout
android:id="#+id/home_categoriesLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="30dp"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/home_contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I really prefer not to interfere the fragment code.
Please give me your suggestions, thank you

Related

Android - How to apply selectableItemBackground to entire ListView item?

I am new to Android, writing my first app right now. I've got a ListView rendering a series of items, each item having multiple TextViews. I want each item in the ListView to be click-able, and to animate the click. The animation is what I am struggling with.
I have the selectableItemBackground attribute, and it works perfectly on a TextView-by-TextView basis. However, that means that the click animation only appears over that particular TextView, whereas I want it to appear over the entire list item itself.
Here is my Android view. I know that this behavior is caused by me putting the selectableItemBackground attribute on the TextView elements, I just can't figure out where else to put it to achieve the effect I'm going for.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="#+id/past_game_update_timestamp"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/small_padding"
android:textSize="#dimen/small_font_size" />
<TextView
android:id="#+id/past_game_start_timestamp"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/small_padding"
android:textSize="#dimen/small_font_size" />
</LinearLayout>
As part of what I understood from your question. Is that you want your whole view to be having an onClick animation.
For this you have two options :
In most cases, you should apply this functionality in your view XML by specifying the view background as:
?android:attr/selectableItemBackground for a bounded ripple.
?android:attr/selectableItemBackgroundBorderless for a ripple that
extends beyond the view. It will be drawn upon and bounded by, the nearest parent
of the view with a non-null background.
Note: selectableItemBackgroundBorderless is a new attribute introduced
in API level 21.
Please check with option 2.
Ok, after I commented on the above answer, I figured out the rest of it. I needed to make the LinearLayout clickable & focusable, and I needed to disable clickable/focusable on the TextViews. I also needed to set them to duplicateParentState. All of that allows the selectableItemBackground to work on the LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:gravity="center"
android:padding="#dimen/small_padding">
<TextView
android:id="#+id/past_game_update_timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:focusable="false"
android:clickable="false"
android:textSize="#dimen/small_font_size" />
<TextView
android:id="#+id/past_game_start_timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:focusable="false"
android:clickable="false"
android:textSize="#dimen/small_font_size" />
</LinearLayout>
you can move ?android:attr/selectableItemBackground into the root view of your item layout.
Like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<TextView
android:id="#+id/past_game_update_timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/small_padding"
android:textSize="#dimen/small_font_size" />
<TextView
android:id="#+id/past_game_start_timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/small_padding"
android:textSize="#dimen/small_font_size" />
</LinearLayout>
don't forget to add android:focusable="true" and android:clickable="true"

How to add FrameLayout inside LinearLayout

Hi i am new for android in my app i am adding Frame-layout inside LinearLayout but Frame-Layout and it's inside fields are not adding
my code is below please help me some one
main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent'"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight ="1"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_dark"
android:text="something" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#android:color/holo_red_light"/>
</FrameLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/frame" />
</LinearLayout>
I think you misunderstand the purpose of FrameLayout.
http://developer.android.com/reference/android/widget/FrameLayout.html
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other.
You are trying to put two items inside of it. I would suggest removing the FrameLayout. You have no need of it.
<?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:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</FrameLayout>
</LinearLayout>
This works for displaying a single button, but there's no point in having it there.

Show fragments above another fragments

Basically, what I want to do is:
1.- I have two fragments "outside" the activity.
2.- Now there could be two possibilities, fragment 1 or fragment must be shown with a slide up animation.
3.- Fragment 1 is always above fragment 2 when both are on screen.
4.- Any of the fragments can hide, placing correctly the showing one at the botton of the screen.
I am a little bit lost with layout design. I have tried
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="-20dp"
android:visibility="gone" />
<FrameLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="-20dp"
android:visibility="gone" />
</RelativeLayout>
And I even show both of them at the same time. Even using different fragmenttransactions for each fragment. Using, android:layout_above doesn't work.
What is the simplest way to develop this design?
Try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-100dp"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorAccent"
/>
<FrameLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
/>
</LinearLayout>
</RelativeLayout>
Animate the linearLayout. And as per your requirement change the visiblity of framelayout(gone/visible)
Maybe using two FrameLayouts for FragmentA would help for the two scenarios.
When it is the only one visible you can use
android:layout_alignParentBottom="true"
When it is on top of FrameLayout holding FragmentB, you can use
android:layout_above="#+id/fragment2"
Then play with the "visibilities" of these based on your scenarios.

Clicking on Edittext inside nested Scrollview scrolls outer scrollview

I have a parent horizontalScrollview that contains various layouts. In one particular layout there is a scrollview. Inside that scrollview there is edittext.
Now when I click on any edittext for input, the horizontal scrollview automatically scrolls the screen to some other layout and I am not able to see the current active layout(containing scrollview).
I know nested scrollviews is a bad coding practice but I can not change it in this case as there will be large amount of rework. Is there any workaround for this.
Following is the xml code for main layout containing horizontalscrollview:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:id="#+id/CloudHScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and following is the code for one of the child layouts that is inserted later at some stage. This layout contains the scrollview which has edittexts:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:background="#drawable/blueoverlay"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="300dp"
android:paddingTop="90dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:gravity="right"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black" />
</LinearLayout>
</LinearLayout>
</ScrollView>
I'm not sure if this will work, but you can try extending outer ScrollView class and handle its onScroll events on your own.
look here:
listening to scroll events horizontalscrollview android
Android: how to listen for scrolling events?
I had the same issue, solved it by adding the following to AndroidManifest.xml file for the corresponding activity.
android:windowSoftInputMode="adjustResize"

Android: Overlap in ExpandableListview in LinearLeyout

In my android application i have an expandable list view and a button in a linearLayout but when i open a sublist, some kind of overlap between items and just listview is in layout and button hidde under listview. I have 2 item over each other. one of them is hiding under other.
How can i fix it?
<?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:layout_height="match_parent">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expandableListView"/>
<Button
android:id="#+id/backup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BackUp"
android:clickable="true"/>
</LinearLayout>

Categories

Resources