I am trying to programatically create a multiple RelativeLayouts inside a ViewFlipper.
I'm doing this in order to build a dynamic ViewFlipper that holds N number of images, N changes.
I am trying to create something like that: (that works BTW..)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/lightning" />
<TextView
style="#style/ImageTitle"
android:text="#string/lightning" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/color_baloons" />
<TextView
style="#style/ImageTitle"
android:text="#string/color_baloons" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/natural_wall" />
<TextView
style="#style/ImageTitle"
android:text="#string/natural_wall" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:id="#+id/swipe_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/swipe_left" />
<ImageView
android:id="#+id/swipe_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/swipe_right" />
</RelativeLayout>
This is my code:
Problem: I don't see the images
What am I missing?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_image_layout);
_context = this;
_viewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
Intent intent = getIntent();
_imageUrls = intent.getStringArrayExtra("theImageUrl");
for (int i = 0; i < _imageUrls.length; i++) {
RelativeLayout layout = new RelativeLayout(_context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.setLayoutParams(layoutParams);
ImageView imageView = new ImageView(_context);
LayoutParams imageParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
imageView.setLayoutParams(imageParams);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.CENTER_CROP);
File file = new File(_imageUrls[i]);
Uri uri = Uri.fromFile(file);
final String imagePathFromUri = uri.getPath();
final Bitmap bitmap = BitmapHelper.loadImageFromUrl(
"file://" + _imageUrls[i],
imagePathFromUri,
BUFFER_IO_SIZE);
if (bitmap != null) {
final Drawable drawable = new BitmapDrawable(
getResources(),
bitmap);
if (drawable != null) {
imageView.setBackgroundDrawable(drawable);
}
}
TextView textView = new TextView(_context);
textView.setTextAppearance(_context, R.style.ImageTitle);
layout.addView(imageView);
layout.addView(textView);
_viewFlipper.addView(layout);
}
_viewFlipper.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Related
I have an ImageView and a TextView and I want to change the color of the background from the bottom to half the height of my ImageView. I tried to do that with a View like this:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="#color/cyan"
android:id="#+id/blue_background" />
<TextView
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:textColor="#color/white"
android:text="#string/credits"/>
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#id/credits"
android:layout_below="#id/resume_button"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo"/>
</RelativeLayout>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView logo = (ImageView) findViewById(R.id.logo);
TextView credits = (TextView) findViewById(R.id.credits);
View background_blue = (View) findViewById(R.id.blue_background);
background_blue.getLayoutParams().height = credits.getLayoutParams().height + (logo.getLayoutParams().height / 2);
}
but it doesn't work, I get the entire background as blue instead of just the part I am interested in.
Use ViewTreeObserver
final ImageView logo = (ImageView) findViewById(R.id.logo);
ViewTreeObserver vto = logo.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
logo.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = logo.getMeasuredWidth();
int height = logo.getMeasuredHeight();
background_blue.getLayoutParams().height = credits.getLayoutParams().height + (height / 2);
}
});
Try this workaround
Replace this
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="#color/cyan"
android:id="#+id/blue_background" />
With this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:id="#+id/blue_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/cyan" />
</LinearLayout>
and remove this line from Main Activity
background_blue.getLayoutParams().height = credits.getLayoutParams().height + (logo.getLayoutParams().height / 2);
I have a gridview being populated from db. When I open some images they get stuck at the top of the layout. What could be causing that?
I noticed that some images open directly in the middle of the layout that is what I really want. And some images just "fall" in the right position after I double tap to zoom them.
Gridview activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
TextView id = (TextView) findViewById(R.id.i_id);
Intent i = getIntent();
i_id = i.getStringExtra(TAG_ID);
id.setText(i_id);
mGridView = (GridView) findViewById(R.id.gridView);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
//Initialize with empty data
mGridData = new ArrayList<>();
mGridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, mGridData);
mGridView.setAdapter(mGridAdapter);
//Grid view click event
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
GridItem item = (GridItem) parent.getItemAtPosition(position);
Intent intent = new Intent(GridViewActivity.this, DetailsActivity.class);
intent.putExtra("image", item.getImage());
startActivity(intent);
}
});
//Start download
new AsyncHttpTask().execute(FEED_URL);
mProgressBar.setVisibility(View.VISIBLE);
}
Details activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent i = getIntent();
String image = i.getStringExtra("image");
//Set image url
imageView = (ImageView) findViewById(R.id.grid_item_image);
Picasso.with(DetailsActivity.this).load(image).into(imageView);
mAttacher = new PhotoViewAttacher(imageView);
Uri bmpUri = getLocalBitmapUri(imageView);
shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
}
Gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<TextView
android:id="#+id/i_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IDDD"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ilustrativas"
android:id="#+id/ilustrativas"
android:layout_above="#+id/likes_count"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:textSize="12sp"
android:textStyle="italic" />
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_below="#+id/ilustrativas"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
Details.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_background"
android:background="#000">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
</RelativeLayout>
GridItem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#f1f1f1"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="100dp"
android:scaleType="fitCenter"
android:layout_height="100dp" />
</LinearLayout>
I found the error by myself. I donĀ“t know why but PhotoviewAttacher was causing the problem. Just removed it and used this class https://github.com/MikeOrtiz/TouchImageView
I have made an application in which I will be dynamically adding image and text to the application for this i am using
1.relative layout (main layout)
1.(a) linear layout ( a linear layout containing all the 1.(b) values)
1.(b) linear layout ( d layout which contains a image and a text) , each (1.(b)) layout contain one image and one text
now as I am running this app on set top box (with remote) with android, which is connected to smart tv,,,,, so i have to control my app with a remote control provided with the set top box.
so what I want is when the application loads , the focus will be on the first ((1.(b) linear layout)) i.e. on the first pair of image and text.
already done:- I have used requestFocus() but of no use
any help will be greatly appreciated thanks in advance cheers !!!!!!!
the code snippet is as follows:-
activity
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
75, 98);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, 25, 0, 0);
RelativeLayout.LayoutParams paramrelative = new RelativeLayout.LayoutParams(
170, 158);
paramrelative.setMargins(2, 0, 0, 0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(180,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(2,0, 0, 0);
//Create four
for(int j= 0;j<=count-1;j++)
{
// Create LinearLayout
ScrollView sv = new ScrollView(this);
final RelativeLayout ll = new RelativeLayout(this);
ll.setBackgroundResource(R.drawable.app_bg);
ll.setLayoutParams(paramrelative);
ll.setFocusableInTouchMode(true);
ll.setClickable(true);
// final String url= separated[j+1];
// Create Button
final Button img = new Button(this);
final TextView tv = new TextView(this);
// Give button an ID
img.setId(j+1);
int s=img.getId();
p.addRule(RelativeLayout.BELOW, s);
tv.setLayoutParams(p);
tv.setGravity(Gravity.CENTER);
//File sdCardRoot = Environment.getExternalStorageDirectory();
// File sdcardPath = new File(Environment.getExternalStorageDirectory()
// .getPath() + "/CATEGORIES");
String filename=finalname[j];
File myDir = new File(sdcardPath,filename);
// File myDir = new File(sdcardPath, "CATEGORIES_hotelinfo.png");
String m1 = myDir.getPath();
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(m1);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
img.setBackgroundDrawable(Drawable.createFromPath(m1.toString()));
//2ndaugust2014 tv.setText(finalname[j]);
int s1= finalname[j].indexOf(".");
final String finalname1=finalname[j].substring(0,s1);
tv.setText(finalname1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String newPath=newPath1+finalname1;
File sdcardPath = new File(Environment.getExternalStorageDirectory()
.getPath() +newPath);
new CountDownTimer(2000,1000) {
public void onTick(long millisUntilFinished) {
ll.setBackgroundResource(R.drawable.hover_btn_d); //This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
ll.setBackgroundResource(R.drawable.app_bg);
}
}.start();
// if the directory does not exist, create it
if (!sdcardPath.exists())
{
Toast.makeText(getApplicationContext(),
"redirecting to www."+finalname1+".com",
Toast.LENGTH_SHORT).show();
}
else
{ Intent intent = new Intent(Second60grid.this,Second60grid.class);
// startActivity(intent);
// i.setClassName(packageName, activitylast);
Toast.makeText(getApplicationContext(),
"name" + finalname1,
Toast.LENGTH_SHORT).show();
intent.putExtra("nameoffile",newPath+"/");
startActivity(intent);
}
}
});
// set the layoutParams on the button
img.setLayoutParams(params);
//Add button to LinearLayout
// setContentView(sv);
ll.addView(img);
ll.addView(tv);
if(j==0)
{
ll.requestFocus();
}
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
and the xml file is as follows:-----
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dip"
android:id="#+id/rl1"
>
<TextView
android:id="#+id/TEXT0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="CATEGORIES" />
<LinearLayout
android:id="#+id/linearMain1"
android:layout_below="#+id/TEXT0"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:id="#+id/linearMain2"
android:layout_below="#+id/linearMain1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain2"
android:orientation="horizontal"
android:id="#+id/linearMain3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain3"
android:orientation="horizontal"
android:id="#+id/linearMain4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
> </LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain4"
android:orientation="horizontal"
android:id="#+id/linearMain5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="1400dp"
android:layout_height="70dp"
android:background="#80ffffff"
android:layout_alignParentBottom="true"
android:layout_below="#+id/rl1"
android:id="#+id/rl2"
>
<ImageView
android:id="#+id/remote_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/TEXT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/remote_btn2"
android:layout_toRightOf="#+id/remote_btn"
android:text="MOVE" />
<ImageView
android:id="#+id/remote_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/TEXT1"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn2" />
<TextView
android:id="#+id/TEXT2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TEXT1"
android:layout_toRightOf="#+id/remote_btn2"
android:text="SELECT" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="wrap_content"
android:layout_height="800dip"
android:background="#10ffffff"
android:layout_marginLeft="10dip"
android:layout_marginBottom = "10dp"
android:layout_above="#+id/rl2"
>
<Button
android:id="#+id/buttoniptv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="iptv"
android:background="#FF0000"/>
<Button
android:id="#+id/buttontvchannels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttoniptv"
android:background="#FF0000"
android:text="tvchannels" />
<Button
android:id="#+id/buttoninternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttontvchannels"
android:background="#FF0000"
android:text="internet" />
</RelativeLayout>
</RelativeLayout>
You are currently adding view in ascending order
parentView.addView(child, 0);
Add it in Descending Order:
parentView.addView(child, parentView.getChildCount() - 1);
I want to develop an app like this. Which is exactly like android gallery.
So I tried with HorizontalScrollView. But still I am facing some problems
Here is my layout.
<ImageView
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/hScrollView"/>
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
and here is my code
LinearLayout image;
ImageView main;
ImageView[] iv=new ImageView[images.length];
int i;
and in Activity,
for(i=0;i<images.length;i++)
{
iv[i]=new ImageView(this);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
iv[i].setLayoutParams(lparams);
iv[i].setImageResource(images[i]);
iv[i].setTag(images[i]);
image.addView(iv[i]);
iv[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
main.setImageResource((Integer) iv[i].getTag());
}
});
But it is always giving me ArrayIndexOutOfBoundException. I know Value of i is getting updated for each iteration.
So What am I supposed to do? Any alternative ways are appreciable.
I am using api level 18 so Gallery won't work.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/hScrollView"
android:background="#drawable/ic_launcher" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
In Activity class:
int[] drawable = { R.drawable.background, R.drawable.forground,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
centerImageView = (ImageView) findViewById(R.id.imageView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < drawable.length; i++) {
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView view = new ImageView(this);
view.setLayoutParams(lparams);
view.setId(i);
view.setImageResource(drawable[i]);
linearLayout.addView(view);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
centerImageView.setImageResource(drawable[v.getId()]);
}
});
}
}
Hope will help you:)
you can use like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Try this..
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Gallery
android:id="#+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
Gallery gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
Reference :
http://saigeethamn.blogspot.in/2010/05/gallery-view-android-developer-tutorial.html
http://learnandroideasily.blogspot.in/2013/07/android-gallery-view-example.html
http://www.androidpeople.com/android-gallery-imageview-example
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
Please note: android.widget.Gallery is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.
As per your requirement the HorizontalScrollView is more suitable. Check out the below tutorial which will help you in implementation.
Implement HorizontalScrollView like GalleryView
I wish to create a custom layout out for my sample application. Previously i used the xml layout for User interface, but now i wish to create the application using custom (without using xml layout). I used the below code in my xml, but i change this xml layout to custom code i can't implement (i don't have knowledge how to do this). How can i write this layout code in my Activity?. Any one could you please help me to solve this. Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/webviewscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/webviewlinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/webviewframe1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview1"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" >
</WebView>
<ImageView
android:id="#+id/webviewimage1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/one" >
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/webviewframe2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview2"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" >
</WebView>
<ImageView
android:id="#+id/webviewimage2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/two" >
</ImageView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
As you want to create layout at run-time, you can do this way:
RelativeLayout layout1 = new RelativeLayout(this);
ImageView imgView1 = new imgView1(this);
layout1.addView(imgView1); // added ImageView into the RelativeLayout
do this kind of process for creating and adding widgets.
LinearLayout ll1,ll2;
ScrollView sv = new ScrollView(this);
RelativeLayout rl1,rl2;
WebView wv1,wv2;
ImageView iv1,iv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int LHeightWrap = LinearLayout.LayoutParams.WRAP_CONTENT;
int LWidthWrap = LinearLayout.LayoutParams.WRAP_CONTENT;
int LHeightFill = LinearLayout.LayoutParams.FILL_PARENT;
int LWidthFill = LinearLayout.LayoutParams.FILL_PARENT;
int RHeightWrap = RelativeLayout.LayoutParams.WRAP_CONTENT;
int RWidthWrap = RelativeLayout.LayoutParams.WRAP_CONTENT;
int RHeightFill = RelativeLayout.LayoutParams.FILL_PARENT;
int RWidthFill = RelativeLayout.LayoutParams.FILL_PARENT;
wv1 = new WebView(this);
iv1 = new ImageView(this);
rl1.addView(wv1,RWidthWrap,RHeightWrap);
rl1.addView(iv1,RWidthWrap,RHeightWrap);
wv2 = new WebView(this);
iv2 = new ImageView(this);
rl2.addView(wv2,RWidthWrap,RHeightWrap);
rl2.addView(iv2,RWidthWrap,RHeightWrap);
ll2.addView(rl1);
ll2.addView(rl2);
sv.addView(ll2);
ll1.addView(ll2);
You can do somthing like this. You can also set properties of any component using native code.