Add or Remove ImageView dynamically - android

Is there any way to add or remove android imageview in UI dynamically ? I have 2 objects : List view and image view. I want to show each other dynamically without crashing each other.

when you want to show listView and hide ImageView, you can follow below code.
listView.setVisibility(View.VISIBLE); // showing listview
imageView.setVisibility(View.GONE); // hiding imageview
you can choose which one to show and which one to hide.

You can have both loaded from xml and then keep playing with View.VISIBILITY set it to GONE to hide and VISIBLE to make it vsible again.

So can either implement idea suggest by above.
Or for more cleaner code you can put elements in different layout and make the complete layout as visible or hidden whenever a certain event is being triggered!!
Hope this helps....!!!

You can put listview and imageview into FrameLayout and switch visibility

Related

Android: Detect if user touches a view inside parent view

I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App

How to change gridview layout correctly while scrolling in android

I have a gridview in my android project which show some Items that fetch from. I use a custom adapter extends BaseAdapter and a custom layout for my gridview to show each item.
What I need is to change the gridview custom layout when a button click, I use a FloatingActionButton that when I click on that the layout change correctly, but the problem is when I am scrolling the gridview and click the FAB the gridview layout change but it render incorrectly !!!
What should I do to change layout correctly? Is there anyway to stop gridview from scrolling then change the layout?
P.S: I've used gridview.invalidate() method and some other methods like this but nothing works ! I also should mention that when the layout render incorrectly when I scroll up and down it became correct !
This should be very possible and easy using android's recycle view with it's layout manager. check up on their documentation https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Using a layout inside a layout?

Which is the best way to change the content inside a layout by pressing buttons?
I want 6 buttons and different content for each push.
I cannot use tab layout because i already used it so..
I would suggest using the gridview that is set up in API Demos. You can import it in as a new project from the Android SDK.
It works effectively like the list of 'All Apps'. However you can change the way it lays out the buttons/icons/text.
In an app I'm working on, I have a list of message types as buttons. Clicking a button changes the display to a layout for composing the selected message. To do this, I have a FrameLayout for the area I want to change. I reference this view as 'compose_content'. When I want to change the content, I run the following code:
compose_content.removeAllViews();
LayoutInflater.from(activity).inflate(R.layout.new_content,compose_content, true);
This will change the FrameLayout content to the content from the specified layout.
One solution can be to have all the 6 views inside your inflated xml and depending on the button pressed set the visibility of that particular view visible and the rest gone

Android: How can I create a layout within a layout ?

In my app I want to have a button that if the user clicks it
than a new layout is opened within the current (acually the main) layout.
the new layout should not fill all of the screen and parts of the previous layout
should be grayed out.
Any ideas on how to do this ?
You can show a hidden layout within your button's onClick event by calling
view.setVisibility(View.VISIBLE)
You can also fade out view elements or whole views with
view.setAlpha(75);
view.setBackgroundColor(Color.GRAY);
Note that "view" in the first example is your layout element.. LinearLayout, RelativeLayout, etc. and in the 2nd example, "view" is the element(s) you're trying to gray out.
Follow the answer of SBerg413. And for more information. you can take the relativelayout for the part that you want to hide and display on the button click.
And as like SBerg413 answer. you can hide the respective layout and show the layout you want to display.
Hope it will help you.
Thanks.
you can use a ViewFlipper to achieve what you want, position the viewflipper where the child views should fit (part of the screen you say)..
Inflate the rest of the "child" layouts from other xml, add them to the flipper and switch between them when you want...

Nested text view in android,with dynamic content

I want to display a recursive or nested text view at depth of 3 in Android.The text to be displayed is, dynamic coming from web service.
for example:
If I Click on Help(Level 1)
it'll show topics under Help
If I Click on Help_Topic_1(Level 2)
it'll show questions under Help_Topic_1
If I click on this question, say HT_Question_1(Level 3)
it'll show Answer of that question(Level 3)
how to accomplish with this? please guide me.
You should use ExpandableListView. Reference http://developer.android.com/reference/android/widget/ExpandableListView.html
You can call expandGroup and collapseGroup methods for expanding and collapsing on clicks.
the simplest way to do this is to have a nested layout structure. Your root view will contain the button to show level 1 and a child layout and initially be visible. The children's layout visibility will initially be set to "GONE". In the onclick listener for each button you change the visibility of the layout below it to view to "VISIBLE".
This of course is a very simple way of doing it. If you require to have open and close animations you'll need to use a more complex method.

Categories

Resources