Horizontal Scroll View(Image View) - android

I have a Horizonal scroll view with Images. Then the output is this.
I loaded 2 same pictures but the Gap is too much.
I just want it to make like 5Margins on left but didn't get the result.
Here's my Code.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none"
>
<LinearLayout
android:id="#+id/imgLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
And here is my code of Adding imageviews to linear layout.
public void setImageViews() {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
parms.setMargins(5,5,5,5);
for(int i=0;i<6;i++){
ImageView imageView =new ImageView(this);
imageView.setLayoutParams(parms);
imageView.setImageResource(R.drawable.bsu);
linearLayout.addView(imageView);
}
}
The results i got.
I want it to be 5margins apart.

Related

View going out of screen in dynamic image views inside horizontal linear layout

I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen.
See what is happening:
I am using layout_gravity to achieve this
Without layout_gravity it appears like this
But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. You can see the first uploaded image for understanding the scenario. In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely.
Here's the xml structure
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="#+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java manipulation
#Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
Isn't that what you are trying to achieve by using HorizontalScrollView? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView.
Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that.
This is what worked out for me
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="#+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You can remove the parent LinearLayout for imageViewLayout too if you don't need it. Let me know if you need anymore help!

How to make Android LinearLayout to clip its children content?

I have a LinearLayout (horizontal) with several child TextViews inside. I have set fixed layout_width for both the LinearLayout and all the TextViews. When the total width of the children exceeds the width of the layout, it automatically shrink the children, making them narrower, which is not what I want. I want the layout to clip the children content and keep their width.
How can I achieve that?
For example: Let's say my LinearLayout is 200dp wide, and there are 4 TextView each 60dp wide. I want it to show the first three TextViews, and 1/3 of the 4th (60 + 60 + 60 + 20 = 200)
Thanks.
Use android:weightSum in LinearLayout
And use android:layout_width="0dp" and android:layout_weight="1" in TextView
Try this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello"/>
</LinearLayout>
Also
You can change android:layout_width="match_parent" to android:layout_width="wrap_content" in TextView .
Edit
And you can use in your java code .
You can change the width or weight by yourself .
private LinearLayout mLinearLayout;
private int viewsCount = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
for (int i = 0; i < viewsCount; i++) {
TextView textView = new TextView(this);
if(i == viewsCount-1){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
} else {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3);
}
mLinearLayout.addView(textView);
}
}

Adding Image views to the horizontal scroll dynamically (through code)

I just started working on android a day ago and I'm working on scrolls. I have already made one but I would now like to do the same thing dynamically.
This is the code for my activity_main.xml
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:id= "#+id/linearlayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onTouch">
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="400px"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="46dp">>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drop"
android:textSize="30sp"
android:text="Drop Zone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Total"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Success"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Fail"
android:textSize="20sp" />
</LinearLayout>
Basically I would like to add 10 images from the drawable into the horizontal scroll as image views dynamically. Any help or ideas are much appreciated.
You can just do like this.
Create an xml layout which holds the imageView.
image_item.xml
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
android:adjustViewBounds="true"
/>
Now find the container where want into java file, like this.
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.linearlayout1);
Now just run a for loop till 10 and add the views at runtime.
for(int a = 0 ; a < 10 ; a++)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.image_item, null);
containerLayout.addView(inflatedView);
}
Hope this helps, Feel free to discuss if found problem.
Happy Coding :-)
I solved the problem. I'm gonna post the solution hoping that it helps someone else having a similar issue. I originally made 3 horizontal scroll views and the xml for one of them is like this
<HorizontalScrollView
android:id="#+id/HorizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none">
<LinearLayout
android:id="#+id/imgLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
The code I wrote to create Image Views inside Linear Layout of horizontal scroll view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int j=1; j<=10; j++)
{
b1=j;
create_img1("drawable/a"+j, b1);
}
}
void create_img1(String ss, int ID)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200, 200);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 20, 20, 20);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
linearLayout.addView(imageView);
imageView.setId(ID);
}
I made one for multiple scroll views with drag and drop functionality but I filtered it out and if you want to create image views inside scroll views dynamically, this is what you are looking for. Hope this helps someone else with a similar issue.
I think you are trying to do something like this:
mHScrollContentView = (ViewGroup) findViewbyId(R.id.linearlayout1);
ImageView iv1 = new ImageView(this);
iv.setImageResource(R.drawable.image_1);
ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.image_2);
ImageView iv3 = new ImageView(this);
iv.setImageResource(R.drawable.image_3);
ImageView iv4 = new ImageView(this);
iv.setImageResource(R.drawable.image_4);
mHScrollContentView.addView(iv1);
mHScrollContentView.addView(iv2);
mHScrollContentView.addView(iv3);
mHScrollContentView.addView(iv4);
http://sandyandroidtutorials.blogspot.in/2013/06/horizontal-listview-tutorial.html
You might get an answer here!

