How can I create a layout with imageviews overlapping? - java

I'm trying to add Imageviews dynamically and
I would like to create this kind of layout, with ImageViews overlapping, for
my android application.
I don't know how to set the overlapping in java code. I want to use java code becouse I'm adding Imageview dynamically!
this is the code that I did:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
and this is my java code:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mImageView = new ImageView (this);
mImageView.setId(1);
name = "0" + ".jpg";
Bitmap bitmap = BitmapFactory.decodeFile(url_image+name);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_START,1);
params1.addRule(RelativeLayout.ALIGN_TOP,1);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
for (int i = 1;i < num_max; i++){
mImageView = new ImageView (this);
mImageView.setId(i+1);
name = String.valueOf(i) + ".jpg";
int id = mImageView.getId() - 1 ;
bitmap = BitmapFactory.decodeFile(url_image+name);
mImageView.setImageBitmap(bitmap);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params2.addRule(RelativeLayout.BELOW,id);
params2.addRule(RelativeLayout.ALIGN_START,id);
params2.addRule(RelativeLayout.ALIGN_LEFT,id);
layout.addView(mImageView,params2);
}

If you want to images overlapping each other(as i understood from your question) you should remove this line :
params2.addRule(RelativeLayout.BELOW,id);
If you wanted something else feel free to post comment below this answer

Use relative layout for it
use this code
<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" >
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/iv"
/>
</RelativeLayout>

Following is the example of how to achieve it.
MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
//Add first image view
ImageView mImageView = new ImageView (this);
mImageView.setId(1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.color1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(300, 300);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
//Add second image view
ImageView mImageView2 = new ImageView (this);
mImageView2.setId(2);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.color2);
mImageView2.setImageBitmap(bitmap2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(300,300);
params2.setMargins(150, 150, 0, 0);
layout.addView(mImageView2,params2);
}
}
activity_main.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"
tools:context="com.example.sample.MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
</RelativeLayout>
Output Screenshot:
Required Images:

Related

TextView and ImageView stuck out of LinearLayout

I'm building an android app with a TextView, a ScrollView and a LinearLayout, I want to add one TextView and an ImageView to this LinearLayout using Java but they both stay outside of the Layout.
This is my activity XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="#drawable/galaxy"
tools:context=".Planet">
<TextView
android:id="#+id/planets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/purple"
android:text="#string/planets"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
<ScrollView
android:id="#+id/scroll"
android:layout_width="409dp"
android:layout_height="646dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And this is the code I'm using to generate and add the TextView and the ImageView:
public class MainActivity extends AppCompatActivity {
private ArrayList<Planet> planets = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Planet mercury = Planet.getPlanetFromResources(0, getResources());
planets.add(mercury);
addPlanetToUI(mercury);
}
public void addPlanetToUI(final Planet planet){
int color = getResources().getColor(R.color.yellow);
LinearLayout linear = findViewById(R.id.linear);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setWeightSum(20f);
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp1 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp2 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout horizontal = new LinearLayout(this);
horizontal.setWeightSum(6f);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
lp.weight = 10f;
horizontal.setLayoutParams(lp);
ImageView iv = new ImageView(this);
lp1.weight = .75f;
iv.setImageDrawable(getDrawable(planet.getImgSmallResourceValues()));
iv.setLayoutParams(lp1);
TextView tv = new TextView(this);
lp2.weight= .5f;
tv.setText(planet.getName());
tv.setBackgroundColor(color);
tv.setTextSize(40);
tv.setLayoutParams(lp2);
horizontal.addView(iv);
horizontal.addView(tv);
linear.addView(horizontal);
}
}
This is the outcome:
So is not that your elements are outside of the layout. The problem is that your ScrollView has a top constraint to the top of parent.
Change your ScrollView top constraint (app:layout_constraintTop_toTopOf="parent") to:
app:layout_constraintTop_toBottomOf="#id/planets"
Since your layout container is a ConstraintLayout elements can overlap between them.

How to put programmatically created Horizontally Scrolling view with XML created Layout in Android?

