ViewFlipper with a variable number of liniar layout - android

In my app I need to use ViewFlipper for Switching screens by dragging over the touch screen. I have a ListView that contains some items. When a item is select I must to display some images. The problem is that the number of images is different from one item to another. So I think that I must create the LinearLayout and the ImageView programatically. Is it possible what I am tring to do?
Thanks in advance..
Here is the code I tried :
<?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="#000000"
android:id="#+id/layout_main">
<ViewFlipper android:id="#+id/details"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>
and on Oncreate() method :
for (int i = 0; i < jArray.length(); i++) {
LinearLayout l=new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
l.setBackgroundColor(0xFFFFFFFF);
l.setOrientation(LinearLayout.VERTICAL);
ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//ImageView img1 = (ImageView) findViewById(R.id.photo1);
Drawable image1 = ImageOperations(getBaseContext(), url[i]);
img.setImageDrawable(image1);
l.addView(img);
}
but nothing happens. I see only a big black screen.

I exactly didnt understand what you saying but i worked on ViewFlipper before. There am using Four linear layouts as childviews for ViewFlipper which in xml. (where as firstly i tried to add listviews direclly as childviews but adding linearlayouts will make easier)
and added listviews,imageviews like you said to corresponding linearlayouts from java code.
and it worked .
Hope it helps.
Here my code i used for xml :
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tabrelay"
android:gravity="center_horizontal"
>
<ViewFlipper android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/FlipperMargins.flipper"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:id="#+id/list1"
android:listSelector="#color/sprgreen"
android:drawSelectorOnTop="true"
android:background="#drawable/deckbackn"
style="#style/Webview.view"
/>
<LinearLayout
android:layout_width="#dimen/webViewWidth"
android:layout_height="#dimen/webViewHeight"
android:layout_gravity="center"
android:divider="#color/grey46"
android:dividerHeight="1dp"
android:id="#+id/list2"
android:background="#drawable/white"
style="#style/Webview.view"
/>
<LinearLayout
android:layout_width="#dimen/webViewWidth"
android:layout_height="#dimen/webViewHeight"
android:layout_gravity="center"
android:orientation="vertical"
android:id="#+id/list3"
android:listSelector="#color/sprgreen"
android:drawSelectorOnTop="true"
android:background="#drawable/commentsbg"
style="#style/Webview.view"
/>
<WebView
android:id="#+id/helloWebView"
android:layout_width="#dimen/webViewWidth"
android:layout_height="#dimen/webViewHeight"
android:layout_gravity="center"
android:background="#drawable/white"
style="#style/Webview.view"
/>
</ViewFlipper>
</RelativeLayout>
after that you can get Ids of linearlayouts from xml and add whaterver you want listviews etc from java..,,

Related

How to make a list of textviews?

Basically here is a mspaint demonstraiton of what I want to accomplish:
I do not want the outline to be visible to the user, it was just to give you an idea of what I want to do. I want new textviews to take position under each other whenever a new goal is added from the edittext-button combo on the bottom part of the GUI.
I know how to save the data - SQLite. But I have no idea how to make new textviews be on the bottom of the last one - and if there is no more room on the screen I want that textview part to be scrollable.
I don't know what I need to use to accomplish this.
Thanks in advance!
create two xml files.
One consisting of list view
<?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"
android:background="#drawable/bkgrnd_320x285">
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
and another xml for just the textView eg. row.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="horizontal"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
Then in the main activity u need to refer to both the xml files as follows
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
where R.layout.row is the row.xml consisting of textView
And R.id.textView2 is the id of the textView present in the row.xml
You may create a LinearLayout, setting the orientation to vertical.
Adding a ScrollView or ListView to show the upper part, with android:weight=1
Add the EditText to the LinearLayout, and this is done.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Dynamically adding views to RelativeLayout inside ScrollView

