Adding multiple images to horizontal scroll view in android - android

I am building an application in which I want to add multiple images in a horizontal scrollview and want to scroll these imageson click of two (left and right) srcolling buttons, but I am not able to do that as I am new to android development.So can anybody provide my with the solution how to do it ? Thanks in advance....

put this code in your xml
<HorizontalScrollView
android:id="#+id/xml_full_img_hor_below_view"
android:layout_width="match_parent"
android:layout_height="#dimen/full_img_below_relative_height"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/xml_full_img_linear_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
/////////////////////////////////////
java code
this model_ProductMaster.listProductImages.size() is images list:
private void setProductImageInView() {
LayoutInflater layoutInflater = (LayoutInflater) viewFullImage
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linear_below_img_view.removeAllViews();
// Set the image height,widt,scaling and margin of the imageview
for (int i = 0; i < model_ProductMaster.listProductImages.size(); i++) {
convertView = layoutInflater.inflate(
R.layout.xml_row_full_image_view, null, false);
final ImageView img = (ImageView) convertView
.findViewById(R.id.xml_full_img_below_middleimg);
img.setMinimumWidth((int) viewFullImage.getResources()
.getDimension(R.dimen.full_img_below_img_width_height));
img.setMaxWidth((int) viewFullImage.getResources().getDimension(
R.dimen.full_img_below_img_width_height));
img.setMinimumHeight((int) viewFullImage.getResources()
.getDimension(R.dimen.full_img_below_img_width_height));
img.setMaxHeight((int) viewFullImage.getResources().getDimension(
R.dimen.full_img_below_img_width_height));
img.setScaleType(ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 0, 0);
layoutParams.gravity = Gravity.CENTER;
convertView.setLayoutParams(layoutParams);
img.setScaleType(ScaleType.FIT_XY);
imageLoader.displayImage(
model_ProductMaster.listProductImages.get(i).ImageUrl, img,
options, animateFirstListener);
linear_below_img_view.addView(convertView);
convertView.setId(i);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < linear_below_img_view.getChildCount(); i++) {
View view = linear_below_img_view.getChildAt(i);
if (view == v) {
view.setBackgroundResource(R.drawable.xml_popup_border_with_bg);
} else {
view.setBackgroundResource(R.drawable.common_bg_with_dark_border);
}
}
loadImageonCenter(model_ProductMaster.listProductImages
.get(v.getId()).ImageUrl);
System.gc();
}
});
}
linear_below_img_view.getChildAt(0).setBackgroundResource(
R.drawable.xml_popup_border_with_bg);
// Image Starting load
System.gc();
}

main.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="100dp" >
<ImageView
android:id="#+id/xml_home_screen_img_leftarrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:padding="5dp"
android:src="#drawable/leftarrow" />
<HorizontalScrollView
android:id="#+id/xml_full_img_hor_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/xml_full_img_linear_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/xml_row_full_image_view" />
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/xml_home_screen_img_rightarrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:padding="5dp"
android:src="#drawable/rightarrow" />
</RelativeLayout>
xml_row_full_image_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xml_full_img_below_mainlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:padding="4dp" >
<ImageView
android:id="#+id/xml_full_img_below_middleimg"
android:layout_width="50dp"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_launcher" />
</LinearLayout>

Related

Reverse layouts inside RelativeLayout programmatically

I have one relativeLayout and three view's inside RelativeLayout. There views are place on each other.
<RelativeLayout
android:id="#+id/includelayoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transparentColorView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<include
layout="#layout/transfer_keyboard_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/finalRequestView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<include
layout="#layout/fragment_transfer_new_version_container_child"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
Is it a possible to reverse position my view's? I mean,in my code last Layout is finalRequestView and in button click i would to my last view in first position,and other view's place on my view,
How i can solve this problem?
I try to reverse layout by following demo & it works as you want, please take a look
ReverseLayout.java
public class ReverseLayout extends AppCompatActivity {
LinearLayout llRoot;
Button btReverseLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reverse_layout);
llRoot = (LinearLayout) findViewById(R.id.llRoot);
btReverseLayout = (Button) findViewById(R.id.btReverseLayout);
btReverseLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<View> alViews = new ArrayList<View>();
for (int i = llRoot.getChildCount() - 1; i >= 0; i--) {
View view = llRoot.getChildAt(i);
llRoot.removeViewAt(i);
alViews.add(view);
}
for (int j = 0; j < alViews.size(); j++) {
llRoot.addView(alViews.get(j));
}
}
});
}
}
reverse_layout.xml layout file
<?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_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tosc185.testproject.Activity6">
<LinearLayout
android:id="#+id/llRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btReverseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Reverse Layout" />
</RelativeLayout>