In my app a user can select multiple images from gallery then he can view it within the app.
I have three buttons within a Relative Layout
<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="com.codenemesis.multipleimg.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="imageSelector"
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="up"
android:text="Upload"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="viewclick"
android:text="View Images" />
To show the images I programmatically created ImageView array within HorizontalScrollView set to WRAP_CONTENT but the problem is when I click on view button HorizontalScrollView span over the whole screen like this.
I want to show HorizontalScrollView below view button. How can I do it?
Here is how I created HorizontalScrollView
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(prams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalScrollView.addView(linearLayout);
// mArrayUri is the arraylist of images selected from gallery
int b = mArrayUri.size();
int c = 0;
ImageView[] imageViews = new ImageView[mArrayUri.size()];
while (c < mArrayUri.size()) {
imageViews[c] = new ImageView(this);
imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imageViews[c].setPadding(10,0,10,0);
imageViews[c].setImageURI(mArrayUri.get(c));
linearLayout.addView(imageViews[c]);
c++;
// Set context view
setContentView(horizontalScrollView);
The code in your xml file should be like this
<?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="com.test.myapplication.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select images"
android:onClick="imageSelector"/>
<Button
android:id="#+id/button2"
android:layout_toRightOf="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="up"
android:text="Upload"/>
<Button
android:id="#+id/button3"
android:layout_toRightOf="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="viewclick"
android:text="View Images" />
<RelativeLayout
android:id="#+id/parent_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_marginTop="10dp"
>
</RelativeLayout>
The code in you java file should be like this
public class MainActivity extends AppCompatActivity {
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout=findViewById(R.id.parent_panel);
}
public void imageSelector(View view)
{
}
public void up(View view)
{
}
public void viewclick(View view)
{
HorizontalScrollView horizontalScrollView =
new HorizontalScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(prams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalScrollView.addView(linearLayout);
// mArrayUri is the arraylist of images selected from gallery
int b = mArrayUri.size();
int c = 0;
ImageView[] imageViews = new ImageView[mArrayUri.size()];
while (c < mArrayUri.size()) {
imageViews[c] = new ImageView(this);
imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imageViews[c].setPadding(10, 0, 10, 0);
imageViews[c].setImageURI(mArrayUri.get(c));
linearLayout.addView(imageViews[c]);
c++;
}
relativeLayout.addView(horizontalScrollView);
}
}
This should do the trick for you.

How to create a scroll list of custom relative layouts not stacking upon each other in Android Studio?

I want to create a scroll view list which would show "boxes" or more accurately custom made layouts (I'm using a custom class that extends a RelativeLayout - basically shows different pieces of information, some are static like places' working hours and some change dynamically and will be pulled from a server).
Though I encountered a problem - I (for the sake of seeing if my solution works) created 5 boxes and added them to the scroll view list but it seems like they are stacked upon each other. What's the proper way to make them appear one under another without manually tweaking their position coordinates? I was using addView() for that purpose but it doesn't work as intended for me or I use it poorly. If you know the answer, please briefly describe how this should be done.
Thanks in advance!
EDIT, here goes the code:
public class RestaurantBox extends RelativeLayout {
RestaurantBox (Context context){
super(context);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName = new TextView(context);
restaurantName.setText("Test Restaurant");
RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(restaurantName, restNameParams);
restaurantName.setTypeface(null, Typeface.BOLD);
TextView freeSpots = new TextView(context);
freeSpots.setText("15/20");
RelativeLayout.LayoutParams freeSpotParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
this.addView(freeSpots, freeSpotParams);
TextView book = new TextView(this.getContext());
RelativeLayout.LayoutParams bookParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
book.setText("Book");
bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
this.addView(book, bookParams);
}
}
public class BrowseRestaurants extends AppCompatActivity {
int restaurantCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_restaurants);
Intent intent = getIntent();
String login = intent.getStringExtra("LOGIN");
TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
text.setText("Welcome, " + login);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox = new RestaurantBox(this);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);
for(int i=1;i<5;i++){
final View view = relativeLayout.getChildAt(i-1);
RestaurantBox restaurantBox = new RestaurantBox(this);
restaurantBox.setTop(view.getBottom() + 50);
restaurantBox.setBottom(restaurantBox.getTop() + 300);
relativeLayout.addView(restaurantBox);
}
}
}
<!-- activity_browse_restaurants.xml-->
<?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"
android:fillViewport="true"
android:orientation="vertical"
tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/txtLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Logout"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/holo_blue_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp" />
</RelativeLayout
>
you are adding view in RelativeLayout so view stacking on each other so change it to LinearLayout in activity_browse_restaurants.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
now make changes according to in BrowseRestaurants.class
replace
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
with
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);
else will be fine if you want to change variable name its up to u, let me know if any problem

ScrollView Android with images

I want to put three images on a vertical scroll view, I don't know why, but I just see the third one ( tuto3 ).
here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:id="#+id/linearLayout42"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
</LinearLayout>
And there the class:
public class about extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutlayout);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout42);
ImageView image = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto1);
ll.addView(image);
ImageView image2 = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto2);
ll.addView(image2);
ImageView image3 = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto3);
ll.addView(image3);
}
}
You keep calling setBackgroundResource on image instead of image, image1, and image2.

How to reference the image from the xml?

This is the oncreate from my application.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
//ImageView img = (ImageView)controlInflater.
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
control.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"
android:gravity="center"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
</LinearLayout>
how to reference the ImageView in control.xml from java code.
View viewControl = controlInflater.inflate(R.layout.control, null);
ImageView img =(ImageView) viewControl.findViewById(R.id.img);
use this to get reference to imageView.
There is a findViewById method in View as well:
ImageView img = (ImageView)viewControl.findViewById(R.id.img)

Categories