Android layout : Align a TextView and a Spinner - android

I would like to align a textview (saying : "Day :") with a Spinner where the user can choose the day of the week he wants (Monday, Tuesday, etc.)
when I try to align them :
<TextView android:id="#+id/labelSpinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textSpinner1"
android:layout_toRightOf="#+id/spinner_days"
android:layout_alignParentTop="true"/>
<Spinner android:id="#+id/spinner_days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/labelSpinner1"
android:layout_alignParentLeft="true"
android:drawSelectorOnTop="true"/>
the result I get is that I only see the Spinner, and the TextView isn't showing (or is underneath the Spinner)
Thank you for your help!

I guess you want the Spinner to the right of the TextView? Check the following code:
<TextView
android:id="#+id/labelSpinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/textSpinner1" />
<Spinner
android:id="#+id/spinner_days"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/labelSpinner1"
android:layout_toRightOf="#id/labelSpinner1"
android:drawSelectorOnTop="true" />
Your problem was that the spinner filled the whole view (android:layout_width="fill_parent") while you forced the TextView to be right of the Spinner (so outside of the screen --> invisible for you)

Related

how to display textview below button in relativeLayout

in the below xml layout, i have two buttons at the same position and they will do their function based on the visibilty set. now i tried to place two textviews
below the buttons, i want the text views to be below both buttons so I used
android:layout_below="#id/actConnect2_btn_connect"
but at run time when the connect-button is visible the text view appears below it, and if pair-button is visible it overlap
how to display the textview below both buttons?
Note: i know that i can use android:layout:marginTop but i want to solve it without it
code:
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_below="#+id/actConnect2_tv_label_devClass"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/actConnect2_tv_label_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_btn_connect"
android:text="Service's UUID: ">
</TextView>
<TextView
android:id="#+id/actConnect2_tv_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_uuids">
</TextView>
Put both buttons in a LinearLayout then put the textview below the LinearLayout
take both button in one layout
<RelativeLayout android:id="#+id/relativeButton"
android:layout_width="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_height="wrap_content">
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_alignParentStart="true" />
</RelativeLayout>
and use this for your textview
android:layout_below="#id/relativeButton"
You have to place the buttons in a Layout (any type and arrange them accordingly)
Assign an ID to that layout.
Place the textView below that layout ID.

How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox

I have a Checkbox in Android with a TextView above it:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_margin="0dp"
android:gravity="center_vertical|center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="true" />
</LinearLayout>
The problem with Checkbox however is that it's internal "Image" got an invisible border around it. How to remove this? I've tried android:padding="-5dp" but this only worked for the right side of the CheckBox for some reason, not the top, left and bottom (my main concern is the Top).
Here is a screenshot of the Checkbox in Eclipse Graphical Layout window:
LineairLayout selected:
Checkbox selected:
In the second picture you can see the Checkbox has some padding around it when you select it. I want to have this removed (especially on the top), so that the space between the Text and the Checkbox is reduced.
Here is a picture when I have padding="-5dp":
Checkbox selected:
PS: If someone knows a solution with just a Checkbox and it's Text-field, so I can removed the LineairLayout and TextView, while still having the Text above the Checkbox I would also appreciate it. A single Checkbox with a custom style and Text is better for performance than a Checkbox inside a LineairLayout with an additional TextView. (I know this is more something for a different question, but I just add it here since it doesn't have a lot of priority. Everything is working already, except for the padding of the Checkbox.)
Ok, I changed the LineairLayout to a RelativeLayout, and than I can change the margin to a minus dp. This is my end result:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="false"
android:gravity="center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_edit_checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="-10dp"
android:checked="true" />
</RelativeLayout>
This gives the following result:
RelativeLayout selected:
This does change the space between the Text and the Checkbox. For me this is what I want, however it still doesn't explain why the Android Checkbox' internal Image got a padding at its Top, Left and Bottom sides.. Now idea who of Android had this idea, but it was a pretty bad one.. It was better to just use no padding on the internal image and then have some default internal Margin/Padding that can be changed by users..
OR
Edit :
You can also get the same result with LinearLayout :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="0dp"
android:text="For all products"
android:textSize="10sp" />
<CheckBox
android:id="#+id/checkBox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:padding="0dp" />
</LinearLayout>

