I have a card view that lists a set of cards dynamically. I have used recyclerview for that. Now I want to disable a card(based on value)by greying out the card or something like that. This is the layout that contains the card view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="112dp"
android:layout_height="140dp"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitXY">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="150dp"
android:padding="8dp">
<ImageView
android:id="#+id/appIcon"
android:layout_width="match_parent"
android:layout_height="62dp"
android:padding="5dp"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/appIcon"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#drawable/selector"
android:textSize="10dp"
android:textStyle="bold" />
<TextView
android:id="#+id/appVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="5dp"
android:layout_marginEnd="1dp"
android:text="version 0.0"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textSize="8dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I have found some related queries and have tried out using selectors and greying out each field instead of the entire card.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#color/colorAppName" />
<item android:state_enabled="false" android:color="#color/colorPrimary" />
</selector>
and in my class(Adapter.kt)
name = itemView.findViewById(R.id.name) as TextView
if(value==true)
{
name.isEnabled=false
}
But I want to disable the entire card. Please help
You can set tint to your relative layout which is direct child of card view in your item design. You have to do this in xml:
<android.support.v7.widget.CardView
android:layout_width="112dp"
android:layout_height="140dp"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitXY">
<RelativeLayout
android:id="#+id/relParent"
android:background="#fff"
android:layout_width="100dp"
android:layout_height="150dp"
android:padding="8dp">
....>
I have added two property in RelativeLayout first is id and second is background.
Now in adapter you can do this:
if(value==true)
{
itemView.relParent.background.setColorFilter(Color.parseColor("#6F6F6F"),PorterDuff.Mode.SRC_IN)
itemView.relParent.isEnabled=false
}
else
{
itemView.relParent.background.setColorFilter(Color.parseColor("#FFFFFF"),PorterDuff.Mode.SRC_IN)
itemView.relParent.isEnabled=true
}
Note: You must have to set background to RelativeLayout from xml otherwise itemView.relParent.background will return null and your app will crash.
Update
You need to set forground color to your card you in that case do this:
itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#906F6F6F"))
Here cardDashboard is card view which you are using in your item design.
So the final code will be like this:
if(value==true)
{
itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#906F6F6F"))
itemView.relParent.isEnabled=false
}
else
{
itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#006F6F6F"))
itemView.relParent.isEnabled=true
}
Add android:background="#drawable/selector" to your RelativeLayout or LinearLayout. That will work for enable and disable.
Note:- This is not related to question but if you follow. It will good for you.
We compare if(booleanVariable) directly instead of comparison like
if(booleanVariable==true).
Try setting the following to all the views,
android:duplicateParentState="true"
When this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
Then dynamically disable the layout of the item (at a particular position) resulting in disabling all its child views.
Note: Since your cardviews are in a recycler view you need to maintain the state for diffrent positions inside your adapter.
You can maintain the background states using the selectors as you did before.
I changed your layouts by adding the above mentioned attribute:-
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="112dp"
android:layout_height="140dp"
android:layout_margin="10dp"
android:id="#+id/cd"
android:scaleType="fitXY">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="150dp"
android:duplicateParentState="true"
android:padding="8dp">
<ImageView
android:id="#+id/appIcon"
android:layout_width="match_parent"
android:layout_height="62dp"
android:padding="5dp"
android:duplicateParentState="true"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:text="#string/app_name"
android:layout_below="#+id/appIcon"
android:layout_centerInParent="true"
android:gravity="center"
android:onClick="clickMe"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textSize="10dp"
android:textStyle="bold" />
<TextView
android:id="#+id/appVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="5dp"
android:layout_marginEnd="1dp"
android:text="version 0.0"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textSize="8dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
And have tested with disabling the cardview state, its child views also show the change with their color fading to light grey(haven't used selector for views so it changes to default disabled state (tested for textview)) .But for any clicklistner on the views you will have to use the saved state for the position disabled and not perform task when state is disabled.
I have created a grid view in Android. Now I want to use this custom layout to put text in the view. I can work on all views, but how can I create the view shown in picture and add text in the white space of image and inside blue rectangle too?
Try framelayout. I have tried one for you. Feel free to ask if you are not getting something.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewCircle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="2dp"
android:src="#drawable/mypng" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:gravity="center|center_horizontal"
android:text="50"
android:textSize="40sp" />
<TextView
android:id="#+id/textViewNumeric"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:gravity="bottom|center_horizontal"
android:text="Text"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
</FrameLayout>
I have a custom row for a listview that contains four TextViews.
Here is a screenshot of the layout I am creating:
The reason for multiple TextView is because I need to use different styles on each textview. Bold Name for example.
Here is the XML layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgPhoto"
android:src="#color/Red"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_margin="10dp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/imgPhoto"
android:padding="10dp"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/txtName"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="viewed a promotion by"
android:id="#+id/txtAction"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txtName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dominos Pizza"
android:id="#+id/txtMerchant"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txtAction" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="14 seconds ago"
android:id="#+id/txtAgo"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txtMerchant" />
</RelativeLayout>
</RelativeLayout>
This is a screenshot of what I am trying to achieve:
The question is how do I wrap the TextViews so that it fits inside the RelativeLayout just like in Picture 2? Or is there any other alternative I can use to achieve what I am looking for here?
I think it's possible through HTML code. You should be use to CSS for the wrap the text and then set into the view.
Use only one TextView and change the style with html. Example:
textView.setText(Html.fromHtml("<h2>This</h2><br><p>is an example.</p>"));
I want to make a link in android with a linear layout as the main view. within that there will be a horizontal linear layout for each row, in each row there will be an image and a description and a price and a id(wont be displayed).
Once i have a list off all my items i want the user to be able to click any where on the row and it will then do some action. How can i do this? I have tried settings clickable=true on the linearlayout:
here is my xml code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="menuItemSelected" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/burger" />
<TextView
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="textMultiLine"
android:text="This a pictuer of a hamburger" />
<TextView
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:ems="10"
android:inputType="numberDecimal"
android:text="R41.24" />
</LinearLayout>
<LinearLayout
android:id="#+id/tableRow12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="menuItemSelected" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Product Image"
android:src="#drawable/burger" />
<TextView
android:id="#+id/editText21"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="textMultiLine"
android:text="This a pictuer of a hamburger" />
<TextView
android:id="#+id/editText22"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:ems="10"
android:inputType="numberDecimal"
android:text="R41.24" />
</LinearLayout>
It sounds to me like you want a custom ListView: How to make custom ListView
This will allow you to create a scrollable, clickable, table of content. By customizing it you can format the content any way you'd like for each row.
What you are trying to achieve is best done using a ListView: ListView Tutorial
You should create a ListView, an Adapter and a custom View for that. By doing that you will be able to have the OnListItemClickedListener from which you can obtain not only the id of your list item, but the item itself.
I have an ImageButton and I want to show a text and an image on it. But when I try on emulator:
<ImageButton
android:text="OK"
android:id="#+id/buttonok"
android:src="#drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I get the image but without the text. How can I show the text? Please help me!
As you can't use android:text I recommend you to use a normal button and use one of the compound drawables. For instance:
<Button
android:id="#+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/buttonok"
android:text="OK"/>
You can put the drawable wherever you want by using: drawableTop, drawableBottom, drawableLeft or drawableRight.
UPDATE
For a button this too works pretty fine. Putting android:background is fine!
<Button
android:id="#+id/fragment_left_menu_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_bg"
android:text="#string/login_string" />
I just had this issue and is working perfectly.
It is technically possible to put a caption on an ImageButton if you really want to do it. Just put a TextView over the ImageButton using FrameLayout. Just remember to not make the Textview clickable.
Example:
<FrameLayout>
<ImageButton
android:id="#+id/button_x"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/button_graphic" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:text="TEST TEST" >
</TextView>
</FrameLayout>
Guys I need to develop the setting and logout button, I used the below code.
<Button
android:id="#+id/imageViewLogout"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_30dp"
android:layout_alignParentLeft="true"
android:text="Settings"
android:drawablePadding="10dp"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:drawableTop="#drawable/logout" />
Actually, android:text is not an argument accepted by ImageButton
but, If you're trying to get a button with a specified background (not android default) use the android:background xml attribute, or declare it from the class with .setBackground();
I solved this by putting the ImageButton and TextView inside a LinearLayout with vertical orientation. Works great!
<LinearLayout
android:id="#+id/linLayout"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/camera_ibtn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/camera" />
<TextView
android:id="#+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/take_pic"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
You can use a LinearLayout instead of using Button it's an arrangement i used in my app
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#color/mainColor"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_cv"
android:textColor="#color/offBack"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/cartyCv"
android:textColor="#color/offBack"
android:textSize="25dp" />
</LinearLayout>
you can use a regular Button and the android:drawableTop attribute (or left, right, bottom) instead.
Best way:
<Button
android:text="OK"
android:id="#+id/buttonok"
android:background="#drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Heres a nice circle example:
drawable/circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#ff87cefa"/>
<size
android:width="60dp"
android:height="60dp"/>
</shape>
And then the button in your xml file:
<Button
android:id="#+id/btn_send"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/circle"
android:text="OK"/>
Here is the solution
<LinearLayout
android:id="#+id/buttons_line1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn_mute"
android:src="#drawable/btn_mute"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
<ImageButton
android:id="#+id/btn_keypad"
android:layout_marginLeft="50dp"
android:src="#drawable/btn_dialpad"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
<ImageButton
android:id="#+id/btn_speaker"
android:layout_marginLeft="50dp"
android:src="#drawable/btn_speaker"
android:background="#drawable/circle_gray"
android:layout_width="60dp"
android:layout_height="60dp"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/buttons_line1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="mute"
android:clickable="false"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
<TextView
android:text="keypad"
android:clickable="false"
android:layout_marginLeft="50dp"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
<TextView
android:text="speaker"
android:clickable="false"
android:layout_marginLeft="50dp"
android:textAlignment="center"
android:textColor="#color/Grey"
android:layout_width="60dp"
android:layout_height="wrap_content"/>
</LinearLayout>
Best way to show Text on button(with image)
Your Question: How to show text on imagebutton?
Answer: You can not display text with imageButton. Method that tell in Accepted answer also not work.
because
If you use android:drawableLeft="#drawable/buttonok" then you can not set drawable in center of button.
If you use android:background="#drawable/button_bg" then color of your drawable will be changed.
In android world there are thousands of option to do this. But here i provide best alternate according to my point of view. (see below)
Solution: Use cardView with LinearLayout
Your drawable/image use in LinearLayout because it shows in center. And with help of textView you can set text on this. We makes cardView background to transparent.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_margin="16dp"
app:cardBackgroundColor="#android:color/transparent"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_selected_image"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Happy Coding"
android:textSize="33sp"
android:gravity="center"
>
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
here i explain some terms:
app:cardBackgroundColor="#android:color/transparent" for make transparent background of cardView
app:cardElevation="0dp" for hide evelation lines around cardView
app:cardUseCompatPadding="true" its provide actual size of cardView. Always use this when you use cardView
set your image/drawable in LinearLayout as a background.
Sorry, for my Bad English.
Happy Coding:)
ImageButton can't have text (or, at least, android:text isn't listed in its attributes).
The Trick is:
It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).