textview is not displayed in gallery item and also there is space at beginning and end of every item

I want to display imageview and textview as a verticle item in gallery but textview is not displayed and also before first item and last item of gallery it contains space, how can I remove this space?
This is my code.
FilterActivity.java
public class FilterActivity extends Activity {
ListView lstfilter;
MyAdapter adapter;
ImageView next,prev;
Gallery horizontalscrollview;
public static ArrayList<String> mylist;
public static ArrayList<String> mycategorylist;
HorizontalScrollView horizontalScroll;
HorizontalScrollAdapter horizonAdapter;
LinearLayout hlayout;
ArrayList<Integer> array_image = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
mylist = new ArrayList<String>();
mycategorylist = new ArrayList<String>();
horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
horizontalscrollview.setHorizontalFadingEdgeEnabled(false);
hlayout = (LinearLayout)findViewById(R.id.horizontal_menu);
prev = (ImageView)findViewById(R.id.imgprev);
next = (ImageView) findViewById(R.id.imgnext);
lstfilter = (ListView) findViewById(R.id.lstfilter);
mylist.add("Rs.250 n Below(253 result found)");
mylist.add("Rs.251 - 500(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mylist.add("Rs.501 - 750(25301 result found))");
mycategorylist.add("Brand");
mycategorylist.add("Price");
mycategorylist.add("Discount");
mycategorylist.add("Availability");
mycategorylist.add("More");
array_image.add(R.drawable.img_brand);
array_image.add(R.drawable.img_rupee);
array_image.add(R.drawable.img_discount);
array_image.add(R.drawable.yellow_check_mark);
array_image.add(R.drawable.yellow_check_mark);
horizonAdapter = new HorizontalScrollAdapter(this, mycategorylist,array_image);
horizontalscrollview.setAdapter(horizonAdapter);
// Getting adapter by passing xml data ArrayList
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
int firstVisibleItem = horizontalscrollview.getFirstVisiblePosition();
int lastVisibleCount = horizontalscrollview.getLastVisiblePosition();
if (firstVisibleItem != 0) {
int count1 = firstVisibleItem
- (lastVisibleCount - firstVisibleItem);
if (count1 < 0) {
horizontalscrollview.setSelection(0);
}
else {
horizontalscrollview.setSelection(count1);
}
Log.d("item selected:",""+count1);
if(count1 == 0)
{
Log.d("first item visible","");
prev.setVisibility(ImageView.GONE);
next.setVisibility(ImageView.VISIBLE);
}
else
{
prev.setVisibility(ImageView.VISIBLE);
next.setVisibility(ImageView.GONE);
}
}
}
});
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// horizontalScroll.scrollTo(
// (int) horizontalScroll.getScrollX() + 10,
// (int) horizontalScroll.getScrollY());
horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
int count = horizontalscrollview.getCount();
int firstVisibleItem = horizontalscrollview.getFirstVisiblePosition();
int lastVisibleCount = horizontalscrollview.getLastVisiblePosition();
if (lastVisibleCount != count) {
int count1 = lastVisibleCount
+ (lastVisibleCount - firstVisibleItem);
if (count1 > count) {
horizontalscrollview.setSelection(count);
} else {
horizontalscrollview.setSelection(count1);
}
Log.d("item selected:",""+lastVisibleCount);
if(count1 == lastVisibleCount)
{
Log.d("last item visible","");
prev.setVisibility(ImageView.VISIBLE);
next.setVisibility(ImageView.GONE);
}
else
{
prev.setVisibility(ImageView.VISIBLE);
next.setVisibility(ImageView.GONE);
}
}
}
});
adapter = new MyAdapter(this, mylist);
lstfilter.setAdapter(adapter);
}
activity_filter.xml
<?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="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#0088cc"
android:gravity="center_vertical"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:background="#null"
android:scaleType="fitCenter" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25" />
<ImageButton
android:id="#+id/imgDeliveryBack"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.28"
android:background="#null"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/img_back_arrow" />
<TextView
android:id="#+id/txvdeliveryheader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:fontFamily="Roboto regular"
android:gravity="left|center"
android:text="Filter"
android:textColor="#f8f8f8"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".9"
android:gravity="left|center"
android:text="#null"
android:textColor="#ffffff" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgprev"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:background="#3374a3"
android:src="#drawable/img_back_arrow" />
<LinearLayout
android:id="#+id/horizontal_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:background="#168fcc"
android:orientation="horizontal" >
<Gallery
android:id="#+id/horizontalscrollview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:background="#drawable/selected"
android:columnWidth="70dp"
android:fadingEdgeLength="0dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</LinearLayout>
<ImageView
android:id="#+id/imgnext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:background="#3374a3"
android:src="#drawable/img_next" />
</LinearLayout>
<ListView
android:id="#+id/lstfilter"
android:layout_width="match_parent"
android:layout_height="340dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="40dp"
android:layout_weight="0.3" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#035f8d" >
<TextView
android:id="#+id/txvremove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:padding="5dp"
android:src="#drawable/icon_trash_white"
android:text="#string/clear_filters"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#000000" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:padding="5dp"
android:src="#drawable/icon_trash_white"
android:text="APPLY"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
activity_horizontal_item.xml
<?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="wrap_content"
android:background="#168fcc"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgcategory"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/txvcategoryname"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#066da1" />
</LinearLayout>
</LinearLayout>
HorizontalScrollAdapter.java
public class HorizontalScrollAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> data;
ArrayList<Integer> image;
private static LayoutInflater inflater = null;
// public ImageLoader imageLoader;
public HorizontalScrollAdapter(Activity a, ArrayList<String> list,ArrayList<Integer> image_array) {
activity = a;
data = list;
image = image_array;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.activity_horizontal_item, null);
TextView txvfirst = (TextView) vi.findViewById(R.id.txvcategoryname); // title
ImageView imgcategory = (ImageView) vi.findViewById(R.id.imgcategory);
// HashMap<String, String> song = new HashMap<String, String>();
// song = data.get(position);
// Setting all values in listview
// title.setText(song.get(CustomizedListView.KEY_TITLE));
// artist.setText(song.get(CustomizedListView.KEY_ARTIST));
// duration.setText(song.get(CustomizedListView.KEY_DURATION));
// imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL),
// thumb_image);
txvfirst.setText(data.get(position));
imgcategory.setImageResource(image.get(position));
return vi;
}
how can I display text below each image of gallery and I also want to remove space before and after each gallery item.
Actually in your activity_filter.xml layout, the parent layout(LinearLayout here) for Gallery widget is having a fixed height of 40dp, if increase it or handle it with weights or something it will show the TextView. You can see, here I have increased that height to 100dp and it shows the TextView clearly. And the space is coming because you have given horizontalSpacing as 5dp, trying reducing that if you don't want the spaces between column.
UPDATE:-
I have updated my answer to remove spaces around Gallery, Here I have RelativeLayout and now the Prev and Next buttons are overlapping with gallery. Hope it solves your problem.
<?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="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#0088cc"
android:gravity="center_vertical"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:background="#null"
android:scaleType="fitCenter" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25" />
<ImageButton
android:id="#+id/imgDeliveryBack"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.28"
android:background="#null"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_pass" />
<TextView
android:id="#+id/txvdeliveryheader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:fontFamily="Roboto regular"
android:gravity="left|center"
android:text="Filter"
android:textColor="#f8f8f8"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".9"
android:gravity="left|center"
android:text="#null"
android:textColor="#ffffff" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/horizontal_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#168fcc"
android:orientation="horizontal" >
<Gallery
android:id="#+id/horizontalscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/drawer_shadow"
android:columnWidth="70dp"
android:fadingEdgeLength="0dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</LinearLayout>
<ImageView
android:id="#+id/imgprev"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_action_pass" />
<ImageView
android:id="#+id/imgnext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_like" />
</RelativeLayout>
<ListView
android:id="#+id/lstfilter"
android:layout_width="match_parent"
android:layout_height="340dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="40dp"
android:layout_weight="0.3" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#035f8d" >
<TextView
android:id="#+id/txvremove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:padding="5dp"
android:src="#drawable/ic_action_pass"
android:text="#string/abc_action_bar_home_description"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#000000" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:padding="5dp"
android:src="#drawable/ic_action_like"
android:text="APPLY"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>