Android: How to create an EditText and set it to another views position?

I have a layout with a textView and a spinner in it.
If the user clicks on a button I like to hide the spinner and create an editText field programmatically at the spinners position. How can I do this?
The layout looks like:
<RelativeLayout
android:id="#+id/rl_steuern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rl_betraege"
android:layout_alignRight="#+id/rl_betraege"
android:layout_below="#+id/rl_betraege"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" >
<Spinner
android:id="#+id/spinner_steuer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/TextView01" />
<TextView
android:id="#+id/TextView01"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Steuer:"
android:textSize="18dip" />
</RelativeLayout>
Hello first you need to add button to your xml file.I am not able to see it anywhere.
then on button click in your java class,hide the spinner and display the Edit Text view using following code:
spinner=(Spinner)findViewbyId(R.id.spinner);
EditText et = new EditText(YourActivity.this);
spinner.setVisibility(View.GONE); //hides the spinner
your_main_layout.addView(et); //dynamically adds EditText
You can ask if you have any further queries :)

Android's spinner text align right

I has a spinner contains 3 items:"English", "Simplified Chinese", "Traditional Chinese".
If I select "English", the spinner will show as below picture.
I means after select and show the selected one, but not all items list as below:
My layout.xml as below:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/spinner_selector"
android:gravity="right" />
</RelativeLayout>
I want to set the text align right.
But the width seems be set as the longest item.
And the text align left.
How can I modify it?

Trouble with word wrapping

I am having trouble getting a screen to appear as I would like it to be
The code below shows two lines of textview's
First line is just a date textview, below this are 3 textviews with some data in. On the right hand side I have a button and I wish to have it span over the two lines.
trouble is the 3 textviews could have data that push into the button and instead of word wrapping the text is either above the button or pushes the button off the screen
Could anyone help me solve this
Thanks for your time
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border_small">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<!-- single date line -->
<TextView
android:text=" - "
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/font_size_8"
android:layout_alignParentTop="true">
</TextView>
<!-- line under the date having 3 text fields -->
<TextView
android:text=" TEST TEXT TEXT "
android:id="#+id/round"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color_white"
android:textSize="#dimen/font_size_6"
android:layout_below="#id/date">
</TextView>
<TextView
android:text=" - "
android:id="#+id/spacer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color_white"
android:textSize="#dimen/font_size_6"
android:layout_toRightOf="#id/_round"
android:layout_below="#id/date">
</TextView>
<TextView
android:text=" TEST TEXT TEXT "
android:id="#+id/classification"
android:layout_toRightOf="#id/spacer1"
android:layout_below="#id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color_white"
android:textSize="#dimen/font_size_6">
</TextView>
<!-- button is on right hand side and spans both lines -->
<Button
android:text="#string/button_view"
android:layout_width="wrap_content"
android:id="#+id/button_details"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textSize="#dimen/font_size_6"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
</LinearLayout>
Change the RelativeLayout
android:layout_width="match_parent"
One thing I noticed is that in your spacer1 TextView, you have this line: android:layout_toRightOf="#id/_round" You have an extra underscore in there. That's not your problem, though. Wrap your date, round, and spacer1 TextViews in a Relative or Linear layout, and add the android:layout_toLeftOf="button_details" attribute to it. Should fix your problem.
Try setting both android:layout_toRightOf and android:layout_toLeftOf on the text field(s) that need to use word wrap. So you might make the right-most TextView use this code:
<TextView
android:text=" TEST TEXT TEXT TEST TEXT TEXT TEST TEXT TEXT TEST TEXT TEXT TEST TEXT TEXT "
android:id="#+id/classification"
android:layout_toRightOf="#+id/spacer1"
android:layout_toLeftOf="#+id/button_details"
android:layout_below="#id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

Categories

Resources