Why table layout item cann't be selected using the touch?

Put your problem here if it does not fit any of the other categories.

Why table layout item cann't be selected using the touch?

Postby x-files » Fri Jan 11, 2008 3:52 pm

1) create scrollView
ScrollView scrollView = ...

2) create scrollViewItem
TableLayout tableItem = new TableLayout(context);
... add content

3) add item to scrollView


Why table layout item can not be selected using the touch screen? … only the navigation keys...
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Top

Postby plusminus » Fri Jan 11, 2008 4:19 pm

Hello x-files,

do you receive the same problem without ScrollView wrapped around :?:

Within the :src: Christmas-Calendar-Tutorial, I can ensure, that the onClickListener of Views within the TableLayout is working properly!

Perhaps provide a bit of your code.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby x-files » Fri Jan 11, 2008 4:31 pm

layout xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <ScrollView id="@+id/scroll_view"
  2.  
  3.             android:layout_width="fill_parent"
  4.  
  5.         android:layout_height="wrap_content"
  6.  
  7.         android:layout_weight="1.0">
  8.  
  9.                 <LinearLayout id="@+id/linear_layout"
  10.  
  11.                 android:orientation="vertical"
  12.  
  13.                 android:layout_width="fill_parent"
  14.  
  15.                 android:layout_height="wrap_content"/>
  16.  
  17.         </ScrollView>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SelectOneView extends Activity {
  2.  
  3.         private final String[] ITEMS = {"One", "Two", "Three"};
  4.  
  5.         private LinearLayout linearLayout;
  6.  
  7.        
  8.  
  9.     @Override
  10.  
  11.     public void onCreate(Bundle icicle) {
  12.  
  13.         super.onCreate(icicle);
  14.  
  15.                 setContentView(R.layout.scroll_view);
  16.  
  17.                 linearLayout = (LinearLayout)findViewById(R.id.linear_layout);
  18.  
  19.                 populate(linearLayout, ITEMS);
  20.  
  21.     }
  22.  
  23.    
  24.  
  25.     private void populate(LinearLayout layout, String[] lines) {
  26.  
  27.         for(int i = 0, count = lines.length, selImgId, deselImgId; i < count; i++) {
  28.  
  29.                 //selImgId = ..
  30.  
  31.                 //deselImgId = ...
  32.  
  33.                 SelectItemView view = new SelectItemView(this, lines[i], selImgId, deselImgId);
  34.  
  35.                 view.setOnClickListener(ITEM_SELECT_LISTENER);
  36.  
  37.                 linearLayout.addView(view, new LinearLayout.LayoutParams(WC, WC));
  38.  
  39.         }
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.     private final OnClickListener ITEM_SELECT_LISTENER = new OnClickListener() {
  46.  
  47.         public void onClick(View v) {
  48.  
  49.             //bussiness logic
  50.  
  51.         }
  52.  
  53.     };
  54.  
  55. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


SelectItemView:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SelectItemView extends TableLayout
  2.  
  3.                                                         implements OnFocusChangeListener{
  4.  
  5.         private ImageView checkedIcon;
  6.  
  7.         private int selImgId, deselImgId;
  8.  
  9.                
  10.  
  11.         public SelectItemView (Context context, String text, int selImgId, int deselImgId) {
  12.  
  13.                 super(context);
  14.  
  15.                
  16.  
  17.                 this.selImgId = selImgId;
  18.  
  19.                 this.deselImgId = deselImgId;
  20.  
  21.                
  22.  
  23.                 setFocusable(true);
  24.  
  25.                 setOrientation(HORIZONTAL);
  26.  
  27.                 setColumnStretchable(1, true);
  28.  
  29.                 setColumnShrinkable(1, true);
  30.  
  31.                 setOnFocusChangeListener(this);
  32.  
  33.                 onFocusChanged(null, false);
  34.  
  35.                
  36.  
  37.                 TextView label = new TextView(getContext());
  38.  
  39.                 label.setText(text);
  40.  
  41.                 label.setWidth(200);
  42.  
  43.                
  44.  
  45.                 checkedIcon = new ImageView(getContext());
  46.  
  47.                 checkedIcon.setImageResource(R.drawable.al_sl);
  48.  
  49.                 checkedIcon.setVisibility(INVISIBLE);
  50.  
  51.  
  52.  
  53.                 TableRow row = new TableRow(getContext());
  54.  
  55.                 row.setBaselineAligned(false);
  56.  
  57.                 row.setVerticalGravity(Gravity.CENTER_VERTICAL);
  58.  
  59.                
  60.  
  61.                 row.addView(label);
  62.  
  63.                 row.addView(checkedIcon);
  64.  
  65.                 addView(row, createParam(FP, WC));
  66.  
  67.         }
  68.  
  69.        
  70.  
  71.        
  72.  
  73.         private TableLayout.LayoutParams createParam(int w, int h){
  74.  
  75.           return new TableLayout.LayoutParams(w, h);
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.         /**
  82.  
  83.          * @see OnFocusChangeListener#onFocusChanged(View, boolean)
  84.  
  85.          */
  86.  
  87.         public void onFocusChanged(View view, boolean focus) {
  88.  
  89.         if (focus) {
  90.  
  91.                 setBackground(selImgId);
  92.  
  93.         } else {
  94.  
  95.                 setBackground(deselImgId);
  96.  
  97.         }
  98.  
  99.         }
  100.  
  101.        
  102.  
  103. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Postby plusminus » Fri Jan 11, 2008 5:57 pm

Hello x-files,

I tried various possibilities and OnClickListeners on every single View you produce, but no one :!: gets called when hitting the specific view with the mouse.

:idea: Only if you use the Button-Class :!:

So this one line makes it "work". Unfortunately a dirty workaround :!:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button label = new Button(context);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


btw: You can make the Button appear not "as a Button", but as you can see in the :src: Christmas-Calendar-Tutorial also transparent and stuff.

How this helped you at least a bit...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby x-files » Fri Jan 11, 2008 10:36 pm

Thanks you for investigation!
But i need to add item to scrolView where item consist of label and icon.
Label should be left aligned.
Icon should be right aligned.
Therefore i used tableLayout.

In other words if scrollView item contain diferent view then it's difficult layout they with help of button.
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Postby plusminus » Fri Jan 11, 2008 11:04 pm

Hello x-files,

Did you check the :src: IconifiedList-Tutorial :?:
The List-Entries there are clickable with the mouse
It contains a IconifiedTextView which itself consists of two more Views, ImageView and a TextView. Also Adapter (custom created IconifiedTextListAdapter).

Putting it into a ScrollView should work too.

So I assume it would fit your needs (with some customization) :?: :!: :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby x-files » Sat Jan 12, 2008 1:33 am

Hello, plusminus.

>>>Also Adapter (custom created IconifiedTextListAdapter).

>>>Putting it into a ScrollView should work too.

ScrollView don't use item Adapter approach.
How can i put ItemAdapter in ScrollView?

Thanks!
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Postby plusminus » Sat Jan 12, 2008 1:52 am

Hello x-files,

I've not yet used ScrollView, but I was assuming it would be like the JScrollPane from Swing, where you can put child-Views into it.

As it extends FrameLayout it is probably capable of doing the same (untested).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby x-files » Sat Jan 12, 2008 8:58 am

Hello, plusminus.


Yes, I can add child-Views into scrollView. But the problem is that if the child-View isn't a button then i cann't select it with mouse.

Yes, you are right, this problem solved in listView. But I cann't use ListView because this one to prevent to change appearance of selection cursor. Please, see image snapshot in attachment.
Attachments
imgs.zip
ListView and ScrollView
(16.26 KiB) Downloaded 141 times
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Postby plusminus » Mon Jan 14, 2008 11:58 pm

Hello x-files,

the "wrong" variant looks pretty strange, because the selection-bar is behind! the actual list.
This looks like an API-Bug or like a Bug in your code...

Please provide the important lines, so we can have a look.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby x-files » Tue Jan 15, 2008 12:35 pm

I found workarround

need overridde TableLayoyt#onMotionEvent

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onMotionEvent(MotionEvent event) {
  2.  
  3.                 boolean res = super.onMotionEvent(event);
  4.  
  5.                 if(event.getAction() == MotionEvent.ACTION_DOWN) {
  6.  
  7.                     performClick();
  8.  
  9.                 }      
  10.  
  11.                 return res;
  12.  
  13.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests