Textbox height is too much - java

Please have a look at the following XML layout
<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:orientation="vertical"
>
<TextView
android:id="#+id/lblPassword"
android:text="#string/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/pwdText"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:inputType="textPassword"
/>
<Button
android:text="#string/btnLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showMessage"
/>
</LinearLayout>
This generates the following gui (attached)
As you can see, the textbox height is unexpectedly too much. Actually what I need is adjusting textbox width to fit to the app properly, providing enough space to type (if I didn't do anything, the textbox appears very small in width). I am new to android. Please help!

To expand the width to match the width of the device replace android:layout_width="wrap_content" with android:layout_width="match_parent", or you can specify a width in dp.
By setting the attributes android:layout_height="0dp" and android:layout_weight="1" you are saying you want this elements height to fill all the remaining space on the screen.

Does it fix the issue when u remove this line android:layout_height="0dp"
edit: or change it to android:layout_height="wrap_content"

change your EditText to this
<EditText
android:id="#+id/pwdText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
/>

Do not use "match_parent" attribute for either width or height for your outermost layout. Use either wrap_content or fill_parent. Also replace 0dp in the layout_height for the editText with wrap_content.

For the text View height change it from wrap content to 10 dp.

Related

Drawing line between two drawables in the textviews

I have the issue with drawing line like in a pic above. How can I do that? Here's the code of the view, where I need to implement that. I guess I need to use canvas, but I'm not sure about that. Thanks for any advice!
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/linear_layout_buttons_margin_top"
android:orientation="vertical">
<TextView
android:id="#+id/button_from_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="4dp"
android:background="#drawable/button_white"
android:drawableLeft="#drawable/ic_green"
android:drawablePadding="#dimen/dots_white_button_padding"
android:drawableStart="#drawable/ic_green"
android:gravity="center_vertical"
android:hint="#string/from_place"
android:padding="#dimen/dots_white_button_padding"
android:textColor="#color/color_accent"
android:textColorHint="#color/text_view_hint_color"/>
<TextView
android:id="#+id/button_to_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button_from_city"
android:layout_marginBottom="4dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="4dp"
android:background="#drawable/button_white"
android:drawableLeft="#drawable/ic_red"
android:drawablePadding="#dimen/dots_white_button_padding"
android:drawableStart="#drawable/ic_red"
android:gravity="center_vertical"
android:hint="#string/to_place"
android:padding="#dimen/dots_white_button_padding"
android:textColor="#color/color_accent"
android:textColorHint="#color/text_view_hint_color"/>
</RelativeLayout>
You can provide two drawable resources for the textviews and set them as drawableStart, based on the required conditions of your work. The point here is using an image which contains a colored circle and a line, which is much simpler than drawing the layout with canvas.
I will simply add two View with grey background of 1dp width above and below your dot drawable and manage visibility depending on the position of your items.

How can i remove unnecessary top padding in cardview?

I managed to implement Cardviews in my app, but the cardview show an unnecessary padding in the top.
What i want to achieve is to get a header image like this :
Here's my cardview Layout file :
<android.support.v7.widget.CardView
android:id="#+id/programCardview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardUseCompatPadding="true"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/programHeader"
android:layout_width="match_parent"
android:layout_height="160dp"
android:src="#drawable/createdprogramviewcard"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Programme d'endurance"
android:textSize="20sp"
android:textAlignment="center"/>
<TextView
android:id="#+id/objectif"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Objectif : " />
<TextView
android:id="#+id/programObjectif"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="6 séances"
android:layout_toRightOf="#id/objectif"/>
<TextView
android:id="#+id/programWorkouts"
android:layout_below="#+id/programTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="textEnd"
android:layout_alignParentRight="true"
android:text="6 séances" />
</RelativeLayout>
</LinearLayout>
This is the code of the RecyclerView :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewPrograms"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
I manage to change the attribute cardUseCompatPadding but that not affect the internal form of the cardview, it's just there to separate them.
Thanks in advance.
I found a solution for my problem by changing the height of the imageView to 130dp. Apparently since i made the with of the image to match_parent, i had to find the exact height that will suit the image inside the cardview without giving it some extra padding.
The situation is due to two reasons, the first is that the height property of the CardView is in match_parent or in a larger size than the components occupy and at the same time the gravity property of the layout is in center.
To fix it just set the gravity of the LinearLayout to center | fill or just to fill.
I used these below attributes and it works for me.
card_view:cardElevation="0dp"
card_view:cardMaxElevation="0dp"
Or you can just use MaterialCardView instead of legacy CardView

Android button layout adapting to screen size

I am new in Android development.I created a simple project with a simple button.I thought that the
button will look the same on different screen sizes but when i previewed all screens eclipse displayed this
http://imgur.com/kjEMhHx
This is the xml code
<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: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="com.example.businessideas.MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Button" />
</RelativeLayout>
Can you please help me and tell me how can i design that button look the same on all screen sizes?
Thanks
You'll need to use the weight attribute when the button's parent is LinearLayout. That way it will force the button to have a size proportional to the screens width:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:text="Button"
android:layout_weight="2"
android:layout_width="0dp"/>
</LinearLayout>
Notice that if the LinearLayout's orientation is horizontal, width will be adjusted by android (implicitly) therefore the children have width of 0dp.
Also note that now the button will take 20% (2/10) of the LinearLayout's (the parent's) width.
On the button part change:
android:layout_width="20dp"
android:layout_height="20dp"
Use specific DP sizes to keep items the same physical size on different screen (replace 20 with your desired size).
If you want your button to be of the same size in all different screen than you have to manually give the DP size.
like
andoird:layout_width = "20dp"
android:layout_height = "20dp"
You can also place your button at specific location by changing your relative layout to linear layout.

