Image background in imageView not changing by using setImageResource() - android

I am trying to change the background image of an ImageView for different options on Radio buttons in the calling activity. The calling activity passes the parameters in a bundle to rendering activity and I am receiving the parameters properly (I get Curve1 and Curve2 as parameters in the rendering activity after making choice on radio buttons). But I am not able to change the image background either by setImageResource() or setBackgroundResource().
Here is the snippet doing that:
Rendering Activity
private void validateImage()
{ wave = (ImageView) findViewById(R.id.image_wave);
switch (extras.getString("Curve"))
{
case "Curve1":
wave.setB (R.drawable.sinewave1);
break;
case "Curve2":
wave.setImageResource(R.drawable.sinewave2);
break;
}
}
activity.xml
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="shantanu.concussion.usu.concussiontest.TestActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image_wave"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Can anybody suggest what am I missing?
Thanks :-)

Try to call invalidate manually. Violently change least something. You need to understand where the problem is (wrongly chosen View, View does not change) Try to change another property for example.
May cause in the resource. (e.g., it is transparent)
Check the entrance to the branch switch. (perhaps wrongly runs comparison)
Change the properties through the editor. Maybe something prevents display background.
It's just a stupid mistake.

try
wave.setImageDrawable(getResources().getDrawable(R.drawable.sinewave2));

Related

Within a new activity, ViewImage does not show an image

