Android Listview OnClickListener not working - java

My problem is when i click list item in main layout some time it's triggered but not all time.
I don't know how to solve this problem?.
Please help me..
My code is:
View v = convertView.findViewById(R.id.chat_window_single);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("position",v.getTag().toString());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putLong("eventId", groupEventMoList.get(Integer.parseInt(v.getTag().toString())).getEventId());
editor.putString("eventTitle",groupEventMoList.get(Integer.parseInt(v.getTag().toString())).getText());
editor.commit();
Intent groupAct = new Intent(context, GroupChatActivity.class);
startActivity(groupAct);
}
});
My layout file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chat_window_single"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/chat_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:shadowDx="1"
android:shadowDy="1"
android:textColor="#android:color/primary_text_light"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/event_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:shadowDx="1"
android:shadowDy="1"
android:textColor="#android:color/primary_text_light"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/event_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:shadowDx="1"
android:shadowDy="1"
android:textColor="#android:color/primary_text_light"
android:textSize="20sp" >
</TextView>
</LinearLayout>
My main layout file :
<?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" >
<ListView
android:id="#+id/chat_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp" >
</ListView>
</LinearLayout>
Any Help would be appreciated.

Your textviews are stealing the focus from parent list view, make them clickable and focusable = false also the upermost linear layout needs android:descendantFocusability="blocksDescendants"

Related

Android Studio limited scrolling size

I'm currently developing an application on Android Studio, and I've an issue with a scrollview. I create buttons dynamically (with a number of buttons equal to the number of buttons in my Database), but I can't scroll to the bottom of the page and see every button. I don't know if this issue is caused by the ScrollView or by the LinearLayout inside the ScrollView. Here is the XML and the Java code for it :
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/layoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_list"
tools:context=".GroupList">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="134dp"
android:fontFamily="#font/cartoon"
android:text="Artists"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/back"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="68dp"
android:layout_marginStart="11dp"
android:layout_marginEnd="67dp"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/back2" />
<ScrollView
android:id="#+id/listG"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="1dp"
android:fillViewport="true"
android:layout_marginTop="10dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back">
<LinearLayout
android:id="#+id/layoutG"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Java (the loop that creates buttons dynamically) :
while(db.selectArtist(i).size()>0){
List<String> data = db.selectArtist(i);
Log.i("Artiste",String.valueOf(data));
Button bouton = new Button(getApplicationContext());
bouton.setX(160);
bouton.setY(1 * i + 60);
bouton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layoutG.addView(bouton,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
bouton.setText(data.get(0));
bouton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent start = new Intent(getApplicationContext(), start.class);
start.putExtra("NomArtiste", String.valueOf(bouton.getText()));
start.putExtra("IdArtiste", String.valueOf(data.get(1)));
startActivity(start);
finish();
}
});
i=i+1;
listB.add(i + 1);
}
Here is the result
image
Thanks for your help, it's my first post here, and I'm new in coding in Java!
You need to wrap the whole layout with the scroll view as wrapping just the linearlayout (in context of your code) will make the layout scrollable but it wont include its child views fully.
consider using the following layout: (tweak some values to your comfort)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/your-desired-view-name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_marginTop="10dp"
android:background="your-desired-background-here"
android:fillViewport="true"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back"
tools:context="your-desired-context-here">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="134dp"
android:fontFamily="#font/cartoon"
android:text="Artists"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/back"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="68dp"
android:layout_marginStart="11dp"
android:layout_marginEnd="67dp"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/back2" />
<LinearLayout
android:id="#+id/layoutG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Stop android scrollview prevent parent LinearLayout onclick

