I have a gridview that is made of of 31 cells. A user can select any combination of cells and then click on a button to save their selections. Once they click the save button a dialog box appears and asks them what they want to do next. Within this dialog is a edit text view. When the user clicks inside the edit text box the keyboard appears. When this happens only 21 of the 31 cells are visible on the screen and when I try to call the getChildrenCount() method of GridView class I only see 21 children. Can someone explain why this happens and what I can do to eliminate this issue? I have attached images in hopes that clarifies the issue. Below I have pasted a snippet of code where I am trying to get the child count of the gridview.
protected Set<String> getSelectedFrequencies() {
Set<String> selectedFrequencies = new HashSet<String>();
for(int i = 0; i < gridView.getChildCount(); i++) {
ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i);
for(int k = 0; k < gridChild.getChildCount(); k++) {
if( gridChild.getChildAt(k) instanceof Button ) {
if(gridChild.getChildAt(k).isSelected()) {
selectedFrequencies.add(gridChild.getChildAt(k).getTag().toString());
}
}
}
}
return selectedFrequencies;
}
There is nothing really special about the code.
Are you using a softInputMode of pan or resize? With pan that will never happen. With resize, it could be that only 21 fit on screen, so the gridView creates only 21 children and does view swapping when you scroll. In case you didn't know, listview and gridview only create as many views as are needed to fit on the screen. When you scroll it just loads data for the new positions into the existing views.
Related
I want to get all views inside of an activity. like;
ArrayList<View> views = new Arraylist<>();
views.addAll(getAllViewsFromActivity());
what I want is "getAllViewsFromActivity()" function. I clearly want to get all the views even a single button. But I couldn't find any clear answer.
For the progress, it must be like this:
MainView : getWindow().getDecorView()
- -
RelativeLayout
- -
Button LinearLayout
-
TextView
How can I get this tree in Android programmatically? and also lets assume that I got this tree, Can I also identify the types of them like: view instanceof Button ?
the view you want to get is clear. so you can use the parent view(such as LinerarLayout) to get the children. so you can try this:
int count = parent.getChildCount();
for(int i =0; i< count;i++){
View child = parent.getChildAt(i);
//here you can to chage the view some proper.
//if you only want to change the background color, it belongs to view, don't
// need to cast.
}
I'm trying to create a One-armed Bandit app.
I have created an animation xml file to go through multiple images. When a button is clicked, the animation stops.
My question is how to compare the picture that one animation stopped on with that of another? So far I've tried something like this:
if(wheel1.getBackground().getConstantState().equals(wheel2.getBackground().getConstantState())) matches++;
Any help is appreciated.
you must be starting the animation with .animationStart()
just use .onAnimationStop() and it will trigger the event automatically.
A View should not maintain application logic, the controller (your hosting Activity or Fragment) should.
That said, to achieve what you want use View.setTag() to apply a logical description of each View to it.
Then when stopping animation, loop through all Views you have and get their position on screen, get the Views mostly visible in each column of your bandit machine and compare their tags (View.getTag())
for example, if the items animate vertically use below method to determine where the bandit stopped.
//the area where to compare views
int BOUND_TOP, BOUNT_DOWN;
//your content view
ViewGroup rootLayout;
//method to get information about what is visible
public List<Object> getVisibleViewTags() {
List<Object> list = new LinkedList<>();
int count = rootLayout.getChildCount();
for (int pos = 0; pos < count; pos++) {
View child = rootLayout.getChildAt(pos);
float translationY = child.getTranslationY();
if (translationY > BOUND_TOP && translationY < BOUND_DOWN) {
list.add(child.getTag());
}
}
return list;
}
Now you just need to attach information about a view as tag to it.
example:
view.setTag("view_apples");
or
view.setTag("view_bananas");
Part of application I'm currently working on act exactly as android shell, shows several pages filled with icons with text labels. User could slide between pages to find needed element. I'm using PagerView with GridView in each page.
Content of each page should fit exactly of visible area, no scroll. The question how to calculate number of icons on each page?
The issue next, I can't call pagerView.getHeight(), I'll have 0 in result because actual layout calculation wasn't executed yet.
UPDATED:
Seems I wasn't able to describe my problem well, I'll try to provide more simple case, suppose I do have activity with status bar at the top and some button bar at the bottom, both fixed height. Whole remaining area in the middle is used by GridView.
Grid view should show rectangular icons, and what I need to calculate is how many icons it could show without scroll (because remaining icons will be shown on next activity).
You can try something like:
public int countElements(ViewGroup group) {
int count = 0;
for (int i = 0; i < group.getChildCount(); i++) {
View v = layout.getChildAt(i);
Class c = v.getClass();
if (c == *Icon*) {
count++;
}
}
return count;
}
In case you have inner views you can check if c is a GroupView and call the method recursively.
As for your second issue, try using this in onCreate:
private void calculateSize(int height, int width){
int rows = Math.floor(height/imageHeight);
int columns = Math.floor(width/imageWidth);
//do something with numbers
}
ViewTreeObserver vto = pagerView.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
vto.removeGlobalOnLayoutListener(this);
calculateSize(gridView.getHeight(), gridView.getWidth());
}
});
}
I have a custom rating bar in which i edited from this tutorial (http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/)
I only want to show full cookies and not empty ones hence i replaced the empty-cookie.png with just a transparent image.
My problem is, in my UI i want my visible cookies to be right aligned, in other words, to be shown from right to left instead of the current left to right.
How can i achieve that? i DO NOT NEED to show half cookies and this is only used as an indicator (android:isIndicator="true")
Alternate non-ratingbar solutions are welcomed too. :)
i solved this by setting visibilities of multiple ImageViews stored in an array and displayed using a RelativeLayout in the end.
private void setClocksVisible(int numClocks) {
if(numClocks == 0 || numClocks == -1) {
for(int i=0; i< holder.ratingClocks.length; i++) {
holder.ratingClocks[i].setVisibility(View.INVISIBLE);
}
} else {
for(int i=0; i<numClocks; i++) {
holder.ratingClocks[i].setVisibility(View.VISIBLE);
}
}
}
i am designing an app for android tablets. I have created a layout file which contains the application bar, next is some buttons. In the middle of the screen i want to have a horizontal scroll view inside which i need to show some images.
The number of images inside the scroll view depends upon the return data from the url. For this i am maintaining an array list. According to the size of the array i need to create the image views withing the scroll view.
I have placed all other buttons, textviews in the layout file and i need to make the above said view alone through coding, how to do this.
If the array size is 19, then the list of images within scroll view to be shown in the following order only
1 4 7 10 13 16 19
2 5 8 11 14 17
3 6 9 12 15 18
In iPad iBook apps library page, the books will be listed out in this way.
how to do this....
not sure but can try approach like this
1- Create/inflate a horizontal scrollview ..
2- make for loop running i= 0 to x
where x= (totalCount/3)+(totalCount%3>0?1:0)
3- Create a Linear layout with orientation vertical
4- create one more loop form j=0 to 3 or (i+1)*3+j< totalCount
5- add your element layout in Linear layout
6 after the inner loop closed add Linear layout in horizontal scroll-view
loop termination condition like the value of x may not be exact please check them
For making item clickable
1- take any view from element layout like in you case image-view is good option
2- creates a class in you activity or better to extend you activity with clickListner.
3- while creating the imageView for each element set this listener to all
4- Set the data object or index with element with image-view in tad using SetTag
5- in Onclick function you will get image-view as argument and use getTag to get that data of attached with clicked element
Thanks to Dheeresh Singh
Following is my main.xml file
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
i have my Horizontal scroll view in my main.xml file
public class LayoutActivity extends Activity
//implements OnClickListener
{
ViewGroup layout;
LinearLayout lr;
int x;
int total = 4;
int count = 0;
LinearLayout lay;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
count = 0;
layout = (ViewGroup)findViewById(R.id.linear_1);
x = (total/3)+(total%3 > 0?1:0);;
for(int i = 0; i < x; i++)
{
lr = new LinearLayout(this);
lr.setOrientation(LinearLayout.VERTICAL);
layout.addView(lr);
for(int j = 0; j < 3; j++ )
{
count++;
final View child = getLayoutInflater().inflate(R.layout.inflateview, null);
lay = (LinearLayout)child.findViewById(R.id.threeByThree_tableRow1_1_Layout1);
lay.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "selected id is "+child.getId(), Toast.LENGTH_SHORT).show();
}
});
lr.addView(child);
child.setId(count);
if(i == total)
{
break;
}
}
}
}
}
in the above code i have lr is the LinearLayout to display it in vertical order and View child is the data which i need to show in both vertical and horizontal order.
Thanks a lot Dheeresh Singh