Set background drawable (Programmatically s)? - android

In my application I have something like this:
Button playBtn = (Button) findViewById(R.id.play);
ProgressBar pb1 = new ProgressBar(this);
playBtn.setBackgroundDrawable(pb1);
But this is not working. And I know why, - pb1 is not a drawable. But how otherwise I can set the background of my play button to the progress bar? I use a programmatic way of creating a progress bar and I have a play button in my .xml resource.

Ah, you really can't do what you're trying to do: there is no way to use one View as the background of another View.
What you can do is something similar to what the Android battery meter in the notification area does: make a number of drawables (I think the battery meter uses 5 or 6 different images, 1 for each stage of the recharge process) and then use a timer or external events to switch the background drawable for your button: this simulates a progress meter.
Let me know if that helps.

Make transparent button (background="#null") and place it on the ProgressBar using RelativeLayout or FrameLayout.
This is simpler than it seems:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:textColor="#android:color/white"
android:text="asdasd"/>
</FrameLayout>
Make the same in code if you want.

Are you sure you need the Button here? Mb it's enough to place the ProgressBar only and make it clickable by setting onClickListener.

Related

How to create speed animation bar in Android?

I will be glad to get some help with how to do a speed animation bar for android app. Something like what is shown here so it fills up automatically but for an app.
So far I found this code, which pretty resembles what I was aiming for but
I don't want the animation to show on a pop up, but as part of the page.
If you are not sure please point me to the right source so I will figure out how this works.
Any help would be much appreciated:)
Add progress base in your activity layout (R.layout.main);
<ProgressBar
android:id="#+id/mybar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp" />
And instead of doing this
progressBar = new ProgressDialog(v.getContext());
do this after setContentView();
progressBar = (ProgressBar) findViewById(R.id.mybar);

Custom seek bar with thumb and progressDrawable

I try to customize seek bar like below
But problem is that
-thumb image not set with text
-thumb position can not set above seekbar.
-android:progressDrawable should not repeat as below
My Code is as below:
<SeekBar
android:id="#+id/volume_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:max="100"
android:progress="20"
android:progressDrawable="#drawable/bar"
android:secondaryProgress="0"
android:thumb="#drawable/greenarrow" />
I've tried doing this, but I ran into problems with response time the way I did it. Having to move the margins took a lot of processing power and time. It was close to 400-500ms. So I had to remove it.
However, here are the steps I took:
First, I would suggest creating a frame layout to hold both the SeekBar and a TextView. Then, depending on the percentage of the progress bar, you can set the TextViews layout property margins.
Does that make sense?
If you would like specific code, I can get that to you, but google does pretty good too. :)
Option B: Android seekbar with custom thumb having dynamic text inside it

how to achieve Google Play Music-like button look in my app?

Hi I have been inspired by a Google Music app and been trying to achieve the same effect on the button when it is pressed. The shot will help u understand:
SO here the button gets that semi-transparent nice-looking effect. Unfortunately, i ve gone nowhere with it, i just managed to similar button but now it doesnt change color when i press it? Im really stuck and need help, im a novice in android dev, i have been trying selectors but when I set a selector to button background the app fails at runtime.
Here is a screenshot of my button:
For this button, to achieve the shadow I used the card_background.9.png file so it also changes the whole button background to white. I assume what is why the button looses the default pressed state.
XML code for the button:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:drawableLeft="#drawable/facebook_icon"
android:background="#drawable/card_background"
android:padding="15dp"
android:textSize="20sp"
android:drawablePadding="10dp"
/>
You can create your custom button with creating a custom style. In this style you can define different images for different button states (for example enabled, pressed...). Take a look at this tutorial.

Background of my custom button isn't transparent

I created a custom button and I saved it as a .png with a transparant background.
this is the code in xml
<Button
android:id="#+id/btnReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:drawableTop="#drawable/report" />
and when i run it in the emulator I get my button with a grey background
(I cannot post images yet, because my reputation is too low. Apologies for this)
How do I make the background transparant?
Thanks.
Vincent
You should be doing
android:background="#drawable/report"
is that image "report" only transparent? You donĀ“t need to set a transparent image, just set
android:background="#android:color/transparent"
to your button.

Set clickable areas over a background image

I'm developing an Android 2.3.3 application that will work on mobile and tablet devices.
I have a main view with a background image. This image has buttons and other texts and I want to make a clickable area over those buttons. They are not real buttons, those buttons are part of the background image.
How can I do that?
Maybe I can use an ImageButton without image because I don't know how to set a button transparent.
You can use a Framelayout as the click area and just use ImageView as an indicator.
For example :
<FrameLayout
android:id="#+id/play_button_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/button_frame_colors">
<ImageView
android:id="#+id/play_button_indicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:src="#drawable/button_indicator"
android:background="#android:color/transparent"/>
</FrameLayout>
create your custom ImageView, define regions (rects or coordinates) and listen for onTouch events and iterate trough your list when touch event occurs and react if user has touched a defined region
You can also place a layout with no background. It will work for sure as you want.

Categories

Resources