Getting nested item on onClick of ListView - android

I have row_item.xml with nestem LinearLayout and multiple ImageViews. It generates 2 x 2 image grid in each row of ListView. On onClick aI want to know on which Image item user has clicked.
In onClick method I have tried to see which item is actually coming using item.getClass() and it shows LinearLayout. I am sure this will be top level layout in row Item. Here is my row item skeleton layout. I am looking for some generic approach where in I can get hold of clicked item at any depth.
<LinearLayout>
<LinearLayout>
<ImageView> </ImageView>
<ImageView> </ImageView>
</LinearLayout>
<LinearLayout>
<ImageView> </ImageView>
<ImageView> </ImageView>
</LinearLayout>
</LinearLayout>

You can set your image views clickable by setting appropriate options in xml where you declare your listView. And then just add onclickListeners to your imageViews.
For me solution that worked was setting:
clickable
focusable
focusableInTouchMode
to true. If you have this done, you can in your adapter getView/bindView setonClickListeners to your imageviews.

For each ImageView add the following:
android:clickable="true"
android:onClick="myOnClickFunction"
and add code like:
public void myOnClickFunction(final View imageView) {
// imageView.getId() - actual image view id.
}

Related

How do i put multiple images(ImageView) in a ScrollView?

i want to put multiple images and scroll down and see them all. I tried to put a ScrollView and inside a ImageView but i dont know if i need to put a layout. I searched tutorials but i didnt found what i needed.
That's clearly not how ScrollViews work. ScrollView is a ViewGroup, so you have to put it outside the view that you want to scroll.
Also, there can only be one view as the direct child of scroll view. So you cannot do this:
<ScrollView>
<ImageView>
<ImageView>
<ImageView>
</ScrollView>
Actually, you should put the images in a LinearLayout, and put the LinearLayout in the scroll view.
<ScrollView>
<LinearLayout>
<ImageView>
<ImageView>
<ImageView>
</LinearLayout>
</ScrollView>
Note: the attributes are omitted because I'm lazy :P
EDIT:
If you want to scroll down, just set the orientation attribute to "vertical".
<ScrollView>
<LinearLayout
android:orientation="vertical"
other attributes.../>
image views here...
</LinearLayout
</ScrollView>
Check out the Android documentation for CardViews http://developer.android.com/training/material/lists-cards.html
You can put an image within each CardView element and a sharebutton below the image. As I previously said, good memory management is a must when working with images and the RecyclerView will work just well.
Cheers
Try this method
<ScrollView>
<LinearLayout>
<ImageView>
<ImageView>
<ImageView>
<ImageView>
<ImageView>
</LinearLayout>
</ScrollView>

Android: How to make Grid view with clickable grid items and nested views (buttons,Image) by remote(For Android TV)

How to make Grid view with click able grid items and nested views (buttons,Image) by remote(For Android TV)
I just want to click able image and button in grid view item should add to play my list if I navigate on button by Remote navigation not by touch
Thank you!!!
How to navigate through d pad in grid view sub items if grid view item have two sub items image button and another button
It is no difference between touch screen or remote control for methods like onClickListener or etc.
For remote control, you need to focus on the element first, than you can click the element.
I have not use the gridView on TV before, but I have used the recyclerView and scrollView. I think they have same situation.
I've try the default simpleAdapter to create the gridView, and it's no problem with the nested views that I can focus and click each grid items after I set the onItemClickListener to the gridView.
I guest your problem is that the grid item conflicts with the button in the grid item.
To avoid this situation, you need to set android:descendantFocusability="blocksDescendants" for the container of your custom grid item view. It will block the items statement in that parent layout.
If you want the button change in the different statement, you can give the button this parameter: android:duplicateParentState="true".
That will let the button follow it's parent's statement.
By the way, don't forget to set some changes to different statement for the grid items (background changes or etc), or you will not know which grid item you have focused :P.
Hope this will help you :)
PS. The following is the sample code for the custom grid item layout. Maybe the example can help you to understand what I mentioned easily.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:duplicateParentState="true"/>
</LinearLayout>
2015.01.06 23:11 update:
How to navigate through d pad in grid view sub items if grid view item
have two sub items image button and another button
For this case you should not use OnItemClickListener. Instead, you should make your grid item unfocusable and unclickable, and than set OnClickListener for both two buttons.
You can set the parameters in your customize adapter for the gridView. In getView method you can set each of the grid item view just like convertView.focusable(false) and convertView.clickable(false).
After that, you can set the click listener and give the method you want to do for the buttons in adapter.
Don't care about the D-pad action. Actually, D-pad will work automatically if there are elements can be focused.
The key point for this question is the conflict between grid item and its child elements. You could have just one part to be focused: grid item (the parent view), or the buttons (the child views).

