I want to make a button, where left right top and bottom paddings will be same.
But I get something like this:
Here is a layout:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/people_number_label"
android:text="#string/number_of_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/people_number"
android:layout_toRightOf="#+id/people_number_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
So the way a Button works is its a text view with a background drawable. That drawable is the one you're seeing. The text will be centered within that drawable, as you see here. If you want the text to appear in line with the prompt, a Button isn't going to work for you. You're better off just making it a TextView and setting an xml background drawable with a rounded rect so it looks like a button, but it actually isn't.
Another thing you can try to do it align_baseline the two view. That should align the text, but may push the prompt down the screen
Related
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
I'm using actionbarsherlock.
In order to get action modes on bottom(action modes on bottom) I use
android:uiOptions="splitActionBarWhenNarrow"
But I need to add a button to main action bar, so I use custom view.
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_view, null);
getSupportActionBar().setCustomView(customNav, new
ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
Here is custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_day"
android:onClick="onMoreClick"/>
It doesn't metter if I use ImageButton or ImageView with the same properties, so I'll talk about ImageButton.
If I use the ImageButton, I get what I want except of that background is gray.
If I add a line to previous custom_view.xml, in order to get transperent background(or use ImageView instead)
android:background="#00000000"
The alpha in icon will be "cropped" and size of button will change.
The question is. How to get button with such sizes as on first image(with default gray background) but with transperent background?
One of the ways to solve a problem is to make a color of the button's background the same as a color of actionbar and it will look like transperent, but I don't think it is a good solution.
You can use image with transparent background. It can be done in photoshop.
Have you considered using an ImageView instead of an ImageButton? Just set your required image in the imageview and use an onclicklistener to detect clicks like you would do for an ImageButton.
Additionally, you can also just specify the width of your ImageButton and set the padding as required (this is if you want to use android:background="#00000000").
i think that you can add to the button
android:background="yourimagethere"; or maybe android:background=NULL
and it will solve the problem
I would like to extend the UI of AutoCompleteTextView. The Functionality is fine, all I need is to add an button to the right that looks like a drop-down button. Sadly AutoCompleteTextView has a 'natural' margin that I can't reduce to 0.
What can I do now?
Dose I have to overwrite onDraw() & onMeasure() to archive my goal (is there an easier way)?
You could put both AutoCompleteTextView and button onto FrameLayout, add some extra margin right to AutoCompleteTextView to make FrameLayout slightly bigger, and align button to parent right. In fact, these 2 views will interfere, but for user they will appear one next to the other w/o any margin.
Another option could be to set custom background to AutoCompleteTextView (probably modified original one taken from Android source with removed margin).
Just remembered that you can supply negative margin. You can put both views onto LinearLayout and set left margin of button to -5dp for example. However, you will still have to supply custom marginless background for button.
you can use RelativeLayout to put Button to the right of AutoCompleteTextView
Sample
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_close_pressed"
android:layout_alignParentRight="true"
android:id="#+id/myBtn"
></Button>
<AutoCompleteTextView android:id="#+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_toLeftOf="#+id/myBtn"
/>
</RelativeLayout>
As you can see the picture below that when there is no item in the list it shows nice icon in the middle of the screen with a text. I can get only a text shown if the listview is empty with this code in my custom layout :
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
adding this element in my layout show a text when the list is empty. I would like to show an icon instead and also it should be in the middle of the screen. Instead of a textview if I use a imageView again it goes top of the screen. what is the best approach to this? I might be able to get it as I like using imageview and textview and both with android:id="#android:id/empty attribute. or is that possbile to have layout with the android:id="#android:id/empty attribute and place imageview and textview inside?
Sure, just use a layout. LinearLayout might work well in this case. You'll probably have to set the gravity to center. It seems you already had the right idea, I'm not sure why you didn't just try it out? You would have had it!
I have a linearlayout which have a textbox(multiline, nearly 5 lines) and image view. Is it possible to draw a image on textview(overlapping)?
Note: I have to specify the coordinates of the image, which are not static, and may be anywhere above text.
Something like this mockup:
I think it can be achieved using RelativeLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/Text2display"
android:textColor="#EEDCAA" />
<ImageView
android:id="#+id/choose_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/user2" />
</RelativeLayout>
By placing the TextView block above the ImageView, it ensures that the image view overlaps the TextView. Now, based on your requirements and position, use the following commands from the link :-
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
You can align left right, top and bottom. Use negative values to navigate the ImageView, if ur using align bottom and stuff.. This will make it to overlap. Please let me know if this was helpful
Is there any specific reason for Linear Layout?
You can do this easily using RelativeLayout . You can have an ImageView overlapping TextView Unless there is a specific reason for using LinearLayout .
If you really (really) need to use LinearLayout, you can subclass TextView and override onDraw to draw your image.
In all your xml files, should define the background color for it, it will solve the problem :
Add this android:background="#android:color/black" in to the View tag you defined.