I need to allow user to CLICK anywhere on the screen, so I've implemented onClick listener for mainLayout and it worked great.
But the problem is I've got to add a scrollview inside mainLayout to support small screen sizes and after adding scrollview, it blocks the parentLayout (in my case mainLayout) CLICK EVENT.
Just to exaplin this, a sample layout is as follows.
<!-- MAIN LAYOUT WHERE I NEED
THE CLICK EVENT-->
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
android:onClick="onClick_MainLayout">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
I searched for this and found various ideas like disableing onClick manually in all other views inside scrollview etc but non of them works.
NOTE: I tried both implementing onClick listener manually in java and adding android:onClick="onClick_MainLayout" in to layout. Both the same.
EDITS ----------------------
All the changes I've done as you (#IntelliJ Amiya) mentioned is as below. Please let me know anything is missing.
<RelativeLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"/>
<Button
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"/>
<TextView
android:id="#+id/textview1"
android:layout_below="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And in on create
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("MyLogs", "Click Event Fires...");
}
});
Your Scroll view overlaps the linear layout i.e #mainLayout,so
set its height to wrap content
then you can perform onclick for both scroll view buttons and mainlayout.
Pls tell me if i wrong.
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 1"
android:onClick="onClick_Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="TEST BUTTON 2"
android:onClick="onClick_Button2"/>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_Button1(View v)
{
Toast.makeText(this, "1ST Button Click", Toast.LENGTH_SHORT).show();
}
public void onClick_Button2(View v)
{
Toast.makeText(this, "2ND Button Click", Toast.LENGTH_SHORT).show();
}
}

Android List onListItemClick not working

I've got a new List of things that needs to be clicked but this one isn't working. onListItemClick is never called. I have another one in my app that has been working as expected and I can't figure out what the difference is. I've seen the people saying to change focusable but I've tried that a bunch of different ways with no effect. So here's some code.
Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
position);
Intent myIntent = new Intent(this, Details.class);
myIntent.putExtra(res.getString(R.string.project), project);
startActivity(myIntent);
}// onListItemClick
Not Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
position);
AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
.setMessage(res.getString(R.string.share_check_dialog_text))
.setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
sendShareEmail(vSitePeople);
}
}).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancelled, so do nothing
}
});
AlertDialog msgBox = builder.create();
msgBox.show();
}// onListItemClick
Working 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:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/app_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search_gradient" >
<ImageView
android:id="#+id/searchBoxIcon"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop"
android:src="#drawable/action_search" >
</ImageView>
<EditText
android:id="#+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="#+id/searchBoxIcon"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/searchBoxIcon"
android:background="#drawable/search_box"
android:hint="#string/search_hint"
android:inputType="text"
android:maxLines="1"
android:minHeight="30sp"
android:paddingBottom="2dp"
android:paddingLeft="25sp"
android:paddingTop="2dp"
android:textColor="#ff000000" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Not Working 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:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/share_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Here is more on how the broken one works, just in case you want more code.
Not Working Row XML
<?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="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="6dp"
android:background="#ffffffff"
android:layout_margin="10dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/moreInfo"
android:gravity="center_vertical"
android:minHeight="20sp"
android:textColor="#ff000000"
android:textStyle="bold" />
</RelativeLayout>
Not Working View Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.share_row, null);
convertView.setTag(holder);
}// if
else
{
holder = (ViewHolder) convertView.getTag();
}
v_SitePeople i = items.get(position);
if (i != null)
{
TextView topText = (TextView) convertView.findViewById(R.id.toptext);
topText.setGravity(Gravity.CENTER_VERTICAL);
topText.setMinHeight(40);
if (topText != null)
{
if (i.SitePerson != null)
{
if (i.PersonTitle != null)
{
topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle));
}
else
{
topText.setText(i.SitePerson);
}
}// if has ProtocolNumber
else
{
if (i.Nickname != null)
{
topText.setText(i.Nickname);
}
}// if does not have ProtocolNumber
}// if
}// if
return convertView;
}// getView
Thank you so much for your help.
I figured it out. I never posted the relavant code, or I'm sure you guys would have found it for me.
My not working adapter had a isEnabled method copied from my functioning adapter that disabled items of type 0. But since my new list is all just one type everything was disabled.
Sorry for the trouble and thank you for your efforts.
You haven't shown where you initialise your list and adapter, but you need to make sure the onClickListner is set. Something like:
myListView.setOnItemClickListener(this);
edit: 'this' assuming initialization is done in the same class as the click listeners appear.
When you're using a ListView, you have mainly two layouts:
1)The layout where you have your listView
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="#dimen/list_left_padding"
android:paddingRight="#dimen/list_right_padding"
android:paddingTop="#dimen/list_separator"
android:dividerHeight="#dimen/list_separator"
android:paddingBottom="#dimen/list_separator" />
</RelativeLayout>
2)The layout which contains each item for your ListView.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="#+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
If you search on StackOverFlow about this problem, you will got the following instructions to solve it:
1) Add
android:descendantFocusability="blocksDescendants"
to the root of your layout
2) Add
android:clickable="false"
android:focusable="false"
To your item.
You can use these two options separately. The first one have helped me. But, you need to pay attention on where you need to put these attributes in your XML File. Put them in your ITEM layout, not in your ListView Layout. I'm saying this, because I was using that solution, but i was putting on the wrong xml. So, it just pay attention and it will work very well.

