Hi
in my app contain a Button ,currently i customized using xml for changing the background and it work fine in 2.1 but when am reach to 2.2 ,The button is hard to hit ,how can i solve the issue?
my button xml code given below
<Button
android:id ="#+id/Button_Continue1"
android:background="#drawable/continue_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
/>
If your button is small and hard to hit use an Inset Drawable. You set the drawable you want to have displayed and the inset on the left, top, right and bottom. The inset is like some kind of margin but clickable.
Here is an example.
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/your_drawable"
android:insetTop="10dp"
android:insetRight="10dp"
android:insetBottom="10dp"
android:insetLeft="10dp"/>
That will make your button to be 10dp taller on every side for touch events. The look of your button will not change.
Now in your layout you don't set your original drawable as the Button's background anymore but the new Inset Drawable.
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 am trying to figure out how to fill background color of a card from bottom to top based on the processing of a particular task. I want the background color of card to be filled slowly from bottom to top approach based on some timer or processing. How to achieve this scenario. Please help me here.
You can easily achieve this with Clipping. This is a modified code from Android Drawable Resources.
First specify the clipping view in XML file saved at res/drawable/clip.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/android"
android:clipOrientation="vertical"
android:gravity="bottom" />
It basically means that whatever drawable you assign to this xml, it will be clipped vertically starting from the bottom. Then in your cardview XML set this drawable as a background.
<CardView
android:id="#+id/cardview"
android:background="#drawable/clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Then in your activity do something like this
CardView cardview = (CardView) findViewById(R.id.cardview);
ClipDrawable drawable = (ClipDrawable) cardview.getBackground();
drawable.setLevel(/*your level*/);
Regarding the level:
Increasing the level reduces the amount of clipping and slowly reveals
the image.The default level is 0, which is fully clipped so the image
is not visible. When the level is 10,000, the image is not clipped and
completely visible.
Hope this helps.
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
I intend to make a custom seekbar, with my own set of images for the background and progress drawables. I've made 2 9-patch images, one each for the background and progress drawables. Here is the code I am using for the progressDrawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/background"
android:drawable="#drawable/experience_seekbar_background" />
<item android:id="#+id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left|top"
android:drawable="#drawable/experience_seekbar_progress" />
</item>
</layer-list>
Also, here is the xml definition of my seekbar:
<?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:orientation="vertical"
android:padding="5dp" >
<SeekBar
android:id="#+id/experienceSeekBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:max="2"
android:progress="1"
android:progressDrawable="#drawable/experience_seekbar" />
</LinearLayout>
The problem is, the progress portion of the drawable is drawn below the background portion, instead of directly overlapping it.
Expected/Desired: (http://i.imgur.com/C27dQZK.png)
Actual Output: (http://i.imgur.com/ftNlMMI.png)
Having gone through tens of custom seekbar tutorials, I am still stuck at what could I possibly be doing wrong here. It might be something very trivial, but I am just not able to spot it.
It turns out there were several things at play here.
1) I used 9-patch images for the seekBar background and progress drawables. However, I did not know that 9-patch images also have, apart from the stretch lines at the top and left edges, padding lines at the right and bottom edges. These define the content area of the image, and the area left automatically becomes the padding area (i.e. the non-content area counts towards the padding of the view for which this image will be used as background). Also, if no padding lines are specified, android uses the stretch lines as padding lines.
2) In a layer-list, the padding of items stacks up. This means that if the first item in a layer-list has some padding, this padding will add up to the padding of all items after it, the second item's padding adds up to padding of all items below it, and so on.
So in my case, the 9-patch image used as seekBar background (first item in the layer-list) got some unintended padding (because stretch lines were being used as padding lines), and the seekBar progress drawable got this padding added to its own unintended padding (again, since it was a 9-patch) which caused it to be displayed below the background.
To correct this, I changed the 9-patch images and set the padding lines to their full length i.e. I defined the entire image as the content area. There was no other option, as any padding would lead to twice the padding for the progress drawable, displacing it from its intended position (which is exactly above the background drawable).
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