How to implement a overlapping TextView in a ImageView, which combinely works as a Button? - android-studio

I am new to Android development and I need to know if I can overlap an imageView and a TextView together into a button. It should be similar to the image that I've shared and I need to frame it in a GridLayout with 2 columns. If I use FrameLayout for each imageView and textView it becomes little complex, is there any alternative method?

Try this and run...if any error occurs please tell me I will help
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/img_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_image"
android:scaleType="centerCrop"
android:textColor="#color/colorBlack"
android:text="Title"
android:textSize="25sp"/>
</GridLayout>

Related

Why Relativelayout doesn't work on fragments?

I have created a bottom navigation bar from this tutorial https://www.youtube.com/watch?v=JS3DIb4x1JQ&t=1352s, but RelativeLayout doesn't work properly on fragments.
Here is the code of bluetooth fragment with textview in the middle:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="#+id/bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0472FE"
android:text="Bluetooth"
android:textSize="30dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
This is what it looks like in
design
And this is what it looks like in
android emulator
How do I fix it?
Are there other components in this view? check the main view if it set to match_parent.

Confuse about recyclerview and imageview sizes

i'm currently using a recyclerview to display a menu consisting of an image each row.
i've created the image to be 1080 x 300 in order so it can be scale down withouth mutch issue
I can't quit understand what width options to use for the RecyclerView, and for the items layouts and imageview.
I need the image view to be, around 150dp in Height and the With to ocupy the entire screen, just like on the editor
EDIT 1:
Now theres a huge empty space between rows
Use this code for ImageView in layout resource file created by yourself like custom.xml i.e. res/layout/custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgItem"
android:layout_width="match_parent"
android:layout_height="150dp" />
</RelativeLayout>
Use this code for RecyclerView :
android:width="match_parent"
android:height="match_parent"
Try to change the Scale of Image View to Center Crop or Fit XY
Like This:
android:scaleType="centerCrop"
Use RecyclerView in your default activity like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:src= "" />
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Note that the height for ImageView is wrap content. This will work fine given that the height of each image is equal

ImageView not showing full size in LinearLayout

For fun I am working on the Udacity Popular Movies app. I'm at the point where I'm clicking the movie image in the GridView and going to the detail view where i'm hoping to display the backdrop image on top, title below that, and overview below that. All in LinearLayout with the gravity for everything at the top.
Main Activity
Detail Activity
Here is the code to set the image in the ImageView:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://image.tmdb.org/t/p/w500/" + movie.backDrop).into(imageView);
Here is the Detail Layout File:
<?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="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:layout_gravity="top"
android:orientation="vertical"
tools:context="com.ferr3t.don.gurumovies.MovieDetail">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/text_view_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/text_view_description" />
</LinearLayout>
I'm not sure why the image is centered and small I want it to Fill the width of the screen. Any help would be greatly appreciated.
Try This in in xml Imageview property
android:scaleType="fitXY"

In Android, how do I position an image on top of another to look like a badge?

Please see below:
I tried using Absolute layout, but that's deprecated. I appreciate your help, thanks.
RelativeLayout is a great option.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scaleType="centerCrop"
android:src="#drawable/iconImage" />
<ImageView
android:id="#+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/icon"
android:layout_alignTop="#+id/icon"
android:src="#drawable/badge" />
If you actually want a badge with a dynamic number/text, then you can make the second ImageView a TextView (or a ViewGroup such as LinearLayout or RelativeLayout) and give it a background drawable and set the text to what you want.
Have a look at the ViewBadger project on github(but keep in mind that you shouldn't try to copy other platforms UI elements in android apps).

Android app RelativeLayout not showing

so im trying to use a relative layout (for basic app tabs at the bottom of the screen) and it is not showing when i place it inside the LinearLayout after 2 other layouts (a LinearLayout and a ScrollView). The ScrollView contains several textViews and scrolls nicely on its own and the linearlayout before it also works nicely, but the relativelayout that i have tried to place at the bottom of the screen does not work.
Here is the general code for what i am trying to do:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/common_bg"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="50dp">
<TextView android:layout_height="50dp"
android:text="Heading"
android:gravity="center"
android:textSize="17dp"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_gravity="top"
android:layout_marginBottom="60dp">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingTop="15dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold"
android:text="#string/text"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/bottomMenu"
android:layout_width="wrap_content"
android:layout_height="60dp" />
</LinearLayout>
UPDATE: the Relative layout that im using is custom in a way that i cant actually show the code for it here, but it contains a radio group with several buttons.
UPDATE 2: Ok, so I solved the problem by manipulating the layout_height="wrap_content" on the 3 layouts (the first linear, the one holding the scroller, and the bottom relativelayout) as well as manipulating the layout_wieght of each of them until I was satisfied with the way it looked... it doesnt seem like this is the best possible solution, but it worked so i cant complain too much lol...
The relative layout in itself wont be displayed in an xml graphical layout or when you are running the app, it needs a child element to occupy the parent layout, try putting a text view inside the relative layout, it'll be displayed, position it right below the linear layout having the scroll view, it'll work properly, i just tried it with your code and it works

Resources