android-wants to put Button above listview

I am using linear layout inside that one button and one list view..
list-view is adding its item dynamically at the run-time..
so at that time button goes behind list-view and not visible and list-view fit in whole layout.
I want to put Button above the List view and it must also stay after loading list-view
Thanks in Advance
my .xml file is:
<?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"
android:background="#000000">
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_gravity="left"
android:background="#000000"
android:textColor="#FFFFFF"
android:text="Search New Devices"
/>
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
</ListView>
</LinearLayout>
here it is search new device button.
click on that button device search for Bluetooth and add into listview
so this is thing happen in my application..
I think even this works.Give a try
edited...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:text="Search New Devices"
android:textColor="#000000" />
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btn_searchNewDevices"
android:scrollbars="horizontal">
</ListView>
</RelativeLayout>
and the activity is as follows .
public class MainActivity extends Activity implements OnClickListener {
ArrayAdapter<String> adapter;
private Button mBTNSearch;
private static ListView mLV_list;
ArrayList<String> devicesList;
SparseBooleanArray checked;
RelativeLayout layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBTNSearch = (Button) findViewById(R.id.btn_searchNewDevices);
devicesList = new ArrayList<String>();
mLV_list = (ListView) findViewById(R.id.lst_add_newDevices);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, devicesList);
mBTNSearch.setOnClickListener(this);
mLV_list.setAdapter(adapter);
}
public void onClick(View v) {
adapter.add("new Device");
adapter.notifyDataSetChanged();
}
}
The output capture is as below
Try using android:layout_weight="1" in the listview's height to that you make sure your button will always be seen in the screen.
You should try:
<?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:background="#000000"
android:orientation="vertical" >
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:background="#000000"
android:text="Search New Devices"
android:textColor="#FFFFFF" />
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="horizontal" >
</ListView>
</LinearLayout>
Or you can use ListView's addHeaderView() method. Here is a tutorial for that.
Try this code for your Layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
Try putting your listview inside a scrollview.
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:background="#000000"
android:text="Search New Devices"
android:textColor="#FFFFFF" />
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="7dip"
android:scrollbars="none" >
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
</ListView>
</ScrollView>
It might work. I haven't tested, so not sure.

static header with dynamic list view troubles

Similar to the contacts view on the androids contacts section, which has the search bar on top with a dynamic list view below it, example here, I am trying to have a spinner instead of a text field.
My problem is that in the layout, the Spinner repeats per list view item.
I have the following list.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sc" />
<TextView
android:id="#+id/rowData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
and the follow Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
application = ((MyAppApplication) getApplication());
populateOffers();
}
private void populateOffers()
{
MyAppRequestor MyApp = new MyAppRequestor();
ArrayList<String> list = MyApp.items(application.getMyAppId());
adapter = new ArrayAdapter<String>(this,
R.layout.list, R.id.rowData, list);
setListAdapter(adapter);
}
Since you are using list.xml as your layout it will be repeated.. If you want spinner only once, then you have to put in the main layout (Layout where you have defined the list view as a component.
try this list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bgd" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sc" />
<TextView
android:id="#+id/rowData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Categories

Resources