GridView with image and text

I'm very new in android and need a little help with this task.
I have gridview witch pull images and text from database and display them. Currently is displayed like image and on the right of the image is text. I want to make text under the image. Can you help me with this task. This is the gridview.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2" >
</GridView>
</LinearLayout>
And this is how I display image and text in it table_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Name"
/>
</LinearLayout>
</LinearLayout>
And if need getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.table_column, null);
}
// ColPhoto
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setPadding(10, 10, 10, 10);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText("" + MyArr.get(position).get("name").toString());
return convertView;
}
u can use relative layout and set align textview to imageview.
try this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ColName"
android:layout_alignRight="#+id/ColName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ColPhoto"
android:gravity="center"
android:text="Name name" />
</RelativeLayout>
I don't know why add a single element LinearLayout in another LinearLayout.
if grid item need to be put in correct position, I'd like use RelativeLayout instead.
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ColPhoto"
android:text="Name"
/>
</RelativeLayout>
`
Try to change your orientation to "vertical".
EDIT:
Like, Tr4X said you don't need second LinearLayout. You can put both in one.
<?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:background="#drawable/bckimageviw" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>

Imageview in LinearLayout won't take parameters

How come my LinearLayout with dynamically added images won't take parameters ?
LinearLayout ImageView = (LinearLayout) findViewById(R.id.settings);
LinearLayout ImageView2 = (LinearLayout) findViewById(R.id.settings2);
LinearLayout.LayoutParams imagev = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imagev.topMargin=2;
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(R.drawable.ic_action_discard);
image.setId(Integer.parseInt(listitems.get(n).get("cbid")));
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
showpopup(v.getId());
}
});
if (listitems.get(n).get("state").toString().equals("1")) {
cb.setChecked(true);
my_checked_layout.addView(cb,params);
cb.setBackgroundColor(Color.argb(255, 184, 239, 220));
cb.setTextColor(Color.rgb(255, 255, 255));
ImageView2.addView(image, imagev);
} else {
cb.setChecked(false);
cb.setBackgroundColor(Color.argb(255, 19, 202, 140));
cb.setTextColor(Color.rgb(255, 255, 255));
my_layout.addView(cb,params);
ImageView.addView(image,imagev);
}
The my_checked_layout(cb, params); does take the other params
my XML, where the images and checkboxes are being set.
the parts that are being used are in both linear layouts with the id's Parent & parent2.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Name Label -->
<LinearLayout
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:textColor="#43bd00"
android:textSize="25dip"
android:textStyle="bold" />
<!-- green -->
<!-- Email Label -->
<TextView
android:id="#+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#EB0505" />
<!-- red -->
<TextView
android:id="#+id/list_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF00FF" />
<!-- pink -->
<LinearLayout
android:id="#+id/Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/test"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#13ca8c"
/>
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="#drawable/devider" />
<LinearLayout
android:id="#+id/Parent2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/checked"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
/>
<LinearLayout
android:id="#+id/settings2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#B8EFDC" />
</LinearLayout>
</LinearLayout>
</ScrollView>
If you want to add margin within the LinearLayout.LayoutParams you can call the setMargin() method of it.
example:
setMargins(135, 176, 0, 0);
where setMargins (int left, int top, int right, int bottom)
<LinearLayout
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
fixed it.

vertical scroll view for dynamically created buttons

I am having a problems with adding scroll view in my dynamically created buttons.here is my xml 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="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="27dp"
android:background="#drawable/custom_border"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="243dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="#string/hinttextitem" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="26dp"
android:background="#drawable/button_shape"
android:onClick="newonClick"
android:text="click" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:id="#+id/dynamic"
android:background="#drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
here is my java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newonClick(View V){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear1);
LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
dynamiclayout.removeAllViews();
EditText edittext = (EditText) findViewById(R.id.editText1);
int x=Integer.valueOf(edittext.getText().toString());
for(int i=1;i<=x;i++)
{
Button addButton =new Button(this);
addButton.setText("Button"+i);
addButton.setId(i);
dynamiclayout.addView(addButton);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"button is clicked"+v.getId(), 8000).show();
}
});
}
if am adding scroll view in dynamic layout, it will disappear.so how can i add vertical scroll view only for dynamically created buttons.can anyone solve my problem?
try this: place scroll view for LinearLayout only.
<ScrollView
android:layout_width="wrap_content"
android:layout_below="#+id/linear1"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="#drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Try to use,
In XML,
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1"
android:id="#+id/scrollView1">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
In Java:
this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.dynamic);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
Button[] btn = new Button[10];
for (int i = 0; i < 10; i++) {
btn[i] = new Button(this);
btn[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn[i].setText("This is the button" + i);
this.linearLayout.addView(btn[i]);
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1" >
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
please replace this code:

Categories

Resources