I'm a bit new to Android, therefore question may sound ridiculous, but:
Is there any way to make a button in form of smthg? There ate tons of apps, where you press on a tree or some house and some action starts. How is that made?
Not action, but form. Or there is no way to make button NOT rectangular?
You are talking about imagebutton.Image button documentation
For example creating a button with tree background.Example:
<ImageButton android:src="#drawable/tree.png"></ImageButton>
Yes you can do that. Considering your trees and house as an image.
ImageView im;
im = (ImageView) findViewById(R.id.image_of_tree);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//perform some action
}
});
Off-course When you get inflated your layout and your view(can be any view) is available by id. your button can be an ImageView, ImageButton etc. just find the id for your view and set whatever event you want to listen by this view.
Related
I have an activity A. I am creating a kind of tutorial for user for this activity, to teach him how he can use the app on that screen.
For that, my requirement is :
I want to blur all the views of the activity except one view. I want to prompt user to click on that view through a hand image pointing at that view.
Nothing should happen if the user clicks on the blurred/greyed out area, but if he taps on that particular active view, it should react to that touch.
I was thinking of using a full screen fragment for this. The Fragment will take the following input from the activity :
for what coordinates, is should not blur the screen and pass the touch event to the activity
the coordinates on which it should show that pointing hand image.
After from these coordinates, the fragment background would be blur.
I wanted to confirm if that's possible, to make the fragment partially active, i.e. delegate it's touch events to the activity for a particular view of the activity.
Also, please let me know if there is any other better approach of achieving the same thing.
Edit1 :
Thinking of using a fragment here, because I'd want this type of behaviour on different screen in future. In that case, I'd make that fragment generic which takes some inputs (as described above) and use it on different screens.
There's a very good library called SCV which does what you're trying to achieve, you're able to customize the styles for it too. I've used this for first time the app is opened to show the user a tutorial.
According to their Github
The ShowcaseView (SCV) library is designed to highlight and showcase specific parts of apps to the user with a distinctive and attractive overlay. This library is great for pointing out points of interest for users, gestures, or obscure but useful items.
Further Reading:
Android Arsenal - Showcase Views Tutorial
ShowCaseView on Android - Indipendev
I found it much easier to include an 'extra' layout around the UI of my activity, and then to add a highest-z grey mostly-transparent filter to it and put the instructions on that.
Each "step" of the instructions was a different layout that was dynamically loaded into that layout container as they clicked. (Just another approach)
The 'container' layout is a: FrameLayout
then in my Activity I have: (ignore bad naming)
private void addOverlayLayout() {
frameLayout = (FrameLayout) findViewById(R.id.framelayoutInner);
frameLayout3 = (FrameLayout) findViewById(R.id.framelayout3);
frameLayout3.setBackgroundColor(Color.DKGRAY);
frameLayout3.setAlpha(0.3f);
// Dynamically create a relativelayout which will be appended to framelayout
relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_1));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_2));
if (FullscreenReaderActivity.isFirstRun) {
displayNextGuide();
}
}
public void displayNextGuide() {
// clean relative layout if it has views
relativeLayout.removeAllViews();
// clean frame layout if it has child (safe if empty)
frameLayout.removeView(relativeLayout);
if (!isFirstRun) {
return;
}
if (instructionOverlays.size() > 0) {
runOnUiThread(instructionOverlays.get(0));
instructionOverlays.remove(0);
} else {
frameLayout3.setBackgroundColor(Color.TRANSPARENT);
frameLayout3.setAlpha(1.0f);
}
}
public Runnable createSimpleClickInstruction(final int resource) {
return new Runnable() {
#Override
public void run() {
getLayoutInflater().inflate(
resource,
relativeLayout,
true
);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayNextGuide();
}
});
frameLayout.addView(relativeLayout);
}
};
}
I have a ScrollView containing a TextView. I linkify parts of that TextView with
Pattern pattern = Pattern.compile("MyLink");
Linkify.addLinks(textView1, pattern, "mylink://");
I habe an Intent filter in my Manifest for mylink:// so a new Activity is opened when MyLink is clicked (as described in this question).
This works most of the time, sometimes though a click on the MyLink portion of the TextView doesn't open the Activity but only scrolls the ScrollView in which my TextView resides to the top.
Where does this behaviour come from and how can I fix it?
If you are attempting to open a link that leads to your current activity, it might be recreating the same activity and gives the sensation that it's scrolling up. Probably you want to modify your manifest and set the activity's launchMode attribute to singleTask
Why dont you use TextViewWithLinks?
With that you can have two types of click on the textview,
1. On TextView
2. On Link TextView
String text = "bananas on www.bananas.ba and Google";
TextViewWithLinks textview = new TextViewWithLinks(this);
textview.setText(Html.fromHtml(text));
textview.linkify(new TextViewWithLinks.OnClickLinksListener() {
#Override
public void onTextViewClick() {
// Do whatever you want
}
#Override
public void onLinkClick(String url) {
// Do whatever you want
}
});
//SET Colors
textview.setLinkColors(Color.RED, Color.BLACK);
setContentView(textview);
Let me know if this is not resolving your issue.
Enjoy Coding... :)
i have an image with some text that i have the audio for.
I want to know how i can set it up so the user can the the button and the audio will start to play.
Thanks!
I have a solution I think it might work.
Fisrt of all, instead of working it an ImageView would be easier to work with ImageButton.
So, in your layour XML file, select ImageButton and create one using your image with the text.
also you might want to set a null bacground for your ImageButton, this is the code
android:background="#null"
Next, in your java class select the sound and the ImageButton and set it to play your sound when clicked.
would be like this
final MediaPlayer yourSoundName = MediaPlayer.create(Lotr1.this,R.raw.yoursoundfile);
final ImageButton yourImageButtonName =(ImageButton) findViewById(R.id.yourImage);
yourImageButtonName .setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
yourSoundName.start();
}
}
});
Any quenstion I would be glad to help
I want to obtain a similar tab effect with the button bar of twitter app.
I wish to click on a button and change the view down. I can switch activity but I think It's wrong, or not?
The top bar have to be fix like a frame. Like this:
Ok now I post a part of my idea (i found something similar here: http://www.talkandroid.com/android-forums/android-development-answers-tutorials-code-snippets/1515-how-i-open-image-imagebutton.html)
code:
newsButton = (ImageButton) findViewById(R.id.news);
newsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// on click go to the news view (no activity yet)
setContentView(R.layout.news);
}
});
Like in the Google IO App?
If so, the Source Code is freely available here.
Okay, a little tour on how Google does it:
The activity_home.xml-layout
includes (Line 21) the
actionbar.xml-layout (This is done
in every Layout so the Actionbar
must not always be duplicated).
The actionbar.xml-Layout
creates a LinearLayout for
the UI-Elements.
Then, for example the
HomeActivity-Activity sets
the content view to the
activity_home.xml-layout, receives
an ActivityHelper-class and calls
its setupActionBar()-method.
The mantioned ActivityHelper-class
is in the hg/ android/ src/ com/
google/ android/ apps/ iosched/
util/-package and has the
setupActionBar()-method which
creates the Action bar.
This might be easier then it looks. Read your way through the Source Code and try it yourself.
I think these controls are Radio Button/ Radio Group with customization.
Im working on changing the image being shown when I have my ImageView Clicked. Im trying to use a similar code that I used for accomplishing this with a TextView but I can't seem to find the right terms to get it to work. Here is my current code. Thanks
electronconfiguration.setOnClickListener(new View.OnClickListener() {
public void onClick(View drawable) {
if (drawable.equals(R.drawable.element_el))
electronconfiguration.setImageDrawable(R.drawable.aluminum_el);
else if (drawable.equals(R.drawable.aluminum_el))
electronconfiguration.setImageDrawable(R.drawable.element_el);
}
});
Why don't you use a ViewSwitcher, it's designed to switch between two views
drawable probably doesn't equal R.drawable.element_el. R.drawable.element_el is probably some random implementation of the image. Try drawable.getId().equals(R.drawable.element_el). I've never tried this so I have no idea