Listview demand focus when added to a layout - android

I want to add a ListView to a LinearLayout. But when the app start it demand focus and open the keyboard. I don't want it to do that.
I have tried both XML and Java code:
contentLayout = (LinearLayout)findViewById(R.id.contentView);
contentList = new ListView(this);
contentList.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
contentLayout.addView(contentList);
XML
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>

I think that I ask the question a little wrong.
But I find out that since I have an input field, the system always want to focus in an input. So I set in the Android Manifest that windowSoftInputMode to stateHidden.
Know the keyboard doesn't open automically when the activity start.

Related

OnClick Listener not working for buttons which exists in a linearlayout for which the alignParentBottom is true

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.

How to prevent lifting listview when keyboard goes up

This is code MainActivity java and main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#75F575">
<LinearLayout
android:clickable="true"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#C69817"
android:id="#+id/secondLayout">
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/mainListView1"/>
</LinearLayout>
</LinearLayout>
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,
new String [] {"item1","item2"});
lv.setAdapter(a);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
When keyboard is shown, listview lifting up.How to prevent this.
Without listview - all normal(layout NOT go up).
Thank you.
UPDATE QUESTION with answer to FOliveira.
Unfortunately i can't remove java code (setSoftInputMode) in my real app. Java code must be and layout with listview must NOT GO UP. Try to remove listview, and you will see the layout not moving absolutely, Why layout with listview is moving? How prevent this according my conditions?
You need to add android:windowSoftInputMode="adjustPan" to your tag in the AndroidManifest.xml file.
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
If this option does not fit your needs, you can allways check Android documention about soft input mode
I'm using android:windowSoftInputMode="stateVisible|adjustNothing" , but this only for API higher 10(unfortunately).
Update:
After adding to listview this attributes:
android:focusableInTouchMode="false"
android:isScrollContainer="false"
I'm completely solved my problem.

How to hide keyboard in a edittext + listview layout

I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:
<?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" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Filtrar por nombre..." >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.
I've tried several methods such as writing the following in the manifest
android:windowSoftInputMode="stateHidden"
and including this in the class
InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.
Any posible solution for this?
EDIT:
I've tried the following solutions(thanks to #Sush, #boztalay and #Agata Sworowska), but none did the trick.
-Placed in my layout:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
-And placed in my onCreate() method:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
Any other posibility?
NEW EDIT:
I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.
This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.
In your Java, you should get an instance of the ListView, then call a couple of functions:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.
See this SO question for more details.
You can just add below two attributes to your LinearLayout.
android:focusable="true"
android:focusableInTouchMode="true"
Try adding in Your layout, that contains EditText this:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
I hope it helps.
http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html
This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.

Adding new widget controls dynamically to an existing layout

i've seen this question added on the stack but the information hasn't been helpful or successful yet, so i remain quite not sure.
The gist of what I'm trying to do:
I have a layout defined in xml, some_details_view.xml for example.
setContentView(R.layout.some_details_view);
It has a bunch of text views laid out using a parent relative layout, a linear header layout, a linear footer layout, a middle scroll layout that contains a relative layout that hold some label - value type of text views.
And at the bottom of the scroll view relative layout, I currently placed a frame layout as a place holder.
On create of the corresponding activity, I set text in respective text views with data handed over from previous activity; basic stuff.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<LinearLayout
android:id="#+id/header"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
...some header content
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
..some footer content
</LinearLayout>
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/footer"
android:layout_below="#+id/header"
android:layout_margin="5dip" >
<RelativeLayout
android:layout_width="fill_parent"
android:id="#+id/relativeScroll"
android:layout_height="wrap_content" >
...text views in relative layout
<FrameLayout
android:id="#+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/moreInfoValue" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
After setting up text views for the given data, I use an async task to get some additional data that I want to show as a history list type of thing at the bottom of the static form layout. There could be 0 or more items so I either add 1 or more text views or none at all.
In the post execute, which I understand to run on the main UI thread, I try to find an exiting container Layout/view group and add either a new Linear Layout to which I add new text Views, or just add the new text views directly to the existing container layout.
here's the latest thing I tried:
ViewGroup mContainer = null; //defined as member variable of activity class and instatiated in on create
mContainer = (ViewGroup)findViewById(R.id.placeholder); //set in on create
LinearLayout ll = new LinearLayout(context); //on post execute of async task
ll.setOrientation(LinearLayout.VERTICAL);
mContainer.addView(ll); //add a new linear layout to an existing container layout
//add some new text view widgets items dynamically
for(NewDisplayItem item : items)
{
TextView tv = new TextView(context);
tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
tv.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
tv.setText(item.getSomeText());
ll.addView(tv); //add text view to new linear layout
}
When the UI loads I don't see new items added to my layout after stepping through and watching the controls get added using the code above.
Not sure what it is but something doesn't seem right about this approach in addition to the fact that it's not working. When the activity loads up, all the static views are setup and in view. I pop up a loading dialog, step through the async task stuff and I guess expect to see the dynamic controls add to the layout one by one?
First of all textView.setWidth(int) takes as parameter the width in pixels.
Second you should also set your layout parameters on the LinearLayout you are adding.
The way you should set LayoutParams is as follows :
ll.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
the same for yout TextViews.
Ovidiu Latcu has a good answer. A good question is: is there a reason why you aren't using a ListView (which btw there ARE cases where what he's doing works better)? A ListView has a lot of mechanisms to help you keep from running out of RAM

How Linear Layout get Focus?

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.

Categories

Resources