I created a list view with a custom list row. That list row contains a image view with four imagebuttons:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:padding="2dip"
android:id="#+id/list_image"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/test"
android:scaleType="centerCrop"/>
<LinearLayout
android:paddingTop="5dip"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/list_image"
android:layout_alignRight="#+id/list_image"
android:layout_below="#+id/list_image"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/copy"
android:background="#color/white"
/>
<ImageButton
android:id="#+id/visitHP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/website"
android:background="#color/white" />
<ImageButton
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/delete"
android:background="#color/white" />
<ImageButton
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_social_share"
android:background="#color/white" />
</LinearLayout>
As a pic:
Now i would like to add to each of the buttons a onclicklistener where i can get the index it of the row or the object of the row.
I tried to implement the OnClickListener to my ArrayAdapter:
ImageButton btn = (ImageButton) convertView.findViewById(R.id.delete);
btn.setOnClickListener(this);
public void onclick(View v) {
int img = v.getId();
}
But with this i wont get the index of the row.
How should i approach this problem ? Thanks
You can use button.setTag() in getView()
And v.getTag() in onClick()
it works in this way:
public void onclick(View v) {
int id = v.getId();
switch(id) {
case R.id.share: .... break;
case R.id.delete:
...
}
Related
I am making a gridlayout which contains multiple textviews. The xml code for the same is:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignMargins"
android:background="#android:color/white"
android:columnCount="1"
android:columnOrderPreserved="false"
android:rowCount="1">
<LinearLayout
android:id="#+id/row1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/white"
android:weightSum="5">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
</LinearLayout>
</GridLayout>
As you can see in this, for each of the textviews I have defined the onClick method. The click is handled like:
public void onClick(View v) {
if (v instanceof TextView && isInitiatePhase) {
((TextView) v).setText(String.valueOf(++count));
if (count == 5) {
isInitiatePhase = false;
}
} else if (v instanceof TextView) {
// now we have to start cutting/setting the color to red
((TextView) v).setTextColor(Color.RED);
}
}
The initiation is handled fine, now after the initiation phase when I am clicking the TextViews I want to know which one is clicked. Is there a way to find something like an index of the TextView in the onClick() method?
Does LinearLayout provide that functionality, if the TextView does not?
Also, is my approach correct here? Or is there another Layout that I should use for the same?
Instead on adding onClick, we can add onClickListener in each child view inside linearlayout of gridlayout. Something like this,
//Add gridlayout id in xml
GridLayout girdLayout = (GridLayout) findViewById(R.id.gridlayout_id);
final LinearLayout ll = gridLayout.findViewById(R.id.row1);
int childCount = ll.getChildCount();
for (final int i= 0; i < childCount; i++){
TextView textView = (TextView) ll.getChildAt(i);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
// Do your job, i current index of child
}
});
}
I need to detect the onclick event on a imageview, and get the position to get the row from a cursor, and I don't know how.
I'm trying to modify https://github.com/Odoo-mobile/framework to and shopping cart app.
The code of the layout view where I need to detect the click event is:
<?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"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="2dip" >
<odoo.controls.BezelImageView
android:id="#+id/image_small"
android:layout_width="40dp"
android:layout_margin="#dimen/default_8dp"
app:maskDrawable="#drawable/circle_mask"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/thumbnail"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/body_text_1"/>
<LinearLayout
android:id="#+id/cart_plus_minus_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/name"
android:orientation="horizontal" >
<TextView
android:id="#+id/list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="60dip"
android:layout_weight="0.23"
android:paddingRight="5dip"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/android_red" />
<ImageView
android:id="#+id/cart_minus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_minuscircle" />
<TextView
android:id="#+id/cart_product_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:text="0"
android:textSize="18dip"
android:textStyle="bold" />
<ImageView
android:id="#+id/cart_plus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_addcircle" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
the images I need to detect the the click are: #+id/cart_plus_img #+id/cart_minus_img
When the row is click the function that capture the event is:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ODataRow row = OCursorUtils.toDatarow((Cursor) mAdapter.getItem(position));
//AddToCart(row);
loadActivity(row);
}
What I need is to call loadActivity(row) when the user click the row but when click #+id/cart_plus_img #+id/cart_minus_img call another function but with the row parameter
Any help is thanksfull
Use the Tag property of the View
ImageView imgMinus = (ImageView)view.findViewById(R.id.cart_minus_img);
ImageView imgPlus = (ImageView)view.findViewById(R.id.cart_plus_img);
imgMinus.setTag(position);
imgPlus.setTag(position);
imgMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
imgPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
i am using below xml for my ListView item layout
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to LinearLayout
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!
In a layout I have a button which toggles view's visibility (hides/displays a linear layout). Inside this layout I also have a spinner. When I click the button it toggles the visibility but it does not change the layout height to wrap_content, but when I change the spinner value it updates the height.
This is the XML Layout code
<LinearLayout
android:id="#+id/layoutA1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvAlarmHeading"
android:layout_margin="5dip"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/transparent_border"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutA1C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnParameterA1C1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnAddA1C1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="+"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutA1C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnConditionA1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnRemoveA1C2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="-"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is the activity code where I toggle the views visibility
//Set the on item selected listener on the parameter spinner for alarm1 condition 1
spnParameterA1C1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
{
tvUnitA1C1.setText(unitList.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
//Set onClickListener for add condition button for Alarm 1
btnAddA1C1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setVisibility(android.view.View.VISIBLE);
btnAddA1C1.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
//Set onClickListener for add condition button for Alarm 1
btnRemoveA1C2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = 0;
btnAddA1C1.setVisibility(android.view.View.VISIBLE);
layoutA1C2.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
I have also tried layoutA1.refreshDrawableState();. Can anybody tell me which method should I call to refresh the layout length?
You need to set the layout params again in order to refresh the view.
try :
LayoutParams params = layoutA1C2.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setLayoutParams(params); // this call is what you need to add
I have a problem with setting gravity of a list item layout. I have a list layout which has 2 textviews. The first one has right gravity and second one has left gravity. When I load list view I always get right gravity.
Here is my 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" >
<TextView
android:id="#+id/poem_verse_list_item_01"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" />
<TextView
android:id="#+id/poem_verse_list_item_02"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left" />
</LinearLayout>
And here is my Cursor and its ViewBinder:
public class CustomViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int getIndex = cursor.getColumnIndex(ClubCP.COLUMN_POSITION);
int position = cursor.getInt(getIndex);
// Log.i("ViewBinder position",position);
TextView right =(TextView)view;
int viewId = view.getId();
switch(position){
case 0 :
// Right = 0,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.RIGHT);
// text.setVisibility(text.GONE);
// return true;
case 1:
// Left = 1,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.LEFT);
// return true;
case 2:
case 3:
case 4:
case -1:
}
return false;
}
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.poem_verse_list_item, c,
columns, new int[] {R.id.poem_verse_list_item_01,R.id.poem_verse_list_item_02}, 0);
I tested a lot but each time I get only right or left gravity for both of them... I hope some one can help me. Thanks for your time.
This is my activity including ListView layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff64512f"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".Read_peom" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_repeat" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scaleType="fitXY"
android:src="#drawable/list_bottom_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/list_left_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="#drawable/list_right_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/list_top_right_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/list_bottom_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/list_bottom_right_no_repeat" />
<TextView
android:id="#+id/txt_poem_container_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#+id/list_poem_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_margin="#dimen/list_top_margin"
android:choiceMode="none"
android:clickable="false"
android:longClickable="false"
android:padding="5dp" >
</ListView>
Remove android:layout_gravity from both the TextViews since android:layout_width="match_parent"
OR
make android:layout_width="wrap_content" and remove android:gravity from both the TextViews.
ELSE
You may also try a RelativeLayout with TextViews having android:layout_width="wrap_content" and android:layout_alignParentLeft or android:layout_alignParentRight set to true accordingly.
Edit:
To debug the issue do the following. Create a ArrayAdapter with static data and set it as the list adapter.
String[] arr = {"a","b","c"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.poem_verse_list_item, R.id.poem_verse_list_item_01, arr);
setListAdapter(adapter);
}
Then set some value using android:text to the TextViews like this.
<TextView
android:id="#+id/poem_verse_list_item_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="poem_verse_list_item_01" />
<TextView
android:id="#+id/poem_verse_list_item_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="poem_verse_list_item_02"/>
So if the problem persists then the issue is not with the view binder. I would suggest you to redesign your main layout file containing the ListView.
Try change android:orientation="vertical"
to
android:orientation="horizontal"
Hope this helped you