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.
Related
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
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.
I'm adding TextViews dynamically with code. Now I'm using a LinearLayout with horizontal orientation which i thought would add a new line when the textviews doesnt fit into the row. And how wrong I was.
Thing is I don't know how many rows there will be, since it depends on user input. Therefor a GridView doesn't feels right, since I can't specify the number of columns. And I don't know every size of every TextView, which might make it look bad if a TextView is very long and I want 3 TextViews per row.
I'm sure there is a simple solution to this, I just wanna know the best one. Which layout should I use so when I add TextViews to it, so it makes a new row below the first one when it reaches the screen width?
Thanks!
See this:
package us.simpleit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SimpleGUI extends Activity {
TextView tv;
EditText et;
LinearLayout ll;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//LinearLayout ll = new LinearLayout(this);
ll = new LinearLayout(this);
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
// ARGB: Opaque Red
ll.setBackgroundColor(0x88ff0000);
tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
tv.setText("sample text goes here");
// ARGB: Opaque Green
tv.setBackgroundColor(0x5500ff00);
ll.addView(tv);
et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
et.setText("edit me please");
// ARGB: Solid Blue
et.setBackgroundColor(0xff0000ff);
ll.addView(et);
Button btn = new Button(this);
btn.setText("Go!");
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
tv.setText(et.getText().toString());
}
});
ll.addView(btn);
setContentView(ll);
//setContentView(R.layout.main);
}
}
Here i have taken One TextView and One EditText. Instead of that you can take two TextView or any Number of view you want.
Enjoy. :)
Thing that you could do is to make your main LinearLayout with vertical orientation and many LinearLayout children with horizontal orientation
When you want to add TextView, get the last child of the main layout to know if this child can contain you new TextView. If not, then add a new LinearLayout with horizontal orientation
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 [...].
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);