Using an image as the parent Activity (interface) - android

Is it possible to use an image as the background of the main Activity? (load screen). I then want to put buttons over the image. If this is possible could anyone tell me how I would load the image into my main Activity? Or send me to a site that would go over this?
Thanks :)

I use the following to set a background image for an Activity..
View view = getWindow().getDecorView();
int orientation = getResources().getConfiguration().orientation;
if (Configuration.ORIENTATION_LANDSCAPE == orientation) {
view.setBackgroundResource (R.drawable.bground_land);
} else {
view.setBackgroundResource (R.drawable.bground_port);
}
bground_land.jpg and bground_port.jpg are images in my project's /res/drawable directory.

Related

Change image after click on ImageView

everyone. After clicking on an ImageView, I would like to change it depending on what kind of drawable is currently displayed. To do this, I compare the currently set image in the ImageView with one from the drawable folder. Unfortunately, it is currently the case that only the else branch is executed. The image changes after the first click, but then it no longer switches. What am I doing wrong?
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
var draw = bookmark_site.drawable.constantState
if(draw == ContextCompat.getDrawable(this,R.drawable.ic_baseline_bookmark_filled_24)?.constantState)
{
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_outline_bookmark_border_outline_24))
}
else{
//Only this two is ever executed
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_bookmark_filled_24))
}
}
you can't base on constantState, its not desired to compare drawables as there is a different instance for every drawable, thus your if isn't true never ever. in fact there is no way for getting resource of drawable already set for ImageView. most convenient way would be to keep some boolean flag outside onClick method informing that image is switched or not. optionally you may keep this flag "inside" ImageView using setTag(...) method
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
val switched = bookmark_site.tag != null
bookmark_site.setImageResource(
if (switched) R.drawable.ic_outline_bookmark_border_outline_24
else R.drawable.ic_baseline_bookmark_filled_24
)
bookmark_site.tag = if (switched) null else "SWITCHED"
}
you should keep category or tag along with the image in the model class and then change the image on the basis of that tag or category.

Android partially active Activity

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);
}
};
}

Partial Screen Refresh in Android

I have a Activity which has 2 components
Image view
Text view
Here the image in the image view is changed using a timer. This works fine. The text is scrolling text is marquee. Individually these works fine.
My problem is when the image gets changed it also reset the marquee string.
Is there any way to avoid this?
Is there any way to refresh the display partially in android.
Edit
File imgFolder = new File(config.AppDataDir+config.MainImagesDir);
if(imgFolder.isDirectory()) {
File[] imageList = imgFolder.listFiles();
if(imageList!=null) {
if(imageList.length>0) {
if(imageList[currentImageNum].exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imageList[currentImageNum++].getAbsolutePath());
ImageHolder.setImageBitmap(myBitmap);
}
if(currentImageNum>=imageList.length)
currentImageNum =0;
}
}
}
Thanks
Normally, changing the source of an ImageView should not have any implications on other views. Most probably when changing the image you are doing something that is reflected on the TextView too.

Android gallery onItemClick show larger image

I've just finished copying the android walkthrough for the gallery (http://developer.android.com/resources/tutorials/views/hello-gallery.html) and was wondering how I would go about having the thumbnail photo scale into a larger one within the onClickItem method.
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(ProjectPreview.this, "" + position, Toast.LENGTH_SHORT).show();
}
Currently it just shows the position as depicted in the Toast. Could someone point me in the right direction or show me a simple way?
Thanks in advance.
EDIT: Added this method and called it in the onItemClick sending it the position. And it works.
public void showLarger(int position){
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setLayoutParams(new Gallery.LayoutParams(200, 100));
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageResource(mImageIds[position]);
}
Use a Dialog with a layout which contains an imageview, and set its source case the position you have and with the size you want.
I would use a Dialog. You can create a new activity and have an ImageView in its layout, and disguise that Activity as a Dialog. See this question for how to do all that: Android Image Dialog/Popup
As mentioned above, you can use a Dialog to display the larger image or you can just fire an intent that would start a regular Activity(You will have to add the dialog Theme to your Activity if you do it as described as above).
You'd have to do something like call v.getItemID(position) and store that in a String, then pass that into your next Activity.
Either way, you'll have to pass in some data to your Activty that will host the ImageView, and then use that data(path, uri, etc) to inflate your larger ImageView. Just don't forget to add the theme to your manifest if you want use the Dialog route.

Android: Any idea why cannot load drawable object?

There are several custom-made graphic objects (.png files) included in the project inside res/drawable map.
All elements are normally loaded and displayed in the user interface except two icons and so far haven't figured out what causes the problem.
The code which doesn't affect the user interface as it should is the following:
if (settings.isMute()) {
muteIcon.setIconImage(R.drawable.ic_volume_off_small);
} else {
muteIcon.setIconImage(R.drawable.ic_volume_small);
}
In both cases there is only ic_volume_small displayed on the screen and the Variables window in the IDE displays the following:
R.drawable.ic_volume_small = Class not
loaded : net.client.android.R$drawable
R.drawable.ic_volume_of_small = Class
not loaded :
net.client.android.R$drawable
The method (member of IconImage class) which should change the icon image is the following:
public void setIconImage(int imageFromResources) {
iconImage = BitmapFactory.decodeResource(getResources(), imageFromResources);
iconWidth = iconImage.getWidth();
iconHeight = iconImage.getHeight();
invalidate();
}
Does anyone know what could cause the described problem?
Thanks!
Assuming what you want is the drawable from the Android framework and not your own, you'd want to use:
android.R.drawable.ic_volumne_off_small
rather than
R.drawable.ic_volume_off_small
(notice the android prefix).
If you recently added file, please refresh (press F5) the drawable directory and R is going to be generated again. Once R is regenerated, you can use the newly added resource.

Categories

Resources