Custom FrameLayout that adds items to sub FrameLayout? - android

I am trying to make some kind of button that can have content added to it like it is done to a FrameLayout. For example:
<ch.pboos.android.ui.ContentButton ...>
<LinearLayout ...>
<TextView .../>
<TextView .../>
</LinearLayout>
</ch.pboos.android.ui.ContentButton>
So far I made a view that xml file that would represent how i want the button to look like:
<?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" android:paddingLeft="#dimen/white_button_margin"
android:paddingRight="#dimen/white_button_margin">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:padding="#dimen/white_button_padding"
android:background="#drawable/channel_bg">
<FrameLayout android:id="#+id/content" ...>
<!-- content -->
</FrameLayout>
<ImageView ... android:src="#drawable/right_triangle" />
</LinearLayout>
</LinearLayout>
Now I would like to make a custom FrameLayout (ch.pboos.android.ui.ContentButton) that will add that view and allow me to add views into the content area of that xml above.
Any suggestions on how to get this working correctly?

1) Dispatch all touch events occurring on ContentButton to custom button via overriding dispatchTouchEvent. And add on click listener to this button (this helps you to recognize and handle only clicks, not e.g. moves)
2) override addView this way:
addView(...) {
if(inflatingInProgress) {
super.addView(view);
}
else {
// add view to #+id/content
}
}
// where flag inflatingInProgress initially = true and is set to false after
// finishing inflating the layout for ContentButton
Hope this helps. Good luck.

Related

Custom view xml versus its usage

I have a custom view CustomSettingEntry this is its xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/layout_custom_setting_entry_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/custom_setting_entry_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Then I use it in a fragment and assign it an id:
<com.mypackage.name.CustomSettingEntry
android:id="#+id/layout_notification_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setting_title="#string/title"
app:setting_subtitle="#string/subtitle" />
My question is what's the difference between the LinearLayout with id layout_custom_setting_entry_container and the usage with id layout_notification_setting? do they refer to the same thing?
If I set a click listener on the custom view inside the fragment, then later in some condition disabled clicking on the root LinearLayout inside the custom view, will this stop the listener?
I understand your custom view is a subclass of LinearLayout which inflates an xml layout. If so, the resulting view hierarchy is
[LinearLayout (actually a CustomSettingEntry), id : layout_notification_setting]
[LinearLayout, id : layout_custom_setting_entry_container]
...
So there are two levels of viewgroups, each one with it own id.
By the way this is inefficient because, the outer viewgroup (your custom class) has only one child (the root of the xml).
On solution is to use a <merge> as root of the xml layout to skip one level. See Optimize by merging or Inflating layout for your custom view
for more details.

Many layouts in Scroll View in android

I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_main.xml
<?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="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.

Drawerlayout is intercepting all touch events

My drawerLayout is intercepting all touch events. I need help figuring out how to click on elements below it. Here is how my drawerLayout is set up
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
.... some code ...
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I then reference this layout in another layout like this
<!-- elements inside of this relative layout
are unclickable because the drawer layout below
intercepts all clicks. If I place the drawer above
this view the drawer is beneath all the elements
in the relative layout, however, I am able to do
my clicks as expected. What I am doing incorrectly? -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... some code * the buttons here are what
I want to be clickable when drawer is closed.
I currently cannot reach them ... -->
</RelativeLayout>
<include layout="#layout/drawer" />
</FrameLayout>
I have added android:clickable="false" and android:focusable="false" in the xml on the drawer. Programmatically I logged the clicks, and it is the actual drawer with id "drawer_layout" intercepting all clicks. Making it unclickable does not work, it is still clickable. I also tried setting the visibility to INVISIBLE and GONE and these are not viable solutions either.
How can I make that drawerLayout allow clicks through it to elements beneath it? Thanks in advance!
By setting clickable and focusable to false, I think you are disabling click to the views in the layout too. Remove these options from the drawer layout.
Try setting android:clickable="true" and android:focusable="true" for the container inside the drawer layout.
I figured this out. I ended up adding my main layout to the drawer which is the reverse of what I was doing before because before I was adding the drawer to my main layout. So the change I made was
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/main_layout" />
</FrameLayout>
<RelativeLayout
.... some code ...
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
If anyone else runs into this problem it is view hierarchy conflict. You cannot have your drawer on top of elements that you want to interact it. If you have buttons or other widgets that you need to interact it you should include it to your drawer layout instead. I hope this helps someone else out there.

Click events on SlidingPaneLayout

I'm trying to use the SlidingPaneLayout. The left view is a ListFragment and the right view is a detail view. The layout is displayed correctly and I can slide it. But if the detail view is in front of the list and I click on it, the list in the background receives the click.
My layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="net.name.multiremote.RemoteListFragement"
android:id="#+id/fragment_remote_list"
android:layout_width="580dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<fragment
android:id="#+id/fragment_remote"
android:name="net.name.multiremote.RemoteFragment"
android:layout_width="850dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</android.support.v4.widget.SlidingPaneLayout>
I use this code for setting up the click listener in the ListFragment
#Override
public void onListItemClick(ListView list, View view, int position, long id) {
iItemClickListener.onListFragmentItemClick(view, position);
}
How can I solve this?
Just add android:clickable="true" to the second Fragment or FrameLayout in the SlidingPaneLayout.
Locutus was correct.
Whatever the fragment on top, add the property
android:clickable="true"
so it will not pass the click event to the fragment below.
Thanks everyone for saving my time.
Here is my code. I have used an overridden layout but this works on regular sliding pane layout as well.
look at the 2nd fragment, I've added clickable true property.
<com.ironone.streaming.application.MySlidingPaneLayout
android:id="#+id/pane"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/pane1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/pane2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
</com.ironone.streaming.application.MySlidingPaneLayout>
I have the same problem, I think it's a combination of "v4" version of Fragment and ListFragment and the SlidingPanelLayout...
If you change the import from "v4" to import normal "android.app.ListFragment;" and "import android.app.Fragment;" everything works.
Sorry for my english ;)

dynamically changing custom view as common footer in android application

In my android app i want to put a common footer for all the activities. I created a custom view which extends Relative Layout, which contains a ImageView. It'll change the source for every 10 sec depends upon the data from server. I had put my custom view class in every activity's XML file also.
Now my problem is, i'm unable to find the way how it will instantiated in every activity means how the result view will added to every activity.
Simply, i want Admob Advertisement like feature in my application.
This is One of my Activities XML layout:
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
>
<RelativeLayout
android:id="____"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
----
----
</RelativeLayout>
<com.ninepstudio.movieme.activities.Banner
android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/activity_movies_layout" />
</RelativeLayout>

Categories

Resources