I am creating a feedback form, and at the end there are two buttons, "Clear" and "Send". This has to be done within a Relative Layout.
The problem is the buttons are not wide enough, I want the buttons to match the width of what is above them.
Here is the app before trying to change the width:
And the 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=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/feedback_name_hint" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:hint="#string/feedback_email_hint" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ratingBar1"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:gravity="center_vertical|top"
android:hint="#string/feedback_actual"
android:inputType="textMultiLine" >
</EditText>
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ratingBar1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="18dp"
android:text="#string/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="#string/Send" />
</RelativeLayout>
I have tried to create a linear layout around the buttons, like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="horizontal" >
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Clear"
/>
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Send"
/>
</LinearLayout>
However, whilst the buttons are the size that I want, they jump to the top of the view, and affect the other elements:
Any help on this would be greatly appreciated, thanks.
SOLVED:
It worked by surrouding buttons with the following linear layout: (note how Rating Bar is set to be above the linearlayoutid)
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearlayout_id"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<LinearLayout
android:id="#+id/linearlayout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_marginBottom="18dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Send" />
</LinearLayout>
Try this
Replace your xml code with the following.
Here I have added android:layout_above="#+id/linearlayout_id" to RatingBar and have put your buttons in LinearLayout by giving android:layout_weight="1" to both buttons
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearlayout_id"
android:layout_alignLeft="#+id/editText3"
android:layout_marginBottom="21dp" />
<LinearLayout
android:id="#+id/linearlayout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_marginBottom="18dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#String/Clear" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#String/Send" />
</LinearLayout>
This Should work
Your attempt of putting them in a LinearLayout is correct. Simply add android:layout_alignParentBottom="true" to the LinearLayout and it should work for you.
Update:
Also, change android:layout_height="fill_parent" to android:layout_height="wrap_content" in the LinearLayout.
Related
I have to place two edittext and a button on the same line.
The two edittext should have the same width, the largest possible so to fill the line. The button should be on the right.
It should be like this:
Edittext_______________________ EditText_________________________ Button
I post some relative code, which for the moment is not working.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/new_formulas"
>
<Button
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:id="#+id/add"
/>
<EditText
android:textSize="30dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/add_hint"
android:id="#+id/add_hint2"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
/>
<EditText
android:textSize="30dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/add_hint"
android:id="#+id/add_hint"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/add"/></RelativeLayout>
Try following Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/button"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="User"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Pass"/>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Click"/>
</RelativeLayout>
Please try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/new_formulas"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/add_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:hint="Hint"
android:textSize="30dp" />
<EditText
android:id="#+id/add_hint2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/add_hint"
android:minWidth="100dp"
android:hint="hint2"
android:textSize="30dp" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/add_hint2"
android:text="Add"
android:textSize="30dp" />
</RelativeLayout>
Use Linear Layout :
<LinearLayout
android:id="#+id/new_formulas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:textSize="30dp" />
<EditText
android:id="#+id/add_hint2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="#string/add_hint"
android:textSize="30dp" />
<EditText
android:id="#+id/add_hint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/add_hint"
android:textSize="30dp" />
</LinearLayout>
Try this
<LinearLayout
android:id="#+id/new_formulas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/add_hint2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="40"
android:gravity="center_horizontal"
android:hint="#string/add_hint"
android:textSize="30dp" />
<EditText
android:id="#+id/add_hint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/add"
android:layout_weight="40"
android:hint="#string/add_hint"
android:textSize="30dp" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_alignParentRight="true"
android:text="#string/add"
android:textSize="30dp" />
</LinearLayout>
You can adjust weight acoording to your need but remember sum of all weight must be 100
You can achieve what you want using PercentRelativeLayout from the new Percent Support Library. It inherits all the alignment attributes of RelativeLayout and lets you specify dimensions and margins in percentages. Basically a counterpart of the layout_weight attribute of LinearLayout. Read Joanna Smith's #proTip for more info.
First add the dependency in app/build.gradle
dependencies {
...
compile 'com.android.support:percent:23.0.1'
}
Then change your code like this:
<android.support.percent.PercentRelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/new_formulas">
<Button
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add" />
<EditText
android:textSize="30dp"
android:layout_toLeftOf="#id/add"
app:layout_widthPercent="40%"
android:layout_height="wrap_content"
android:id="#+id/add_hint" />
<EditText
android:textSize="30dp"
android:layout_toLeftOf="#id/add_hint"
app:layout_widthPercent="40%"
android:layout_height="wrap_content"
android:id="#+id/add_hint2" />
</android.support.percent.PercentRelativeLayout>
Some tips:
Use sp instead of dp for textSize
Always format the code (ctrl + alt + L) before posting. Makes it more readable.
I am trying to create a layout in XML something along of the lines of http://i.stack.imgur.com/aPoeU.png but I am little confused as to how to position the buttons in that format. I have created the buttons and tried different things like alignParentBottom, alignParentRight etc but I can't seem to get it the way I want it to be. Can someone please help me out?
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button" />
We can manage it in all screen and in all resolution by weight_sum
Paste below code in your xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:layout_weight="0.30"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button3" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.30"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Paste it in your XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2258A2"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.20"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button3" />
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Your code seems correct, but if you want to replicate the size of the buttons then you can't set the height as wrap_content. This will make it as tall as the text, change that to some given value:
android:layout_height="50dp"
Well, in RelativeLayout, as the name suggests, the elements are positioned relative to something else, like the parent view, or another widget in the layout. So you can align the widget in many many ways, like apending it to the right of a view, to the left, above, below, and so on.
Please refer to this link and this other link for more information.
So, if you want to make your buttons look exactly like your example image, try this:
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button1"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:text="Button 3" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:text="Button 4" />
You can simply make the button width to fill_parent instead of align left and right. And the height, you can define the measures you'd like to use.
Btw, I used alignParentLeft and alignParentStart because I don't know which API level you're using, so I've used both parameters, old and new. But the effect is the same, your buttons will always start to the left of the parent view.
EDIT: As the user hasternet suggested, you could also use LinearLayout and get similar results.
EDIT 2: And if you like to change the look of your buttons, try to change the style (Take a look here to know what I mean).
Since you need to set the height of the buttons differently and also it depends on different screen size, you cannot achieve this with Relative Layout.
So go for LinearLayout and weightsum attribute. This way screen big or small, you can achieve your expected UI. Mentioned code below modified.
<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"
tools:context=".MainActivity"
android:Orientation="vertical"
android:weightsum="8" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button"
android:layout_weight="1"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button"
android:layout_weight="3"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button"
android:layout_weight="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button"
android:layout_weight="1"/>
</LinearLayout>
I design a xml( in layout package) in android project, I want my UI run on any android mobile and android tablet, if size of device change my UI shows good, I use LinearLayout for each group, in linearLayout3 I have one textview and one EditText, when I run program I get error from my textview and EditText in linearLayout3,error is:
“Binary XML file line #90: Error inflating class android.widget.EditText”
Please suggest to me a solution for resolving my problem.
Code is:
<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=".MainActivitytest" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:drawableTop="#drawable/r_stop"
android:text="Stop" />
<Button
android:id="#+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:drawableTop="#drawable/r_start"
android:text="Start" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1">
<RadioGroup
android:id="#+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="150"
android:gravity="right" >
<RadioButton
android:id="#+id/Rb_Defualt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:ems="150"
android:gravity="right"
android:text="testdefualt"/>
<RadioButton
android:id="#+id/Rb_Periodic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="150"
android:gravity="right"
android:text="testperiodic"/>
<RadioButton
android:id="#+id/Rb_Maual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="150"
android:fitsSystemWindows="true"
android:gravity="right"
android:text="testmanual"
android:visibility="visible" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2" >
<TextView
android:id="#+id/TxtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="200"
android:textIsSelectable="true"
android:text="Please Enter Time"/>
<EditText
android:id="#+id/Txt_Minute_Periodic"
android:layout_width="0dip"
android:layout_marginLeft="-300dp"
android:layout_height="wrap_content"
android:paddingBottom="#+id/TxtTime"
android:layout_weight="50"
android:layout_marginTop="50dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout3"
android:layout_marginTop="60dp">
<Button
android:id="#+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:drawableTop="#drawable/r_ok"
android:text="ok" />
<Button
android:id="#+id/btn_Config"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:drawableTop="#drawable/r_config"
android:text="config" />
<Button
android:id="#+id/btnkeyManagment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:drawableTop="#drawable/r_save"
android:maxLines="2"
android:text="key" />
</LinearLayout>
</RelativeLayout>
The android:paddingBottom field in your EditText should be a number and not an id.
I have comman header with two button and single textview.
textview is in center of the screen. button 1 is at the right side of the parent and button2 is at the left side of the parent.
now, I want to show text view in the center of the parent not in the center of the space between two buttons.
Text length in the textview can be any thing either it could be 3 words or it could be more than 10 word.
I dont want to overlap textview above the two buttons while length is more than 10 words.
and also I want textview in the center of the screen while there is only 3 words.
When I am using below code it is not showing textview in the center of the screen horizontally when there is only 3 to 4 words but below code also dont overlap while there is more than 10 words.
<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"
tools:context=".Webviewdemo" >
<ImageView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/backbtn" />
<ImageView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/infobtn" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/button2"
android:layout_toRightOf="#+id/button1"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp"/>
</RelativeLayout>
In same way When I am using below code it is showing textview in the center of the screen horizontally when there is only 3 to 4 words but below code overlap while there is more than 10 words.
<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"
tools:context=".Webviewdemo" >
<ImageView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/backbtn" />
<ImageView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/infobtn" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp" />
</RelativeLayout>
So, My question is how can I achive above both thing with single code.
Textview should not overlap while there is more words.
Textview should be in center while there is less no of words.
I hope you all get my problem. if any query plz ask.
<LinearLayout
android:id="#+id/panelIconLeft1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" >
<Button
android:id="#+id/btnHome1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOCATION"
android:onClick="btnHomeClick" />
</LinearLayout>
<TextView
android:id="#+id/txtHeading1"
style="#style/heading_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/panelIconRight1"
android:layout_toRightOf="#id/panelIconLeft1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:singleLine="true"
android:text=""
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/panelIconRight1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" >
<Button
android:id="#+id/btnFeedback1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post Ad"
android:onClick="btnFeedbackClick" />
</LinearLayout
The above code will help you
Use This Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
try this code
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/button2"
android:layout_toRightOf="#+id/button1"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp"/>
EDIT
remove this line
android:layout_centerInParent="true"
try this (without centerInParent)
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/button2"
android:layout_toRightOf="#+id/button1"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp"/>
In my layout I have an admob ad showing with an EditText in the layout. When I focus in the edit text to type something the keyboard appears and moves the admob ad over the edittext and you cant see what you are typing in.
this is my layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="false" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wood_floor" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.tyczj.bowling.adapters.RobotoThinTextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:text="Add Bowler"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#33b5e5"
android:textSize="40dp" />
<FrameLayout
android:id="#+id/line"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
android:background="#drawable/ab_solid_custom_blue_inverse_holo" >
</FrameLayout>
<com.tyczj.bowling.adapters.RobotoThinTextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/line"
android:layout_below="#+id/line"
android:layout_marginTop="20dp"
android:text="#string/bNameTV"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_below="#+id/line"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="#+id/textView1"
android:inputType="textCapSentences" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_below="#+id/editText1"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="#+id/textView3"
android:inputType="textCapSentences" >
<requestFocus />
</EditText>
<com.tyczj.bowling.adapters.RobotoThinTextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/line"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:text="#string/lNameTV"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:layout_marginTop="15dp"
android:checked="true"
android:text="Right Handed" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/line"
android:layout_below="#+id/radioButton1"
android:text="Left Handed" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/line"
android:layout_alignParentBottom="true"
android:layout_below="#+id/radioButton2"
android:onClick="onDoneClick"
android:text="#string/done" />
</RelativeLayout>
</ScrollView>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="IAB_BANNER"
ads:loadAdOnCreate="true" />
</RelativeLayout>
in my manifest I set android:windowSoftInputMode="stateVisible|adjustResize" in the activity but that didnt have any effect
How can I stop the ad from moving when the keyboard appears?
Have you tried the mode adjustPan? That may work better for you. Since you have a RelativeLayout with an element aligned to the bottom of the screen, an adjustResize will always keep that element at the bottom of the screen. You may be able to play with your layout to fix things by laying the ad out as being below the scrollview, rather than aligning to bottom.
Try using a LinearLayout instead of outmost RelativeLayout and add the ScrollView with android:layout_weight="1" and the AdView with android:layout_weight="0":
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0"
android:layout_weight="1"
...
/>
...
</ScrollView>
<com.google.ads.AdView
android:layout_width="fill_parent"
android:layout_height="0"
android:layout_weight="0"
.../>
</LinearLayout>