RecyclerView and CradView in Android Studio - java

I want to development an app that contains the following images.
enter image description here
and I don't know how to do that with CardView
How to create a CardView that can be added like the image?

Simply add an image view inside a card view
<CardView
android:width="wrap_content"
android:height="wrap_content"
app:cardElevation="2dp">
<ImageView
android:width="300dp"
android:height="300dp"
src="yourImgFile" />
</CardView>
And the image reference you provided, you can nest this code in another CardView element containing a Linear Layout with horizontal orientation i.e
<CardView
android:width="match_parent"
android:height="wrap_content"
app:cardElevation="2dp">
<LinearLayout
android:width="match_parent"
android:height="wrap_content"
android:orientation="horizontal">
<CardView
android:width="wrap_content"
android:height="wrap_content"
app:cardElevation="2dp">
<ImageView
android:width="300dp"
android:height="300dp"
src="yourImgFile" />
</CardView>
<CardView
android:width="wrap_content"
android:height="wrap_content"
app:cardElevation="2dp">
<ImageView
android:width="300dp"
android:height="300dp"
src="yourImgFile" />
</CardView>
<CardView
android:width="wrap_content"
android:height="wrap_content"
app:cardElevation="2dp">
<ImageView
android:width="300dp"
android:height="300dp"
src="yourImgFile" />
</CardView>
</LinearLayout>
</CardView>

Related

Elevation on circular Image not woking Android

