How do I referente all ImageView in android studio? (java) - java

I have 5 ImageViews, and i want to hide all.
My code:
findViewById(R.id.foto1).setVisibility(View.INVISIBLE);
findViewById(R.id.foto2).setVisibility(View.INVISIBLE);
findViewById(R.id.foto3).setVisibility(View.INVISIBLE);
findViewById(R.id.foto4).setVisibility(View.INVISIBLE);
findViewById(R.id.foto5).setVisibility(View.INVISIBLE);
I want to hide all of them once time, like:
Imageview.all.setVisibility(View.INVISIBLE);
There is some way?

Create a Parent layout and keep all the imageviews as child. Then make the parent layout invisible
Your XML:
<LinearLayout or RelativeLayout or CoordinatorLayout etc..
id = llayout>
<ImageView 1/>
<ImageView 2/>
<ImageView 3/>
<ImageView 4/>
</LinearLayout or RelativeLayout or CoordinatorLayout etc..>
your java:
llayout.setvisibility(INVISIBLE)

Related

Cannot find added elements in Tabbed Activity

I created a Tabbed Activity template project using Android Studio.
Then I created a LinearLayout with a nested TextView on it:
<LinearLayout
android:id="#+id/updateView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="707dp"
android:orientation="vertical">
<TextView
android:id="#+id/updateTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
This view is shown on preview, but there's none of it in the built app.
When I try to get a reference to it from my code:
LinearLayout lastUpdate = findViewById(R.id.updateView)
I get null in runtime.
So, how can I modify the code?
Preview screenshot
I solved my problem by moving xml code of LinearLayout upper, than code of AppBarLayout, and modification of dynamic pixels (dp) of ViewPager and my textView

Xml card layout

i trying to get a layout just like the image below for a game in android studio
currently my xml is
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dreacot.testmymemory.Game">
<ImageView
android:id="#+id/imageView"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="136dp" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="156dp"
tools:layout_editor_absoluteY="136dp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="266dp"
tools:layout_editor_absoluteY="136dp" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="285dp" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="156dp"
tools:layout_editor_absoluteY="285dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="72dp"
android:layout_height="115dp"
app:srcCompat="#drawable/backside"
tools:layout_editor_absoluteX="266dp"
tools:layout_editor_absoluteY="285dp" />
i want more cards to be added on the next level eg lvl 2
do i need to make several layouts for each level?
also i want each card to show its face when it is clicked
and finally i feel i am not doing the right thing as i feel xml files cannot be called into the gameplay is there a better way to do this layout(maybe programmatically)?
could anyone help?
The way you are doing it is not the proper way to do it, In this way maybe you can achieve this for 6 cards. What if you have let's say 100 cards or may be 1000, will you add 100 images in Layout and get reference of 100 images in Activity and set Click Listeners and animations to turn the side on 100 images?
Use "GridView" and Adapter for your views, it will do your work with ease.
Check this: https://developer.android.com/guide/topics/ui/layout/gridview.html
You have to do it programmatically.
1. Use RecyclerView with GridLayoutManager to show your card as grid style.
2. Create a layout with just a CardView with single ImageView for your single card item.
3. Create an custom RecyclerView.Adapter<> and use it with RecyclerView to populate your card data on CardView.
4. Add onItemClick listener to Adapter to flip to the specific Card item and do something as per your needs.
Here are some useful links about RecyclerView:
Android RecyclerView with GridLayoutManager Example
Android GridLayoutManager with RecyclerView in Material Design
Simple Android grid example using RecyclerView with GridLayoutManager
Here is a good library for CardView animation.
Hope this will help~

RecyclerView animates items all at once instead of one by one

My Problem
I've been trying to get my Recycler View's items to animate as I scroll down. These items are loaded from a local XML and passed through my custom adapter.
I'm using this library for my adapter animation
I noticed that no matter what I tried, the items would just not animate at all, or they would animate all at once (every item would slide in on screen at the same time).
What I've Tried So Far
Utilizing the library I mentioned, I tried animating items in my RecyclerView.Adapter.
> IndexFragment.java snippet
//Create an adapter
SongAdapter adapter = new SongAdapter(songs, getContext());
//Set adapter
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
This one failed somehow. I also tried the setItemAnimator method which also failed.
recyclerView.setItemAnimator(new SlideInLeftAnimator());
Layout Structure
So I assume it has something to do with my RecyclerView being inside of two NestedScrollView's.
Another person seemed to be having a similar issue
Below are some portions of my layout files
activity_main.xml
....
<!-- Fragment Container -->
<android.support.v4.widget.NestedScrollView
android:id="#+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
....
This is where I put my fragments
index_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nestedScrollView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
This is pretty much my Recyclerview and its parent Nested Scrollview.
item_song.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp">
<!--I have two text views in here. Removed them just for this snippet -->
</LinearLayout>
<!-- This displays a background on each item when I press them AKA touch effects -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground" />
</FrameLayout>
This is the structure of each item in the Recyclerview. I inflate this layout for each item. Pretty straightforward.
Any ideas as to what I could do to solve this issue? Thanks.
So after much testing, I finally came up with a solution that worked.
Change the fragment container in activity_main.xml from NestedScrollView to RelativeLayout or perhaps LinearLayout.
Remove the parent NestedScrollView and keep only the RecyclerView in index_fragment.xml.
Animations now work at last. Yay!
Apparently, it is a bad idea to put your RecyclerView inside of a NestedScrollView.
I read somewhere that because of having to load the proper height of its child layout, the NestedScrollView has to wait for it to completely load, in order to know the total height of the RecyclerView. However, I'm not totally sure if that's the reason, so feel free to correct me.
Hope this helps someone!

Java - How to add a JPG/PNG file from resources(drawable) to a layout?

I have a relative layout:
RelativeLayout battle_layout = (RelativeLayout) findViewById(R.id.battle);
I need to attach here a child (a JPG image from resources/drawable).
I was writting in c# and javascript earlier, but here it's a bit harder to accomplish :)
Is there someone who could help me? Thanks!
Maybe you can use one of these options. programmatically or in xml:
Add a ImageView to your layout
Set the image as background of your layout
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/YOUR_IMAGE">
<ImageView
android:src="#drawable/YOUR_IMAGE"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Code
ImageView imageView = new ImageView(this);
image.setImageResource(R.drawable.YOUR_IMAGE);
battle_layout.addView(image);

Java - Runnable

I'm trying to set an imageview's resource like so:
Code:
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.bg);
Yet I see nothing on my screen.
I can set
imageView = (ImageView) findViewById(R.id.bgView);
And it works, but in my instance I can't use this because I need apply the imageview to an arraylist of imageviews.
Is there any other way to construct my imageview or am I doing something wrong?
Edit:
My xml looks like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/entire_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
tools:context="com.example.guilyan.Game" >
<ImageView
android:id="#+id/bgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/Descript"
android:scaleType="fitXY"
android:src="#+drawable/testpicks" />
</FrameLayout>
I think u should add the dynamically created image view to your layout.just try this.Suppose if your layout id is frameLayout then
frameLayout.addView(imageView);
Using of
new ImageView(getApplicationContext()); not correct.
You need use activity context, not application.
And of course after initialization of view you need attach it to parent.

Categories