Android : Centering Items in a ListVIew - android

I have just started learning Android and thought of a simple project to help me learn.
I have created a simple menu using a ListView on a LinearLayout.
What I want to do is center the text in the ListView and also center the ListView in the layout.
I tried setting gravity to center-Hozitonal but that didn't work.

You can set the gravity (not layout_gravity) attribute to center text. Make sure that you also set layout_width is set to "fill_parent". For example, in my_text_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
And then you can use R.id.my_text_view as your resource id.

Related

I can't change position of textview and buttons in layout editor

My layout preview doesn't show the action bar only the textviews and buttons I added and when I add a new button or textview, it just goes to the top left corner of the screen even though I tried using Relative and Constraint Layouts. I've tried using "Base.Theme.AppCompat.Light.DarkActionBar" but it doesn't solve it. I've had this issue since I installed android studio on my new PC.Here's a screenshot of my layout preview
<?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="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Create Account" />
</RelativeLayout>
It's totally normal that your button is on the top left corner if you don't set its position.
If you add this to your Button:
android:layout_centerInParent="true"
Than your Button will be in the center of the root layout.
You can even use ConstraintLayout for this problem. With the help of constraint layout handles you can easily set the button to the centre position.constraint layout centering button
it can be done easily : join right handle first, then left handle, then top and bottom handle.
also with the help of this, layout will not be distorted on different screens

Fit content of textview after change text size

I have text view:
<?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:background="#drawable/item_table_light_grey"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
When I change size of text using code
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextSize(adapter.getTextSize());
width and height of textView not changed though text size changed successfully.
What am I doing wrong?
Android does NOT refresh layout of views with "wrap_content" once it has been displayed.
So if you add a child view, or modify the content dynamically, you're screwed.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content". The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Another solution is to set fixed values to layout_width and layout_height, add gravity="center" on the TextView, and remove the wrap_content
Hope it helps!
For anyone might need this now and in the future, I did a trick to solve this. If your TextVew is inside a LinearLayout, eg. with orientation="vertical", then put these:
android:layout_height="wrap_content"
android:layout_weight="1"
to your TextView will make its height updated when the text change.
I don't know what happened inside, but this worked for me.
Another solution to this issue would be to remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent

Android - RelativeLayout w/TextView .. text in tTextView not centering

I been round and round on this, and I can not seem to get the text in the ListView to center.
The ListView itself centers good, but text inside is not.. I hate to ask seems so basic..
Here is Code I have:
<ListView
android:id="#+id/lvVoiceReturn"
android:layout_width="200dp"
android:layout_height="48dp"
android:layout_above="#+id/mute"
android:layout_toLeftOf="#+id/mute"
android:gravity="center" >
</ListView>
Im sure its an easy line of code but i have not located it, urgh.. =/
Thank you.
Droidster
It is because you have used android:gravity="center"
This doesn't center the textview, but centers the text inside of the text view.
What you want to use is
android:layout_centerInParent="true"
If you were using a linear layout, then you could use
android:layout_gravity="center"
This is where you got confused, in a relative layout you can't use this because it will give you a syntax error.
If you are using a custom adapter with a custom element layout file then please post the xml here. setting gravity attribute to center for list view does not ensure that your list items are gonna center align.

How to set the background of all the childview of the expandablelistView?

I'd like to set the background of an expandablelistView without changing the groupview.
Setting the background of each children item fails to meet the requirement. I think, the childView of the expandablelistView ,to some extend,is just like a listview ,and there must be somewhere to set it's background without having to set item by item. I'd be very appreciate for your kindness response ! thanks.
Set background to the layout/child which you must be inflating in adapter,not to the parent layout.
In childview layout ( child_.xml ) put in main layout background property.
like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
It changes only background of childviews.

How to create android activity that shows at specified screen position?

My current layout displays activity that is not full screen (that's OK).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dip"
android:layout_height="200dip" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hello"></TextView>
I also added android:theme="#android:style/Theme.Translucent" to manifest for my activity.
My 200x100dip activity now shows in the upper left corner. How can i specify position of my linear layout (or my activity)?
You can use either FrameLayout or RelativeLayout as outer most layout for this. Ant then use absolute position in dp or android:layout_centerInParent or similar.
I believe your Activity´s outmost layout element (LinearLayout) will be placed in a FrameLayout that is the parent given from Android. I suggest you let your outmost layout match_parent/fill_parent in layout_height and _width and then center the content inside it with gravity="center" on your outmost layout. By letting the outmost layout being transparent and not catch click element it will appear as layout in the middle where elements behind is visible. If Im correct guessing that's what you want to achieve here.
put that layout in another absolute layout in which you use android:layout_width="fill_parent" and android:layout_height="fill_parent" the other thing you can do is to use this: http://www.droiddraw.org/
you can move your elements around manually with that and it will give you the XML code that is used to do that. I found it very useful in laying out XML in android.

Categories

Resources