Alignment issue in Android EditText

Please have a look at the following code
<ScrollView 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"
tools:context=".Form" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/heightLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/height"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/heightLabel"
android:layout_marginLeft="5dp"
android:hint=""
/>
</RelativeLayout>
</ScrollView>
I need this EditText to be displayed right next to the TextView, with it's appropriate size. But instead, it is displaying in no where. I don't even see it! Please help!
You are using wrap_content but no default text. So if the EditText is visible, it will be quite small. You need to define an appropriate size yourself. Try using match_parent as the width or adding an attribute like minWidth or ems.

How can I display a long TextView height in a ListView row?

I have a ListView that looks like this:
Notice how the TextView is getting cut off with elipses. How can I make sure the entire TextView is visible inside of the ListView?
Here is my XML for the row (the textview is called bodyTextView):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="4px">
<ImageView android:id="#+id/avatarImageView"
android:layout_width="48px"
android:layout_height="48px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="4px">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView android:id="#+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#444444"
android:padding="0px"/>
<TextView android:id="#+id/bodyTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textColor="#666666"
android:maxLines="5"
android:ellipsize="end"/>
<TextView android:id="#+id/dateTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Set ellipsize="none". Also, be sure to specify maxLines big enough.
Actually, ellipsize="none" won't affect the width of the element.
To get an element to display its full contents in a LinearLayout, you need to set its layout_width or layout_height (whichever is applicable) to "wrap_content", and then ensure that other elements in the LinearLayout will not take up the space first. Even with this option, a singleLine TextView will still ellipsize once it fills the width of the surrounding layout, of course.
How do you make sure other elements play nice? Make sure they are not also set to a fill_* option or wrap_content. An easy way to display several TextViews with one taking up all the space it needs and the others playing nice is this: Set the primary one to a layout_width of "wrap_content" with a layout_weight of "0" (this is the default weight, you do not need to set it), and then set the others to a layout_width of "0dip" with a layout_weight of "1". This will give the primary TextView to the layout with a set width matching the content, and the other TextViews with an effective width of zero. The layout will then see the layout_weights and resize the other TextViews to take up the remaining space.

Categories