Android ImageButton interaction - android-xml

I wish to create the same activity like in google fit
And I created an activity with android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<RelativeLayout 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:background="#ddd"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="12dp"
android:background="#null"
android:onClick="onPreferencesClick"
android:src="#drawable/ic_more_vert_grey600_24dp" />
But when I click on three dots button it doesn't have any push effect in ui, but onPreferencesClick works fine.
How to create pushable ImageButton?

The easiest way would be to create a custom selector, for example like this (drawable xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#33000000"/>
<item android:drawable="#android:color/transparent"/>
</selector>
and apply it to the background of ImageButton

Related

Horizontally aligning two TextViews in Constraintlayout

Helllo!
I am currently learning the basics os JAVA and I can't quite figure a little thing out...
What I want to do: Horizontally align two TextViews in a CardView, while keeping them contrained to eachother
What I have tried:
Constraining them separately, this creates space between those two TextViews which I do not want.
Also I have played around with Chains, but couldn't quite get it to work (doing it wrong probably).
Lastly I tried searching this forum but could only find very big problems where mine did not fit into (relative layouts and others)
Image of the Android Editor: https://imgur.com/ICLPv8v
The pink circle is where Im having trouble with (too much space in between)
This is the code so far I've come up with :-)
FYI: I'm following the Android Basics: User Interface and it's been fun, this just seemed a bit too advanced for them, but I'm eager to learn!
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.cardview.widget.CardView
android:id="#+id/materialcard"
android:layout_width="200dp"
android:layout_height="200dp"
android:backgroundTint="#color/Gray200"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/Hi"
style="#style/Headline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="2dp"
android:text="Hi"
android:textColor="#color/Gray900"
android:textStyle="bold"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="#+id/materialcard"
app:layout_constraintTop_toTopOf="#+id/materialcard" />
<TextView
android:id="#+id/ExclamationMark"
style="#style/Headline_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="2dp"
android:text="!"
android:textAlignment="center"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#+id/materialcard"
app:layout_constraintLeft_toLeftOf="#+id/materialcard"
app:layout_constraintStart_toEndOf="#+id/Hi"
app:layout_constraintTop_toTopOf="#+id/materialcard" />
</androidx.constraintlayout.widget.ConstraintLayout>```
[1]: https://i.stack.imgur.com/Fd8Jf.png
You can use this layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/cardview_dark_background">
<androidx.cardview.widget.CardView
android:id="#+id/materialcard"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.7"
app:layout_constraintHeight_percent="0.4"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/Hi"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorPrimary"
app:layout_constraintWidth_percent="0.34"
app:layout_constraintHeight_percent="0.4"
android:elevation="2dp"
android:text="Hi"
android:textStyle="bold"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="#+id/materialcard"
app:layout_constraintTop_toTopOf="#+id/materialcard" />
<TextView
android:id="#+id/ExclamationMark"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorAccent"
app:layout_constraintWidth_percent="0.34"
app:layout_constraintHeight_percent="0.4"
android:elevation="2dp"
android:text="!"
android:textAlignment="center"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#+id/materialcard"
app:layout_constraintLeft_toLeftOf="#+id/materialcard"
app:layout_constraintStart_toEndOf="#+id/Hi"
app:layout_constraintTop_toTopOf="#+id/materialcard" />
</androidx.constraintlayout.widget.ConstraintLayout>
The important thing are those attributes:
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.7"
app:layout_constraintHeight_percent="0.4"
By doing that, now your cardView will be be exactly 70% of the screen with and 40% of the screen hegiht.
And now if you look at the layout I have done that for the other views, allowing myself to control the space inside the cardView.

Android remove cardview padding

From everything I have read, I should have cardview:cardPreventCornerOverlap="false" in my XML file to prevent padding between each cardview.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/card_view"
cardview:cardPreventCornerOverlap="false"
cardview:cardUseCompatPadding="false"
cardview:cardBackgroundColor="#android:color/holo_green_light"
android:layout_width="match_parent"
android:layout_height="100dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="120dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:layout_alignTop="#id/image"
android:layout_toEndOf="#id/image"
android:layout_toRightOf="#id/image"/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
android:layout_alignStart="#id/name"
android:layout_alignLeft="#id/name"
android:layout_below="#id/name" />
</RelativeLayout>
Yet, on my LG phone running 4.4.2, I am seeing white space i.e. padding between the cards. I deliberately set the background to light green.
Here is the screenshot.
enter image description here

How to change subtitle color in listVIew

I have a listView. Each list item consists of a title and a subtitle under title. I want to change the color of the subtitle. Please help me to change.
This is the layout you can use to make a different color textview for your subtitle:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#999999">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Color Change on this text view
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
After this you can make your own adapter and inflate your row and you will get the different color on your txtview in the listview you mentioned.
Hope this helps.

Reset background color in a ListView

I have a ListView where I'm changing the background color of the selected item using the onListItemClick event:
#Override
public void onListItemClick(final ListView l, final View v, final int position, final long id)
{
v.setBackgroundColor(Color.GRAY);
}
Is it possible to reset all of the rows of the ListView back to the original color before changing the single view's background color? The problem is that each selected item retains their background color and I only want the latest item the user clicked on changed.
UPDATE:
JRaymond's response seems to be the best approach, however, and it's usual when working with Android layouts, I've put the code in and nothing happens. No way to debug it, no error messages, nothing. I absolutely hate dealing with Android layouts, it's the most convoluted, terribly implemented design for dealing with UI.
Anyhow, this is what I have so far:
ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<ListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:listSelector="#drawable/listing_selector"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
/>
</LinearLayout>
listing_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:state_activated="true" android:drawable="#android:color/black" />
<item android:state_activated="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:drawable="#android:color/transparent" />
</selector>
Like I said, nothing happens when I click on items, even the built-in highlighting is gone. I even tried changing all of the color to black, but nothing happens.
UPDATE2:
I'm getting closer. After reading this article, you have to place your selector xml in a folder called color under res, then you set the selector as the background in the layout for each item in the ListView, not on the ListView itself:
<?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="fill_parent"
android:orientation="horizontal"
android:background="#color/listing_selector"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I'm seeing the background color change flash when I touch each item in the ListView however, they are not staying and reverting back to the original background color. This is the selector I'm using:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#color/blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/blue" />
</selector>
color.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="blue">#ff33b5e5</color>
</resources>
What an absolute pain in the ass this is. Whoever came up with this terrible system for dealing with the UI should be fired.
Solution:
Not 100% sure, but this may only work in Android 3.0+ devices. state_activated is what sets the background color.
What you're looking for is a combination of a stateListDrawable (also known as a selector) and choiceMode.
Include in your drawables folder an xml like this one (I'm using other drawables as my backgrounds, but color works just as well):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_pressed="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_selected="true"
android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_selected="true"
android:drawable="#android:color/black" />
<item android:drawable="#android:color/transparent" />
</selector>
and then set your listview's choiceMode to singleChoice
<ListView
...
android:choiceMode="singleChoice"
This lets the OS handle your backgrounds for you.
just a simple solution is in onitemclick just rememeber the clicked position of the item and call notifydatachanged() and in getView() of your custom adapter just before retrun statement a if loop comparing like below:::
if(postion == itemclickedposition)
converterView.setBackgroundColor(Color.GRAY);

Aligning fonts in tabwidget

Myself designing an app which uses GPS and Maps. I have created the UI as tabs. But i couldn't able to keep the font below the image.
Code in my Mainactivity
TabSpec gpsspec = tabHost.newTabSpec("GPS");
gpsspec.setIndicator("GPS", getResources().getDrawable(R.drawable.gps));
Intent gpsIntent = new Intent(this, GpsActivity.class);
gpsspec.setContent(gpsIntent);
tabHost.addTab(gpsspec);
XML file in my drawable folder for tabs
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/gps_icon"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/gps_icon" />
</selector>
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
Kindly help in aligning the font.
You need to provide
Small size of icons for small density devices in drawable-ldpi folder
Medium size of icons for medium density devices in drawable-mdpi folder
Large size icons for large density devices in drawable-hdpi folder
Here is documentation guide for sizes of the drawable and layouts.
if it doesn't solve your problem, you can consider provding your own customized tab indicator, which I've used in my post here.