Android: create TextView at the bottom of a LinearLayout dynamically

I created an app where clicking a button I create a TextView inside a LinearLayout(hosted inside a ScrollView).
When I click the button the TextViews are displayed starting from the top like this example:
First Image
http://imgur.com/8fhtYZs (Sorry I don't have reputation to display it in the post so I inserted the link)
But my task is to create the textviews at the bottom of my LinearLayout and make the TextViews already created scroll up the new one. (Sorry for my bad english) I show an example to be more comprehensible.
Second Image
http://imgur.com/RLHooaH
This is my code:
public class Messaggi2 extends ActionBarActivity implements OnClickListener {
LinearLayout mLayout;
ScrollView scroll;
Button invia;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaggi2);
mLayout = (LinearLayout) findViewById(R.id.LinearScrollLayout);
scroll = (ScrollView) findViewById(R.id.scrollView2);
invia = (Button) findViewById(R.id.Invia);
invia.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
mLayout.addView(createNewTextView("Message"));
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}
}
And this is my Layout.XML
<?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/scrollView2"
android:layout_width="fill_parent"
android:layout_height="640dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Messaggio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/balloon_broadcast_incoming_pressed"
android:text="#string/messaggi" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:background="#drawable/linear_layout_bg" >
<EditText
android:id="#+id/Scrivi"
android:layout_width="440dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/scriviMessaggio" />
<Button
android:id="#+id/Invia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/send_btn" />
</LinearLayout>
</LinearLayout>
There is no such thing as LinearLayout.BOTTOM, nor can we add rule for LinearLayout.
But the good news is to insert into LinearLayout, one could decide the sequence.. Just use the addView function with 3 arguments as below
mLayout.addView(view, index, param);
The index determined the order.
You need to set your linear layout's height to the height of your ScrollView so instead of using wrap_content cause that is wrapping your linearlayout change it to match_parent
example:
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
When you want to add the button at the bottom of the linearlayout just add a rule to your layout params.
example:
private TextView createNewTextView(String text) {
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}

Adding items to LinearLayout (and resizing)

I have the following layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#f00"
android:id="#+id/main_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0f0"
android:id="#+id/bed_list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f"
android:text="Howdy cowboy?"
android:textSize="50dip"
android:textColor="#fff"
android:id="#+id/textview"/>
</LinearLayout>
I want to add some textviews to R.id.bed_list (as if they were vertical tabs)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout bedList = (LinearLayout) findViewById(R.id.bed_list);
for (int i = 0; i < 12; i++) {
System.out.println("In the loop");
TextView textView = new TextView(this);
textView.setText("HJM-" + i);
textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
bedList.addView(textView,i,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
So far, the code is run, the System.out.println messages in the loop appear at the console, but I only see the textview (R.id.textview). I wanted the list to appear at the left of the screen with the options calculated, but it does not work.
Further on, if I define a first textview in the label (so that there is some items at the very beginning and a space is reserved), I get to see that textview but the others (added from the program) do not appear either.
Your textview (R.id.textview) has a width of match_parent, effectively pushing your other views (the linearlayout) off screen. Instead you can give it a width of wrap_content and possibly a layout_weight of 1.

Categories

Resources