place a chart in a view - android

I have a chart that I use to display a graph.
To display the chart I use a jar taken from here:
http://writerbay.wordpress.com/2011/03/12/android-tutorial-displaying-chart-on-android/#comment-54
And the code for it is also similar to that one....and it looks like this:
package com.google.android.chartView;
import com.kidroid.kichart.model.Aitem;
import com.kidroid.kichart.view.LineView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class chartView extends Activity {
/** Called when the activity is first created. */
LineView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String xaxis[]=new String[4];
xaxis[0]="2006";
xaxis[1]="2007";
xaxis[2]="2008";
xaxis[3]="2009";
float line1[]=new float[4];
line1[0]=120;
line1[1]=240;
line1[2]=500;
line1[3]=100;
float line2[]=new float[4];
line2[0]=100;
line2[1]=650;
line2[2]=700;
line2[3]=300;
float line3[]=new float[4];
line3[0]=50;
line3[1]=180;
line3[2]=360;
line3[3]=900;
Aitem items[]=new Aitem[3];
items[0]= new Aitem(Color.BLUE,"pauOut",line1);
items[1]= new Aitem(Color.GREEN,"pauOut",line2);
items[2]= new Aitem(Color.YELLOW,"pauOut",line3);
lv=new LineView(this);
lv.setTitle("Yearly Budget");
lv.setAxisValueX(xaxis);
lv.setItems(items);
setContentView(lv);
}
}
The problem I'm facing is that the chart fills entire my image and when I also wanna place a button on that activity it won't get visible because the chart occupies the full screen....
Question: Is someone who could tell me how to place that chart in order for it not to fill my entire screen so I could place button underneath it??
Thx...My app is on android

You're calling setContentView on just the layout object you've created. So thats all that will be shown!
Try using an xml file containing a blank linearlayout, and a button setup how you want it, and then in code, ThatLinearLayout.addView(lv);

Related

show image in "popup" with animation when clicking?

I want to click on an image in my app and it get bigger and get in the foreground and the other part of screen become darker. Is it possible to do that with an animation. I mean, like a popup in the android settings where i can tick different things, but only with my idea. So there is a picture instead settings. Or something similar. I can´t define it very good :D
Hope you understand me :D
Thanks in advance!
okay edit for better understanding:
I created an app with swipe view and tabs. Then i added some pictures with imageviews in one fragment. I know this feature from other apps: If i click on a picture, it zooms from its location in the center of the screen. You could compare it with halo from paranoid android.
I´ll for an example.
Code of Fragment1 with the picture:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (RelativeLayout)inflater.inflate(R.layout.fragment1, container, false);
}
}
i made example code to you as much as understand your request
you can use it like this
new Large_ImagePopup(<activity context>,< image url path >);
for example how to use it
new Large_ImagePopup(myclassname.class,"my image url link");
create new class Large_ImagePopup.class then create layout dialog_mylargeimage.xml add Imageview inside or anything you want
public class Large_ImagePopup extends Dialog {
Context context;
public Large_ImagePopup(Context context,String imageurl) {
super(context);
this.context = context;
//Set costume dialog information
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_mylargeimage);//<< your xml layout for custome image popup
//get current screen h/w
Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
int w = d.getWidth();
int h = d.getHeight();
//Set popup window h/w full screen or 98% it up to you
getWindow().setLayout((int)( w/100)*98, (int)( h/100)*98);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setCancelable(true);
//now get imageview inside custome dialog layout
ImageView large = (ImageView) findViewById(R.id.large);
//Show dialog
show();
//after you show dialog you can animate image zoom effect or anything you want to do with large imageview you can Google it how to animate imageview fade-in or zoom
}
}
hope this useful to you

Exporting Charts as images in Android

