I am having an activity and want to create a like button at the end of the layout, So I created a layout file and in a LinearLayout I have set it's layout_alignParentBottom property to true and created button for Likes in it. Now I am including this layout file in some other layout file but when I am applying onClickListener to the button, it does nothing.
When I remove this layout_alignParentBottom from the LinearLayout properties, then OnclickListener start working.
Can you please help me here to resolve this issue?
Some other widget might be coming in its way. if there is something above that button, it wont take clickListener.
For Ex. if there is a list in that layout too,
<Button
android:layout_alignParentBottom="true"
android:layout_marginLeft="#dimen/viewSpace1"
android:layout_marginRight="#dimen/viewSpace1"
android:layout_marginBottom="#dimen/viewSpace1"
android:layout_width="fill_parent"
android:layout_height="#dimen/headerHeight_small"
android:id="#+id/btnShare"
style="#style/ButtonLogin"
android:text="Next" />
<ListView
android:layout_above="#id/btnShare"
android:id="#+id/list"
android:layout_below="#id/layoutHeader"
android:layout_marginLeft="#dimen/viewSpace3"
android:layout_marginRight="#dimen/viewSpace3"
android:layout_marginBottom="#dimen/viewSpace3"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
so your share button stays safe for clickability.
I have kept the list above btnShare. Just for my safety if it overlaps the button.If there is still problem, post your code so exact problem can be pin pointed.
I need 2 relative layouts to overlap in a parent layout. I'm not sure what the parent layout should be, but I think it has to be FrameLayout.
<FrameLayout>
<RelativeLayout id=layout_one>
<Button></Button>
<ImageView></ImageView>
...
</RelativeLayout>
<RelativeLayout id=layout_two>
<Button id = a></Button>
<Button id = b></Button>
...
</RelativeLayout>
</FrameLayout>
Only one of the RelativeLayouts will be seen at a time. At first it is "layout_two". Then when Button "a" is clicked "layout_two" needs to be invisible and "layout_one" must appear. However, clicking Button a doesn't call onClick method, although I can see the button. Is there better way of doing what I'm trying to do?
Make Relative Layout as parent and then put your layouts on it.what I suggest is use Relative.
In my android project, I need to add controls dynamically into my main activity screen. I created one xml (row.xml) which is added on button click on main screen. I want to capture events from the controls (button) given in row.xml.
Can anybody help me where and how to capture onClick events from newly added layouts?
Also, I want to add many child layout elements, do I need to write separate onClick methods for all the child views added dynamically?
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_Time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_FromTime"
android:layout_width="216dp"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:hint="#string/hintFromTime" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/button_Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDelete" />
</LinearLayout>
</LinearLayout>
So, when I click on Add Time slot button, I get a newly created row with two elements.
I want to delete this row when I click on Delete button. Do I need to have a viewID also to delete this newly-added-view?
Create an onclicklistener in the list adapter and set it to the buttons in the getView method of the adapter. That should work.
You can keep track of your controls as Java variables - don't worry about dynamic android xml. Consider declaring them all at the top, outside of methods.
One way to avoid adding a new OnClickListener for each control is let your class implement OnClickListener then use view.setOnClickListener(this). Alternatively create a subclass which overrides onClick(View) and use setOnClickListener(MyListener).
You can use Layout.removeView(View) to remove controls, as long as you keep track of them.
I am developing an android application.
I was created an activty that contains several components
on the top it contains spinner,
after that it contains linear layout in which it has two textview,
1 has static value and other is dynamic value that is filled when user click on that linear loyout an dialogbox is created and after setting value on that dialog it fills other textview.
i have 4 linearlayout of this type after that i have another linear layout at the end that contains 2 button.
The problem is that in emulator when i scroll mouse it focus on the spinner and after that the last button(means it color changes to orange)
So the question is that how can i get focus on that 4 linear layout?(i set focusable & focusontouch & clickable value true of that linearlayout.)
I have done this, and setting android:clickable="true" on my LinearLayout did the trick. I just set a click handler for that layout when I set up my views.
From #david-lord's comment, only the parameter
focusableInTouchMode="true"
is necessary, so in XML
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
allows the call
linear_layout.requestFocus()
I have a LinearLayout with:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selectorbarbutton"
android:clickable="true"
android:focusable="true"
android:orientation="vertical" >
and this work getting focus and click events correctly.
Please try to set android:focusable=true ,
I am not sure about this but may solve your problem.
I have activity and a lot of widgets on it, some of them have animations and because of the animations some of the widgets are moving (translating) one over another. For example the text view is moving over some buttons . . .
Now the thing is I want the buttons to be always on the front. And when the textview is moving I want to move behind the buttons.
I can not achieve this I tried everything I know, and "bringToFront()" definitelly doesn't work.
note I do not want to control the z-order by the order of placing element to layout cause I simply can't :), the layout is complex and I can not place all the buttons at the begging of the layout
You can call bringToFront() on the view you want to get in the front
This is an example:
yourView.bringToFront();
With this code in xml
android:translationZ="90dp"
I've been looking through stack overflow to find a good answer and when i couldn't find one i went looking through the docs.
no one seems to have stumbled on this simple answer yet:
ViewCompat.setTranslationZ(view, translationZ);
default translation z is 0.0
An even simpler solution is to edit the XML of the activity. Use
android:translationZ=""
bringToFront() is the right way, but, NOTE that you must call bringToFront() and invalidate() method on highest-level view (under your root view), for e.g.:
Your view's hierarchy is:
-RelativeLayout
|--LinearLayout1
|------Button1
|------Button2
|------Button3
|--ImageView
|--LinearLayout2
|------Button4
|------Button5
|------Button6
So, when you animate back your buttons (1->6), your buttons will under (below) the ImageView. To bring it over (above) the ImageView you must call bringToFront() and invalidate() method on your LinearLayouts. Then it will work :)
**NOTE: Remember to set android:clipChildren="false" for your root layout or animate-view's gradparent_layout. Let's take a look at my real code:
.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hw="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/common_theme_color"
android:orientation="vertical" >
<com.binh.helloworld.customviews.HWActionBar
android:id="#+id/action_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_actionbar_height"
android:layout_alignParentTop="true"
hw:titleText="#string/app_name" >
</com.binh.helloworld.customviews.HWActionBar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/action_bar"
android:clipChildren="false" >
<LinearLayout
android:id="#+id/layout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<ImageView
android:id="#+id/imgv_main"
android:layout_width="#dimen/common_imgv_height"
android:layout_height="#dimen/common_imgv_height"
android:layout_centerInParent="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Some code in .java
private LinearLayout layoutTop, layoutBottom;
...
layoutTop = (LinearLayout) rootView.findViewById(R.id.layout_top);
layoutBottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom);
...
//when animate back
//dragedView is my layoutTop's child view (i added programmatically) (like buttons in above example)
dragedView.setVisibility(View.GONE);
layoutTop.bringToFront();
layoutTop.invalidate();
dragedView.startAnimation(animation); // TranslateAnimation
dragedView.setVisibility(View.VISIBLE);
GLuck!
Try FrameLayout, it gives you the possibility to put views one above another. You can create two LinearLayouts: one with the background views, and one with foreground views, and combine them using the FrameLayout. Hope this helps.
If you are using ConstraintLayout, just put the element after the other elements to make it on front than the others
i have faced the same problem.
the following solution have worked for me.
FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView);
glFrame.addView(yourView);
glFrame.bringToFront();
glFrame.invalidate();
2nd solution is by using xml adding this attribute to the view xml
android:translationZ=""
You can try to use the bringChildToFront, you can check if this documentation is helpful in the Android Developers page.
There can be another way which saves the day. Just init a new Dialog with desired layout and just show it. I need it for showing a loadingView over a DialogFragment and this was the only way I succeed.
Dialog topDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
topDialog.setContentView(R.layout.dialog_top);
topDialog.show();
bringToFront() might not work in some cases like mine. But content of dialog_top layout must override anything on the ui layer. But anyway, this is an ugly workaround.
You can use BindingAdapter like this:
#BindingAdapter("bringToFront")
public static void bringToFront(View view, Boolean flag) {
if (flag) {
view.bringToFront();
}
}
<ImageView
...
app:bringToFront="#{true}"/>
The order of the overlapping views really depends of 4 things:
The attribute android:elevation which is measured in dp/sp
The attribute android:translationZ which is also measured in dp/sp.
In Constraint Layout, the order in which you put the views in your Component Tree is also the order to be shown.
The programmatically order that you set through methods like view.bringToFront() in your kotlin/java code.
The numerals 1 and 2 compite with each other and take preference over the points 3 and 4: if you set elevation="4dp" for View 1 and translationZ="2dp" for View 2, View 1 will always be on top regardless of the numerals 3 and 4.
Thanks to Stack user over this explanation, I've got this working even on Android 4.1.1
((View)myView.getParent()).requestLayout();
myView.bringToFront();
On my dynamic use, for example, I did
public void onMyClick(View v)
{
((View)v.getParent()).requestLayout();
v.bringToFront();
}
And Bamm !
You can use elevation attribute if your minimum api level is 21. And you can reorder view to the bottom of other views to bring it to front. But if elevation of other views is higher, they will be on top of your view.
If you are using a LinearLayout you should call myView.bringToFront() and after you should call parentView.requestLayout() and parentView.invalidate() to force the parent to redraw with the new child order.
Arrange them in the order you wants to show. Suppose, you wanna show view 1 on top of view 2. Then write view 2 code then write view 1 code. If you cant does this ordering, then call bringToFront() to the root view of the layout you wants to bring in front.
Try to use app:srcCompat instead of android:src
You need to use framelayout. And the better way to do this is to make the view invisible when thay are not require. Also you need to set the position for each and every view,So that they will move according to there corresponding position
You can set visibility to false of other views.
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
...
or
view1.setVisibility(View.INVISIBLE);
view2.setVisibility(View.INVISIBLE);
...
and set
viewN.setVisibility(View.VISIBLE);