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.
Related
I am building a app and I just need some help with the attached screen shot.
Currently I have 5 Plain Textfields with the last 2 being textMultilines. I have a Relativelayout with a button placed at the bottom for the user to press submit. The problem is when the textViews above are filled in, especially the multiline ones, it will push the Relativelayout and button off the screen.
How can I stop this from happening
Picture of layout
Either You should fix the height of multi lines text or you can simply use Scroll View and put all the text view and button in same relative layout
You can make them Scrollable by adding ScrollView around them.
like
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//all your views here
</LinearLayout>
</ScrollView>
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.
Simple question, i have this code
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
...
<LinearLayout
android:id="#+id/insideLinearLayout1"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
<LinearLayout
android:id="#+id/insideLinearLayout2"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
</LinearLayout>
</HorizontalScrollView>
The idea, is to change the textField text property, when clicking the button on the same layout!
Ok for 2 is easy, just need the reference of each button and each textfield and change use the get and set method!
But i want to add more layouts dynamically, and grab each one's reference would be a hard task.
So two questions :
How can i grab the id of the Linear layout, that have the clicked button?
I want to handle the 'insideLinearLayout(1 or 2 depending on the clicked button)'id in the 'updateExpression'!
How can i add more layouts with the same widgets as the ones created manually?
Thank you in advance.
Best of codings!
1)Your onClick function is passed the view that was clicked. Every view has a getParent() function. You can use it to get the LinearLayout, then get the id.
2)Create them with the new keyword then add them to the parent layout. For something like this I would probably make a custom compund view holding everything you want to instantiate at once, so you can treat the entire set of widgets as one.
I hava a problem that can be presented like this : I have a RelativeLayout filling the screen and inside it a Button. Under this layout I have an other Button (this is like a layer structure).
<Button android:layout_marginLeft="133dp" id="button_single"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
<Button id="button_inside"/>
</RelativeLayout>
When I keep pressed the button button_inside it gives the focus to the layout so the button_single is no more clickable.
How can I disable the layout focusable ability ?
Thank you in advance
NOTE : in reality, in my project, the button_single is a GLSurfaceView filling the screen but the problem is the same.
add
android:focusable="true" or "false"
in the xml file at your component
Quick question: at runtime I do a boolean check, if it returns true I would like to have two buttons in a relative layout on my MainActivity class. If its false I want to instead have two other widgets where those buttons would be (or near enough). How do I do that?
you could also implement a ViewSwitcher where a more complicated set of buttons/widgets can be switched out very easily with a single call to
ViewSwitcher mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
if (some_logic == true) {
mViewSwitcher.showNext();
}
Set up your XML like this and the above will switch between the two LinearLayouts:
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
If you have just those two alternatives put them both in your layout and hide / show the one you want. View#setVisibility()
If you want it more dynamic you can add and remove widgets programmatically. ViewGroup#addView()
Modifying a RelativeLayout during runtime is quite complicated since you need to set all those layout parameters so you could add a simple layout like a FrameLayout in the place where the buttons should go and put them inside the frame. Has the advantage that you can setup all the relative layout parameters for the frame in xml.