I'm stucked in this for almost a month. I have a perfect chart in my android code using 'afreechart', however, the library does not seem to have support for exporting the chart as an image yet (since it's based on 'jfreechart' and this one does the job).
I tried to get the view of the chart, convert it into canvas and save using the compress functions of the bitmap library, however everytime i try this i get a totally black image as a result. I tried this same method and it works for the other views of my code (simple views, like linearlayout and relativelayout).
After that i tried to create a routine to make a screenshot of the chart activity and close that activity after that. But I couldn't find a way to do that by code, the closest i got was with monkeyrunner.
So i gave up of this idea and tried to look for other libraries, such as 'kichart', 'achartengine', etc, but none of them seem to do the job for, i'm freaking out and i thought that exporting the chart to an image wouldn't be that hard... any ideas?
When i set a background color for the layout, the returned image is a full rectangle with the color of the background, so it's getting the layout, just not the chart.
My Code:
package com.kichart;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class Main extends Activity {
LinearLayout ll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
float[] values = new float[] { 2.0f,1.5f, 2.5f, 1.0f , 3.0f };
String[] verlabels = new String[] { "great", "ok", "bad" };
String[] horlabels = new String[] { "today", "tomorrow", "next week", "next month" };
GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.BAR);
ll = new LinearLayout(this);
ll.addView(graphView);
Draw2d d = new Draw2d(this);
setContentView(d);
//setContentView(graphView);
}
public class Draw2d extends View {
public Draw2d(Context context) {
super(context);
setDrawingCacheEnabled(true);
}
#Override
protected void onDraw(Canvas c) {
ll.setBackgroundColor(Color.WHITE);
ll.measure(MeasureSpec.getSize(ll.getWidth()), MeasureSpec.getSize(ll.getHeight()));
ll.layout(400, 400, 400, 400);
ll.draw(c);
try {
getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File("/mnt/sdcard/graph2.png")));
} catch (Exception e) {
Log.e("Error--------->", e.toString());
}
super.onDraw(c);
}
}
}
You can get a image bitmap from a view by doing:
Bitmap bitmap;
chart.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(chart.getDrawingCache());
chart.setDrawingCacheEnabled(false);
if you can't call getDrawingCache on the chart, try to place it inside a layout like RelativeLayout or something like that. and call it on the layout.
After that you can save it as a image..
If the image is still black, you need to make sure the chart is created before getting the bitmap.
Hope this will help you

Drawing shapes programmatically for Android

I'm new to the Android SDK so I'm trying to figure this out. I have read the documentation and a text book and they haven't been particularly helpful in this matter.
I'm just trying to draw a simple rectangle in a linear layout on the screen. I can't get the shape to show up, however, when I add text to this layout in the same fashion, the text does show up. What am I missing?
package jorge.jorge.jorge;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ShapesActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShapeDrawable rect = new ShapeDrawable(new RectShape());
rect.getPaint().setColor(Color.GREEN);
ImageView view1 = new ImageView(this);
view1.setImageDrawable(rect);
LinearLayout frame = (LinearLayout)findViewById(R.id.linear1);
frame.addView(view1);
// TextView tx = new TextView(this);
//
// tx.setText("Hello World");
//
// frame.addView(tx);
}
}
The Shape is usually used for making a background to some View. Its width and height is the same of the view that is using it. Then, if this view has no width and height, It'll have no width and height, too.
Basically, I think that your ImageView has no width and height, then it's invisible.
You can see how to set it programatically here:
Set ImageView width and height programmatically?
But, I recomend you to make the layout in XML's way.

ImageView without xml definition

I want to create a dynamic number of ImageViews in Android.
But to display these ImageViews I have to create them in the main.xml (Am I right?)
Is there a way to display the ImageViews without creating them in the main.xml?
You can create them dynamically like this:
ImageView image=new ImageView(this);
and to set image in this dynamically created view use:
image.setImageResource(R.drawable.yourimage);
Put your image my_image.jpg into res/drawable/my_image.jpg and use:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.my_image);
setContentView(imageView);
}
}
Tested on Android 22 with the default template generated by android create project [...].

Android layouts, how to make two items in a line

I am beginner in android. And i could not understand differences between layouts. i want to make a button and next to button i want to set an image. So which layout should i have yo use and how can i set the positions.(Programitacilly)
You need to visit this page:
Common Layout Objects
You will want to use the LinearLayout - inside the linear layout you can place your button and image.
There's a good tutorial on how to use LinearLayout over here:
Android Developers-LinearLayout
If you are new to Android, I would recommend checking out the other "Hello *" tutorials over there.
Cemal wanted to see this done programatically. The above references are good for showing the XML versions. Here is a quick example of the button and image in a linear layout done entirely programtically.
package com.example.android.ProgramLinearActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProgramLinearActivity extends Activity {
private static final int HORIZONTAL = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(HORIZONTAL); //HORIZONTAL is default but here for clarity
ImageView imageView = new ImageView(this);
imageView.setImageResource( R.drawable.icon);
Button button = new Button (this);
button.setText("Test");
linearLayout.addView(button);
linearLayout.addView(imageView);
setContentView(linearLayout);
}
}
Hit ctrl+space a lot in the eclipse editor to see tutorials on the other attributes for the button and imageview wigets.

Categories

Resources