ListView inside two LinearLayout and scrolling issue

I have this structure
<LinearLayout
android:id="#+id/1">
<LinearLayout
android:id="#+id/2" />
<LinearLayout
android:id="#+id/3">
<ListView
android:id="#android:id/list" />
</LinearLayout>
</LinearLayout>
And I populate the ListView with some data. Well my problem is that the ListView becomes scrollable (while the LinearLayout number 1 fits the screen without scrolling), but what I want is this View to become full height and that the LinearLayout with id=1 becomes scrollable.
Inside ListView number 2 I have some TextViews, etc.
ListView's are scrollable by default if when the content is more than its display area.
However, LinearLayout would need to have a ScrollView in order to scroll.
Red Alert - You cannot use ScrollView and ListView together. You will end up seeing un-expected.
Alternate Solution: Prioritize what is more important to you, if scrolling the entire screen then add a ScrollView (provided your ListView items are static) else I will to wait to hear a good solution on this one. :)
Add ScrollView for ListView.
ScrollView will work for only one child view, so you have to add like this
LinearLayout
LinearLayout
LinearLayout
ScrollView
ListView...../ListView
/ScrollView
/LinearLayout

Android: put something on top of linear layout

What I want to do is show a "frame" (or new layout) on top of "2" (second LinearLayout), when a button would be pressed. How should I do it? Precreate it and make it somehow hidden if button not pressed?
I have this type of layout:
XML:
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ViewFlipper>
</ViewFlipper>
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
Use FrameLayout to show view over-lapping another view. You can keep the view as INVISIBLE or using GONE in the xml and then just make it visible when the Button is Clicked.
Yes...you should prepare it in xml and give it an id.then you can easily manage its visibility on button click using mLinearLayout.setVisibility(View.GONE); and mLinearLayout.setVisibility(View.VISIBLE); like:
Button mButton=(Button)findViewById(R.id.button);
LinearLayout ll=(LinearLayout)findViewById(R.id.frame_layout);
static int count=0;
mButton.setOnClick.... (new OnClick...()
public void onClick(){
count++;
if(count==1)
ll.setVisibility(View.VISIBLE);
else
{
count=0;
ll.setVisibility(View.GONE);
}
}
);
Here you have two options:
As you said pre-create layouts and set visibility to Visibility_Gone to layouts initially, not to be shown, set Visibitlity to View.Visible to display the layouts.
Another approach is to create views dynamically, and adding to parent on specified index, like to add on top of linearlayout use:
linearLayout.addView(view, 0);
If you want to show any view on button click then first put that view inside xml and make its visibility gone, and on button click make it visible. I have put imageview inside your code which visibility is set as gone so it wont show in layout.
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
For making image view visible,
imag1.seVisibility(View.VISIBLE);

Can I make the entire activity scroll instead of the ListView?

I'm populating an activity with a ListView that sometimes exceeds the window size vertically. Instead of the user being able to scroll the ListView, I want the ListView to just take up the space it needs and let the users scroll in the activity instead. Is this possible?
Here is an image for clarification: http://imgur.com/1I2bc
On both sides there is a ListView containing 8 elements, only the right one takes up the space it needs to show the list fully though, pushing the other views down and making the entire activity scrollable.
You need to use the methods addHeaderView and addFooterView to add views before and after the ListView.
To add several views, first put the views in a Layout or ViewGroup and add that ViewGroup as the header or footer view (ViewGroup is a subclass of View).
-have you tried to put your scroll element as top level element in your layout .xml?
Something like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/listView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</ScrollView>

Categories

Resources