the elevation is not working even after adding padding to the view, here is the Relative layout, what else I should add for a shadow in the image view.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
I use the code below to add shadow to any View that looks circular. Works on Lollipop and above.
myCircularImageView.setOutlineProvider(new ViewOutlineProvider() {
#Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
myCircularImageView.setClipToOutline(true);
I don't encourage using CardView because it makes your layout more complex.
you can use cardview as parent layout of relative layout & give the elevation to cardview. It will give shadow to your imageview. Hope it will work for you.
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardCornerRadius="5dp"
card_view:cardBackgroundColor="#color/colortransperent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Easy way is to use textview in a card view and then add shadow to it using
android:elevation="", see this on how to use CardView.
Best is to use CardView and add the RelativeLayout inside the CardView and fill it with match_parent.
Then use 'elevation' attribute of CardView
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Dependency for CardView
implementation 'com.android.support:cardview-v7:28.0.0'

android - button under a layout is still touchable

I have a layout with 5 buttons and each of them has a OnTouchListener that slides a frame layout on screen (on top of the buttons) but after that all of my 5 buttons is still touchable.
How to disable touch for views that is under other layouts such as
FrameLayout that filled with a fragment ?
<Button
style="#style/tab_btn"
android:background="#drawable/village_button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_height="70dp"
android:layout_width="70dp"
android:text=""
android:onClick="buttonPress"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn4"
android:layout_marginTop="80dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn3"
android:layout_marginTop="120dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn2"
android:layout_marginTop="160dp"
android:layout_marginLeft="-18dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/sidebar_btn"
android:id="#+id/sidebar_btn"
android:layout_marginTop="200dp"
android:layout_marginLeft="-18dp" />
<FrameLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_marginTop="-17dp"
android:layout_marginLeft="-176dp"
android:layout_marginBottom="-17dp"
android:id="#+id/sidebar"
android:background="#color/black">
</FrameLayout>
I think you must add android:clickable="true" to FrameLayout
So it can capture the touch or click event when it's on above the button's
<FrameLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_marginTop="-17dp"
android:layout_marginLeft="-176dp"
android:layout_marginBottom="-17dp"
android:id="#+id/sidebar"
android:clickable="true"
android:background="#color/black">
try use
bt.setVisible(View.GONE);
on the buttons you need to disable and if you need use it again you can use
bt.setVisible(View.VISIBLE);

Get two buttons 50/50 width in my layout

i've got a layout that looks like below. But ideally I want the buttons Save and Cancel to be 50% width each, next to each other. Can't quite seem to get them to work though.
Any suggestions?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="-" />
<EditText
android:id="#+id/txtCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnPlus"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/btnPlus"
android:layout_toRightOf="#+id/btnMinus"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="+" />
<Button
android:id="#+id/btnGo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnMinus"
android:layout_toRightOf="#+id/button1"
android:maxWidth="100dp"
android:text="Save"
android:width="100dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnMinus"
android:text="Cancel" />
</RelativeLayout>
You shouldn't be using a RelativeLayout for this design. You should be using a LinearLayout.
Something like this outline (very stripped-down):
<LinearLayout android:orientation=vertical>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=100 /> <!-- Minus -->
<EditText android:layout_width=0 android:layout_weight=1 />
<Button android:layout_width=100 /> <!-- Plus -->
</LinearLayout>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Cancel -->
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Save -->
</LinearLayout>
</LinearLayout>
Your looking for android:layout_weight="0.5". This only works with LinearLayouts, though.
So what you could do is add a LinearLayout that contains your two buttons and set the weights then.
You should use a LinearLayout, inside the RelativeLayout, only for buttons, and give them the same android:layout_weight (1 and 1, 2 and 2, as you wish).

Android XML Issue - images not displaying in AVD

I'm fairly new to android but I've managed to figure out how all the layout stuff works with XML. That's all fine.
In the Graphical Layout tab in Eclipse I have a few buttons and a logo which is a .gif image. I have place the .gif image into my XML file as an ImageView within a LinearLayout and it shows up on the Graphical Layout.
But when I run it in the Android Virtual Device (AVD) everything shows up but the image. Anyone know why this is happening? See XML code below...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/natlib"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-8dp"
android:padding="#dimen/padding_medium"
android:text="#string/welsh_libs"
android:textColor="#79438F"
android:textSize="22dip"
android:textStyle="bold"
tools:context=".MainActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#A4C81C"
android:text="#string/ask_lib" />
<Button
android:id="#+id/button2"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FF0066"
android:text="#string/find_book" />
<Button
android:id="#+id/button3"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#3F83F1"
android:text="#string/find_lib" />
<Button
android:id="#+id/button4"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FE0002"
android:text="#string/register" />
<Button
android:id="#+id/button5"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#FBFC3F"
android:text="#string/login" />
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
</LinearLayout>
set android:layout_height greater than 0...try this
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
Which folder did you place the image in? /res/drawable?
Also, which folder did you place the layout in? /res/layout?
The /res/ folder and it's subfolders are used in Localization.
So if you only have it in /res/drawable-xhdpi/, it'll only show up on very large devices.
Also, if you modified your layout file at /res/layout-large/ and didn't modify the one at /res/layout, only the large version would get the modifications.

Android Beginner: Prevent admob from pushing content up

I am a beginner so bear with me. I have a layout with several buttons and then an adview at the bottom. When the ad loads, it pushes the buttons up and makes them very small. Is there anyway to prevent the content from being pushed up?
Here is my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_hdpi" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="a14fc541226f07b"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/ad"
android:orientation="vertical"
android:weightSum="7.0" >
<Button
android:id="#+id/basics1"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2.0dip"
android:layout_marginTop="100.0dip"
android:layout_weight="1.0"
android:text="Overview"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics2"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Campaign"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics3"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Special Ops"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics4"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Zombies"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics5"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Modes"
android:textColor="#ff000000"
android:textSize="16.0sp" />
<Button
android:id="#+id/basics6"
android:layout_width="150.0dip"
android:layout_height="0.0dip"
android:layout_gravity="center_horizontal"
android:layout_margin="2.0dip"
android:layout_weight="1.0"
android:text="Ranks/Unlocks"
android:textColor="#ff000000"
android:textSize="16.0sp" />
</LinearLayout>
</RelativeLayout>
You have a few options.
Make your buttons a fixed size instead of weight dependent. Weights make your Buttons take up a fraction of the space available. So when the ad loads, the space available decreases and then your buttons become smaller.
Change your RelativeLayout into a FrameLayout and put the ads after the button LinearLayout.
Put everything inside ScrollView, ie:
RelativeLayout -> ScrollView -> LinearLayout (height:wrap_content,width:fill_parent) -> AdView + LinearLayout with Buttons

Categories