I created an Activity, which I load from a fragment within my "main" activity.
Within this new activity I placed an ImageView with an image from my drawable directory (I have about 10 other drawables I present the exact same way in the main activity).
The problem is that although in the IDE I see the image, it is not displayed in run time (via the simulator).
-- to clarify, the image is static hence the difference compared to other questions (I'm not loading it by code).
Any ideas on how to solve it?
My hunch is that it relates to the fact it's a new activity, but I'm new to android development, so I can't base it on any knowledge...
I did not add any code to the java section of the activity (didn't touch any "on.." nor added functionality to this specific file)
P.S. I tried cleaning the project, tried presenting an image that is presented on the main activity, and restarting anything that I can... nothing helped :(
My Activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="reducted">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/manageSubscriptionScreenLogo"
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="#drawable/managesubscription_header" />
</LinearLayout>
</RelativeLayout>
It doesn't matter I take the ImageView and place it outside of the LinearLayout (and remove it), or change the size to wrap_content or match_parent (I tried all combinations), the image is not presented.
How it looks in the IDE:
And how it looks in run time:
Try using android:src="#drawable/managesubscrip_header" instead of app:srcCompat="#drawable/managesubscription_header" in ImageView to avoid automatic scaling of the drawable.
Let me know if it helps

Image not resizing in Android Studio's emulator

I'm using a USB connected device to run my app. In this practice app, I used an Android logo image to implement the zooming feature. When I try to change the image dimensions in the activity, it does not show the changes when the app imported into the usb device is loaded. The logo is simply zoomed to fit. But I want it to display the image with the way I see it in the Design view: http://i.imgur.com/hcaOHjW.png
I forked the code from this source to implement the zooming: http://www.c-sharpcorner.com/UploadFile/88b6e5/multi-touch-panning-pinch-zoom-image-view-in-android-using/
It does not have the double tap zoom in and out feature so let me know what I can add to implement that
And here is the code for my activity:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.test.firstapp.Login"
android:id="#+id/activity_activity2"
android:focusable="false"
android:nestedScrollingEnabled="false">
<ImageView
android:layout_width="180dp"
android:layout_height="220dp"
android:id="#+id/botImg"
android:src="#drawable/bot"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"/>
in your java class try creating this view and then try changing the height and width parameters .inflate this view dynamically and assign parameters in the java itself dont use xml for this view .

How to use two activites in a single screen in android?

I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<FrameLayout 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"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.

listView pointToPosition is incorrect 'half the time'

I'm trying to use list.pointToPosition to determine which list item position the user's finger is touching. My list contains a TextView followed by a ListView in a RelativeLayout. The problem that I've discovered is that pointToPosition seems to ignore the fact that I've got a TextView and returns the wrong position of the item.
Let me explain further with some pictures and some samples of what's being returned. I've enabled the debug feature that let's me track finger touches. You'll see it in the blue line in the following images.
In the following image, you'll notice at the top of the screen is a TextView labeled "LIGHTS". Just below that is my ListView. Look at the blue line that tracks my finger touch and slide - you'll notice that it starts near the top of the row labeled "Front Porch". Performing "pointToPosition" at that location returns the correct value of 4.
Now however, if I start my touch half way down that same row, pointToPosition returns a value of 5. You can see my touch/drag in the following screen shot.
When I change the size of my TextView to have a height of 1dp, the problem goes away; thus I'm lead to believe that the pointToPosition method assumes that my ListView is the only thing on the page.
I could implement a hack and deal with this by calculating the offset caused by the TextView, but my feeling is that this will eventually come back to bite me when this bug (assuming it's a bug) gets fixed. What I'm wondering is if there's a proper way to fix it.
Source Code Samples:
layout.xml
<com.mls.util.RelativeLayoutTouchInterceptor 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/app_bg"
android:id="#+id/rlRoot" android:clickable="true"
tools:context=".DeviceListActivity" >
<TextView android:id="#+id/list_header"
style="?android:listSeparatorTextViewStyle"
android:text="Lights"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
<ListView
android:id="#+id/dlList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="65dp"
android:layout_below="#+id/list_header"
android:layout_alignParentLeft="true">
</ListView>
</com.mls.util.RelativeLayoutTouchInterceptor>
RelativeLayoutTouchInterceptor.java
public class RelativeLayoutTouchInterceptor extends RelativeLayout {
...
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downStart = MotionEvent.obtain(event);
ListView list = (ListView)this.findViewById(R.id.dlList);
int position = list.pointToPosition((int)downStart.getX(), (int)downStart.getY());
}
...
}
The reason this happens is because you're overriding the onInterceptTouchEvent of the RelativeLayout, not the ListView. It's reporting the y coordinate within the layout itself, which includes the TextView.
If you want to get the coordinates relative to the ListView only, you could just override it for the ListView itself. It is an indirect subclass of ViewGroup, so it has access to the method.
There's no bug that will be fixed to worry about, it's working like it should.
I went ahead and added the perceived hack to fix this of adding the height of the TextView to the Y position provided to pointToPosition

Android -- Hiding Views

Okay, so I've done some looking around and I see how you are SUPPOSED to do it, but for me, it is just not working.
I need to be able to set the alpha of a RelativeLayout both in XML and in code. For my XML, I have the following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/player_controls"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:alpha="0.0">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/player_controls_touch_me"
>
</RelativeLayout>
</RelativeLayout>
I get the error: no resource identifier found for attribute 'alpha' in package 'android'
Also, based on the Android documentation, I should be able to call setAlpha(double) on any View object, but when I try to make that call on a RelativeLayout it tells me that this method is not defined for this object.
Why am I not able to control the alpha transparency for a RelativeLayout object in Android? Am i missing something? Thanks!
Update
Although using the visibility property works, it prevents me from be able to click on the ViewGroup. This is important for me because I am utilizing the OnTouchListener of the ViewGroup.
What I am trying to do is to have a layer with media controls, initially hidden. when the user taps anywere on the screen, I want the controls to fade in and when they tap the screen again I want the controls to fade out. I have this part already working. I am using a viewgroup that sits over-top my entire application with an OnTouchListener attached that can determine if it has or hasn't been touched. My problem is that after the animation runs to fade out the controls, they re-appear. If I use #Hydrangea suggestion, I can have it fade out and immediately made invisible. This gives me the desired effect, but then the ViewGroup is unclickable and the user cannot get the controls to come back (or go away, depending on what we decide to do first).
I hope this makes sense.
You'll want to use a alpha animation to fade things in and out. This will maintain your touch events for your layouts. Here's an example
public class Main extends Activity {
/** Called when the activity is first created. */
private boolean mShowing = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.textview).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mShowing){
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setFillAfter(true);
arg0.startAnimation(animation);
} else {
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setFillAfter(true);
arg0.startAnimation(animation);
}
mShowing = !mShowing;
}
});
}
}
Here's the accompanying xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:clickable="true"
/>
</LinearLayout>
Unless you need levels of alpha between 0 and 1, I'd suggest, if you truly want to make this item invisible, to use setVisibility();
android:visibility="invisible"
I checked out the android:alpha line, and my ide doesn't find it either. I can't guess why, though... the documentation seems pretty clear.
The alpha property is new in Android 3.0, and it's not the most efficient way to hide a view. Use View.setVisibility() or android:visibility to achieve what you want.
You can set alpha by setting the (background) color i guess. Color values can be in the format of #aarrggbb (alpha, red, green, blue).
You can add to the right answer the following option:
animation.setDuration(xxx);
To each animation instance. In this way your animation will look better.
Based on your discription, you should be able to create a view that contains only the relative layout and have the onClickListener set to it. This way you can set the visibility of the relative layout to invisible, but still register a click.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/clickable_layout"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<RelativeLayout
android:id="#+id/player_controls"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/player_controls_touch_me"
>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
Use onTouchEvent in Activity, and then you could get touch event to control to your RelativeLayout even if it is "invisible".

Categories

Resources