Android centering an textview next to a imageview - android-layout

I have an imageview and i want to put in the right a textview, but that will stay centered verticaly, so I can make my custom listview.
This photo can explain better:
My code
<?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="match_parent"
android:gravity="top" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Set your TextView's attribute
android:layout_centerVertical="true"
It would be even better if you set your image as a compound drawable inside the TextView (to the left of it):
android:drawableLeft="#drawable/icon1"
android:drawablePadding="8dp"
The two lines above are in the TextView definition and completely replace your ImageView.
This is a best practice and increases the overall performance, by flattening your layout hierarchy.
And remove the android:gravity="top" attribute in the parent.
Also, to make this the item row of a ListView, this android:layout_height="match_parent" should be android:layout_height="wrap_content"

Related

How to make multiple Textview selectable in a activity?

I am developing an Android app. It is just like an article reading app in which there are many textview (more than 10) under one activity.
Like -
<?xml version="1.0" encoding="UTF-8"?>
<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="wrap_content" android:paddingBottom="#dimen/activity_vertical_margin" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" tools:context=".EnviarMensaje">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My text is here" android:textSize="20sp" />
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My text is here" android:textSize="20sp" />
<TextView android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My text is here" android:textSize="20sp" />
</RelativeLayout>
I want to make all these textview selectable when user touch for a long time on text, an option should come "select all". And when choosing the select all option, the Text of all Textview (From textView 1 to textView3) should select.
I tried - android:textIsSelectable=true
But after apply this, when I choose select all option, the Text is selected of only textView1 or textView2. Not selected all text on screen together.
Please help me.
Checkout this: https://stackoverflow.com/a/6179365
You can register the textviews in onCreate, then override onCreateContextMenu and getTextfrom each textview.

RecyclerView Items have huge spaces between them

I have a RecyclerView that has several items. Rather than the items being directly under each other with the least possible amount of space between each row, there is a massive gap between each item like shown: (Sorry that the Image isn't very good, I took it on a small phone)
Image displaying the issue
My layout file for the RecyclerView is here:
<?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="match_parent">
<TextView
android:id="#+id/layout_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/layout_list_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/layout_list_name"
android:layout_alignLeft="#+id/layout_list_name"
android:layout_below="#+id/layout_list_name" />
<ImageButton
android:id="#+id/layout_list_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/layout_list_delete"
android:src="#drawable/delete" />
How do I get rid of this massive gap?
Try changing layout_height="match_parent" to layout_height="wrap_content". That should hopefully solve your problems.
Change the height of the parent layout to wrap_content. For the XML file you posted, it should be:
<?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">

Material Design Toolbar CustomView RelativeLayout

RelativeLayout doesn't seem to play nice in the v7 support library. I have three elements, One is aligned to the bottom and the other two stack above it. Rather than appear nicely stacked at the bottom, they are rendered off screen, this is unexpected.
I've included an example XML file which is for the custom view added to the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:layout_above="#+id/divider"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/textLighter"
android:text="#string/example_sequence_name"
android:singleLine="true" />
<View
android:id="#+id/divider"
android:layout_width="wrap_content"
android:background="#color/textLighter"
android:layout_above="#+id/description"
android:layout_height="81dp"/>
<EditText
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_description"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:textColor="#color/textLighter"
android:layout_alignParentBottom="true"
android:singleLine="true" />
Results in this view.

Custom action icon on android devices

I am developing custom menu item by calling action layout, but when i am taping that action menu that pressing color is not applying total height giving some top and bottom margin.
Below is my code:
<?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="match_parent"
android:gravity="center">
<ImageView
android:minWidth="56dp"
android:id="#+id/voicemailNotificationCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/ab_pressedicon_bgcolor"
android:contentDescription="#string/voicemailnotification"
android:src="#drawable/ab_voicemail">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vmNotificationTextCounter"
android:text="1"
android:visibility="visible"
android:layout_alignRight="#+id/voicemailNotificationCounter"
android:layout_marginRight="10dp"
android:gravity="center"
android:textColor="#fff"
android:includeFontPadding="false"
android:background="#drawable/notification_alertbg"
android:textSize="12sp"/>
</RelativeLayout>
Part of the problem is that you're not using an ImageButton.
Combining an image with text is possible with an ImageButton. You can even independently adjust the text and the image inside that ImageButton.
Hopefully, an ImageButton will work inside an action menu, I haven't actually tested that part yet.

can't get android custom dialog box to size properly

I'm trying to create a custom dialog box but it's both making it as wide as the screen (minus padding) but more importantly it's adding about an inch to the top in white (the background colour is red) above the first textview. What I want is a dialog box just as big as it needs to fill the content.
If I change any layout from fill_parent to wrap_content I get the contents about the size of the image and everything else (i.e. the text) is truncated.
What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customdialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/titledialog"
android:orientation="vertical" >
<TextView
android:id="#+id/textTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/titleback"
android:gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="24dp"
android:paddingTop="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/buttonCustomDialogOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
I found the answer here
Android: How to create a Dialog without a title?
Apparently the top blank bit is the title of the layout and you cannot get rid of it unless you use an AlertDialog and not a Dialog.
ah, thanks for the update.
Android Dialog: Removing title bar
you can do it like this
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
(OR Try This)
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Resources