I'm using Fredrik Bornander's SandBoxView from this tutorial
http://www.codeproject.com/Articles/319401/Simple-Gestures-on-Android
It works efficiently for an imageview but I want to use multiple imageviews. Here is my code under GestureActivity but It still works for only one imageview.
Can anyone help me about this?
public class GesturesActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.d);
SandboxView s = new SandboxView(this, bitmap);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
SandboxView s2 = new SandboxView(this, bitmap2);
RelativeLayout ly=new RelativeLayout(this);
ly.addView(s);
ly.addView(s2);
setContentView(ly);
}
The problem is that that control isn't being laid out with any LayoutParameters and it will always take all of the available space if that is the case, so you get two SandboxViews overlapping and that means only the top one receives the touch events.
To fix this (without too much hassle) change the layout to make sure the available space is split between the two controls;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.d);
SandboxView s = new SandboxView(this, bitmap);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
SandboxView s2 = new SandboxView(this, bitmap2);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
// Note the weight parameter of 1, making sure they split the space between them
LayoutParams lp1 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
LayoutParams lp2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
l.addView(s, lp1);
l.addView(s2, lp2);
setContentView(l);
}
This is easier to do if you add the three constructors to SandboxView from it's base class View, that will allow the control to show up in the designer and it's a lot easier to play around with the layout in there (which is admittedly what I should have done in the first place when I wrote the article).
Related
I was tryng to create something really simple. My idea was an HorizontalScrollView with some different coloured cards.
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = (LinearLayout) findViewById(R.id.modeList);
Color[] colorList={Color.BLUE,Color.GREEN,Color.RED,Color.ORANGE,Color.AQUA};
String[] nameList={"Film","Music","Brand","Animals","People"};
for(int i=0; i<5; i++) {
drawCard(parent, nameList[i], colorList[i]);
}
}
Here is the method i use to display the cards
private void drawCard(LinearLayout parent, String cardTitle, Color color){
int middleColorID, darkColorID;
switch(color){
case RED: {
middleColorID=R.color.middleRed;
darkColorID=R.color.darkRed;
break;
}
case BLUE: {
middleColorID=R.color.middleBlue;
darkColorID=R.color.darkBlue;
break;
}
case ORANGE: {
middleColorID=R.color.middleOrange;
darkColorID=R.color.darkOrange;
break;
}
case PURPLE: {
middleColorID=R.color.middlePurple;
darkColorID=R.color.darkPurple;
break;
}
case CYAN: {
middleColorID=R.color.middleCyan;
darkColorID=R.color.darkCyan;
break;
}
case AQUA: {
middleColorID=R.color.middleAqua;
darkColorID=R.color.darkAqua;
break;
}
case GREEN: {
middleColorID=R.color.middleGreen;
darkColorID=R.color.darkGreen;
break;
}
default: {
middleColorID=R.color.middleGreen;
darkColorID=R.color.darkGreen;
break;
}
}
//create the card and assign background
RelativeLayout child = new RelativeLayout(this);
child.setBackgroundResource(R.drawable.card);
child.setElevation(30);
//set margin and layout parent
RelativeLayout.LayoutParams parentParams = new RelativeLayout.LayoutParams(400, -1);
parentParams.setMargins(10,0,10,0);
parent.addView(child, parentParams);
//create the wave_outline
RelativeLayout waveOutline = new RelativeLayout(this);
Drawable waveDrw = getDrawable(R.drawable.ic_newwave_outline);
waveDrw.setColorFilter(getResources().getColor(darkColorID), PorterDuff.Mode.SRC_ATOP);
waveOutline.setBackground(waveDrw);
RelativeLayout.LayoutParams waveOutlineParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 120);
waveOutlineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
waveOutlineParams.bottomMargin=120;
child.addView(waveOutline, waveOutlineParams);
//create the wave
RelativeLayout wave = new RelativeLayout(this);
waveDrw = getDrawable(R.drawable.ic_newwave);
waveDrw.setColorFilter(getResources().getColor(middleColorID), PorterDuff.Mode.SRC_ATOP);
wave.setBackground(waveDrw);
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 120);
containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
containerParams.bottomMargin=90;
child.addView(wave, containerParams);
//create the bottom filler
RelativeLayout cardFiller = new RelativeLayout(this);
waveDrw = getDrawable(R.drawable.card_bottom);
waveDrw.setColorFilter(getResources().getColor(middleColorID), PorterDuff.Mode.SRC_ATOP);
cardFiller.setBackground(waveDrw);
RelativeLayout.LayoutParams cardFillerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 90);
cardFillerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.addView(cardFiller, cardFillerParams);
//set the name
TextView child_name= new TextView(this);
child_name.setText(cardTitle);
RelativeLayout.LayoutParams child_nameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
child_name.setGravity(Gravity.CENTER);
child_name.setTextSize(30);
child_name.setTextColor(getResources().getColor(R.color.dirtyWhite));
child_nameParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child_nameParams.bottomMargin=30;
child.addView(child_name, child_nameParams);
}
The strange thing is that when i try to run this code the two wave vector drawable that i use have the same color on all cards (they seems to get the last color passed to the method).
If i try to swap the resource, using some other xml shape file instead of the vector, the displayed colour is the right one. This made me thinking that the problem are the two vector but i can't understand what am i missing. They actually successfully set the proper color filter one time, but only the last one. It seem that the old filter is overwritten.
Any ideas?
Thanks
Here are some screenshots:
Drawables pointing to a same resource share their state. This means that for example if you have 2 Views which instantiate drawables using the same resource, then whatever changes you apply to one of the View's drawables will also be seen in the other View.
What you need to do is to call the method drawable.mutate() right after getDrawable.
This will ensure that whatever changes are applied to one of the drawable instances are unique to that specific instance, without affecting other drawables in the other views pointing to the same resource.
I want to add one imageView, TextView in a Relative Layout with background image through programmatically.I spedified height and width for the relative layout but it is not fitting to the specified width and height. where am going wrong ploesae help
Thanks in advance
here is my Code:
RelativeLayout.LayoutParams lp_topheader = new RelativeLayout.LayoutParams(800,45);
relative_topheader = new RelativeLayout(this);
relative_topheader.setLayoutParams(lp_topheader);
relative_topheader.setId(1);
Resources resources_topheader = getResources();
Drawable drawable_topheader = resources_topheader.getDrawable(R.drawable.headerbar_m);
relative_topheader.setBackgroundDrawable(drawable_topheader);
setContentView(relative_topheader);
RelativeLayout.LayoutParams lp_banner = new RelativeLayout.LayoutParams(385, 206);
relative_banner = new RelativeLayout(this);
relative_banner.setId(2);
relative_banner.setLayoutParams(lp_banner);
lp_banner.setMargins(40, 40, 0, 0);
lp_banner.addRule(RelativeLayout.BELOW,1);
ImageView iv = new ImageView(this);
iv.setScaleType(ScaleType.FIT_XY);
iv.setLayoutParams(lp_banner);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner_image);
iv.setImageBitmap(bitmap);
setContentView(iv, lp_banner);
The root view (that is, the view that you set as the content view) always fill the entire area of the window. If you want it to take only a certain part, add it to another Layout that will take the entire window.
Try this instead of the last line:
LinearLayout ll = new LinearLayout(this);
ll.addView(iv, lp_banner);
setContentView(ll);
I am a newbie in Android. I have a requirement where I have to add the imageViews dynamically onto a linear layout and then have to animate each image individually.
I am not aware of the way to add the imageViews dynamically onto a linear layout. Plz help me out.
Thanks
You can add view dynamically like this.
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
mainlayout.addView(imSex,imParams);
You can give this piece of code a try. It also has the bit for setting the dimensions for the ImageView along with a margin between multiple ImageView's. The int dimens = 45 and the int dimensMargin = 4; are pixel values and are being converted into dp.
The LinearLayout linlaLikes has to be in your layout XML and then cast in your activity.
LinearLayout linlaLikes = (LinearLayout) findViewById(R.id.linlaLikes);
ImageView imgUsers = new ImageView(getApplicationContext());
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 45;
float density = getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgUsers.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgUsers.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 4;
float densityMargin = getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
// SET YOUR IMAGER SOURCE TO YOUR NEW IMAGEVIEW HERE
// ADD THE NEW IMAGEVIEW WITH THE PROFILE PICTURE LOADED TO THE LINEARLAYOUT
linlaLikes.addView(imgUsers, imgvwMargin);
see this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.rainbow);
ImageView tv1 = new ImageView (this);
tv1.setImageresorce(R.drawable.image1);
ImageView tv2 = new ImageView (this);
tv2.setImageresorce(R.drawable.image2);
ImageView tv3 = new ImageView (this);
tv3.setGravity(Gravity.CENTER);
tv3.setImageresorce(R.drawable.image3);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
setContentView(ll);
}
see this usefull data
http://mobile.tutsplus.com/tutorials/android/android-layout/
You could use fragment for this http://developer.android.com/guide/components/fragments.html
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment imageFragment = new ExampleFragment();
fragmentTransaction.add(R.id.image_container, imageFragment);
fragmentTransaction.commit();
I have an app that displays some data graphically. It creates two Views to draw graphics in and adds them to my layout. Each view shows the data differently way but each View implements onSizeChanged() the same:
protected void onSizeChanged(int curw, int curh, int oldw, int oldh) {
if (bitmap2 != null) {
bitmap2.recycle();
}
canvas2= new Canvas();
bitmap2 = Bitmap.createBitmap(curw, curh, Bitmap.Config.ARGB_8888);
canvas2.setBitmap(bitmap2);
}
The views are invoked thusly:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.revlay);
GraphView1 graphView1 = new GraphView1(this, theEventArrayList);
myLayout.addView(graphView1);
GraphView2 graphView2 = new GraphView2(this, theEventArrayList);
myLayout.addView(graphView2);
Always the first onSizeChanged() that gets called gets a height of 652 and a width of 480; the second one gets a height of 0, which causes createBitmap() to fail. If I reversed the order of the above invocation then graphView1 would fail that way. I'd like each bitmap to have about half the area.
Thanks in advance for explaining what's going on!
its difficult to answer your question without knowing further implementation details about your graphviews and the layout parameters of the LinearLayout.
But assuming your LinearLayout has a horizontal orientation try this:
GraphView1 graphView1 = new GraphView1(this, theEventArrayList);
GraphView2 graphView2 = new GraphView2(this, theEventArrayList);
myLayout.addView(graphView2, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
myLayout.addView(graphView1, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
this will tell the LinearLayout to arrange your views so that they equally divide the horizontal space between themselves.
This is thebasic idea, but the image is ugly and pixelated. WHY???
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.iv);
Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.btn_default_normal);
NinePatchDrawable npd = new NinePatchDrawable(bmap, bmap.getNinePatchChunk(), new Rect(0,0,512,512), "name");
npd.mutate();
npd.setBounds(new Rect(0,0,512,512));
npd.invalidateSelf();
Bitmap bp = Bitmap.createBitmap(512,512, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bp);
npd.draw(canvas);
FileOutputStream ofo=null;
try {
ofo = openFileOutput("image", MODE_WORLD_READABLE);
} catch (IOException e) {
e.printStackTrace();
}
bp.compress(Bitmap.CompressFormat.PNG, 100, ofo);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_STREAM, getFileStreamPath("image").toURI());
intent.setType("image/png");
//startActivity(intent);
iv.setImageDrawable(new BitmapDrawable(bp));
}
}
You're making it infinitely more complicated than it needs to be. Just set your view in the XML like so:
<TextView
android:background="#android:drawable/btn_default_small"
android:layout_width="512px"
android:layout_height="wrap_content"
android:text="NinePatch View"
/>
Just using TextView as an example. The same will work for an ImageView, a View, pretty much any widget. The important thing to remember is to use the background attribute instead of the src attribute for an ImageView. The src attribute will stretch the image as is, it won't respect the NinePatch data.
If you're set on doing it in code, just use:
iv.setBackgroundResource(R.drawable.btn_default_small)
As a side note, what device are you displaying it on? Most mobile phones only have up to 480 pixels for the width.