I am trying to add dynamically created several RelativeLayouts into a LinearLayout which is inside a RelativeLayout, which is inside a ScrollView. When the total height of the all views exceed the size of the phone screen, all views are displayed correctly. But when the total size of dynamically added views is not enough for filling the screen, only the first RelativeLayout element is shown and the others are not displayed in the screen. I am really hopeless and do not understand why.
Here is the code to dynamically populate views inside linear layout:
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(Comment c: commentsList) {
RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(
R.layout.list_item_comment, null, false);
TextView tv = (TextView) layoutItem.findViewById(R.id.textView);
ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);
// set tv's text
// set iv's image and onclicklistener, nothing fancy here, everything goes well
commentsLayout.addView(layoutItem);
}
Here is list_item_comment.xml:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:textSize="16sp"
android:layout_toRightOf="#id/imageView"
/>
</RelativeLayout>
And here is the xml file for this activity:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/main_layout"
>
...
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:id="#+id/scrollView"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relativeContainer"
>
...
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/comments_layout"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And the screenshots:
Without sufficient layouts: (INCORRECT, needs to show 3 comments)
With enough layouts: (CORRECT ONE, screen is filled)
I just need to show all three comments in the first case :/ Thanks in advance.
instead of fill_parent, try changing the layout_height of the <RelativeLayout> of your list_item_comment.xml to wrap_content.
Also, why do you need another <RelativeLayout> inside your <ScrollView> of the xml of your activity. The LinearLayout is sufficient to do what you want your activity to look like. Maybe you can just remove it.

Using a listview inside a scrollview

In my project I used setcontentview(R.layout.first).here. In a gallery I am displaying second.xml. In second.xml I have one listview.when. I am trying to scroll these listviewitems total screen scrolling, but it is not scrolling properly. Because outside we used scrollview. When I delete outside scrollview we cannot see the bottom of listview. How to resolve this issue?
Use ListView's headers and footers if you want to put some items only at the end/beginning of the ListView.
Watch this video beginning from 42:40 it's really useful.
A list view will scroll automatically once it goes past its parents height or the ListViews maximum height
If you put your ListView/any scrollable View inside the scrollView it will not work properly because when you touch the screen ,main focus of your touch is on parent view(scrollView ) not the child View (ListView).
<?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="#ffffff"
>
<ListView android:layout_height="match_parent" android:id="#+id/all_workouts_list"
android:cacheColorHint="#ffffffff"
android:layout_width="match_parent"
android:layout_above="#+id/add_workout_all_workout_list"></ListView>
<Button android:layout_width="wrap_content"
android:id="#+id/add_workout_all_workout_list" android:layout_height="wrap_content"
android:text="Add Workout" android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"></Button>
</RelativeLayout>
ListView inside ScrollView is not possible.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- here the picture info -->
</LinearLayout>
</LinearLayout>
I have no any ready example with me,
So written a raw code like this as you asked in comment,
ListView lv = (ListView) findViewById(R.id.listView);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
Button btn = new Button(this);
ll.addView(btn);
lv.addFooterView(ll, null, true);

How to get 2 images attached to layout bottom

I want to attach 2 images
1st at left bottom corner and other on right bottom corner
but I want that images to be hardcore in bottom so that if I scroll the page the images should remain at the same place
Dynamically ,i am succesful to answer my own question so i m hereby putting the code for all the user and it works so have the code and enjoy
LinearLayout llMain=new LinearLayout(this);
llMain.setOrientation(LinearLayout.VERTICAL);
llMain.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
llMain.setBackgroundColor(Color.WHITE);
LinearLayout llLsView=new LinearLayout(this);
llLsView.setOrientation(LinearLayout.VERTICAL);
llLsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1.0F));
//llLsView.setPadding(0,0,0,20);
LinearLayout llImage=new LinearLayout(this);
llImage.setOrientation(LinearLayout.HORIZONTAL);
llImage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
llImage.setBackgroundColor(Color.BLACK);
LinearLayout llHome=new LinearLayout(this);
//llHome.setOrientation(LinearLayout.HORIZONTAL);
llHome.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT,0.5F));
llHome.setPadding(0,0,5,0);
llHome.setGravity(Gravity.RIGHT);
ImageView imgHome=new ImageView(this);
imgHome.setImageResource(R.drawable.home);
llHome.addView(imgHome);
LinearLayout llBack=new LinearLayout(this);
//llBack.setOrientation(LinearLayout.HORIZONTAL);
llBack.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT,0.5F));
llBack.setPadding(5,0,0,0);
llBack.setGravity(Gravity.LEFT);
ImageView imgBack=new ImageView(this);
imgBack.setImageResource(R.drawable.back);
llBack.addView(imgBack);
llImage.addView(llBack);
llImage.addView(llHome);
llMain.addView(llLsView);
llMain.addView(llImage);
setContentView(llMain);
Something like:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/nvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/nvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
<RelativeLayout >
<ScrollView >
</scrollView>
<RelativeLayout android:layout_alighParentBottom=true abdroid:backgrond=#color/transparnt">
<Button >
</Button >
<Button android:layout_alighParentRight=true>
</Button >
<RelativeLayout >
</RelativeLayout >

Adding a view to a LinearLayout at a specified position

I have the following main.xml file with a LinearLayout
<?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:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the 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:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)

Categories

Resources