In my app, I insert pairs of spinners into a linearlayout, which is inside a scrollview. Problem is, when I add enough spinners, the top two are partially obscured. Here's what I'm talking about.
<LinearLayout
android:id="#+id/ChordHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:drawable/bottom_bar"
android:orientation="horizontal" >
<Button
android:id="#+id/addChordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Add Chord" />
<TextView
android:id="#+id/spacetext1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/removeChordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Remove Chord" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/ChordList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isScrollContainer="true"
android:orientation="vertical"
android:weightSum="100" >
(Spinners get inserted here)
</LinearLayout>
</ScrollView>
</LinearLayout>
And here is the code that inserts spinners into the list, if the problem is with that.
Spinner chordName = new Spinner(this);
Spinner chordType = new Spinner(this);
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout chordList = (LinearLayout) findViewById(R.id.ChordList);
chordName.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 80, 50));
chordName.setAdapter(nameAdapter);
container.addView(chordName); //add name spinner to container
chordType.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 80, 50));
chordType.setAdapter(typeAdapter);
container.addView(chordType); //add type spinner to container
container.setBackgroundResource(android.R.drawable.bottom_bar);
container.setPadding(10, 0, 10, 0);
chordList.addView(container); //add container to list layout
Here is my layout.. try it out :)
<?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">
<RelativeLayout
android:id="#+id/ChordHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/bottom_bar">
<Button
android:id="#+id/addChordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Add Chord" />
<Button
android:id="#+id/removeChordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ellipsize="middle"
android:text="Remove Chord" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ChordHeader"
>
<LinearLayout
android:id="#+id/ChordList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now for the buttons you might want to set a dimen if you really want them the same size.
Here is how to store dimensions in Android : More Resource Types: Dimension
Try getting a reference to the top item in the list and then request focus on it, after you are done adding the spinners.
chordList.getChildAt(0).requestFocus();
Related
I've a layout like design 1 and I want to align cancel button to bottom of the dialog always. recyclerview1 & recyclerview2 have different heights due to display content. if the both recyclerview1 and recyclerview content are small and able to display within screen without scroll it should be like design 2 . If the recyclerviews exceeded the heights dialog should take the device height and align cancel button to bottom of the dialog like design 3.
Current output and final output
Current code:
<?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:layout_centerInParent="true" >
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="text1"
android:textSize="10sp" />
<TextView
android:id="#+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/textview1"
android:text="text2"
android:textSize="10sp" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_below="#id/textview2"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutContainer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutContainer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</RelativeLayout>
You will have to create a custom dialog
inside onViewCreated() inflate a layout_bottom_button using gravity bottom, so inflated layout will always be at bottom
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.BOTTOM;
View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
this.getDialog().addContentView(viewM, params);
keep in mind that recyclerView should have bottom margin same as layout_bottom_button layout height, so that recyclerView does not hide below button.
I'm using the below xml for dialogue in android.
However the background only applies on ViewPager, does not apply on linearlayout under the view pager. but that linearlayout is always white..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/AA"
android:layout_width="wrap_content"
android:layout_height="560dp"
android:background="#fcfcfc"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/BBB"
android:layout_width="match_parent"
android:layout_height="333dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="18dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/dialog_page_mark"
android:layout_width="match_parent"
android:layout_height="18dp"
android:gravity="top|center_horizontal"
android:orientation="horizontal" />
<TextView
android:id="#+id/dialog_page_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15dp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="67dp"
android:gravity="end|center_vertical"
android:orientation="horizontal"
android:paddingEnd="24dp" >
<Button
android:id="#+id/CCCC"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:focusable="true"
android:gravity="center"
android:minWidth="0dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textAllCaps="true"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And, in the code, I call these codes,
WindowManager.LayoutParams wm = new WindowManager.LayoutParams();
wm.copyFrom(this.getWindow().getAttributes());
wm.width = LayoutParams.WRAP_CONTENT;
wm.height = LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(wm);
getWindow().setBackgroundDrawableResource(R.color.FCFCFC);
Am I wrong??
R.color.FCFCFC = "#fcfcfc"
Why the backgound color setting is not working the linearlayout under ViewPager?? Help me... Please..
Assign an id for your LinearLayout
Using below code to get instance:
LinearLayout ll = (LinearLayout) findViewById(R.id.yourLinearLayout);
Using below code to change background color:
ll.setBackgroundColor(getResources().getColor(R.color.FCFCFC));
I need to arrange two ListViews in a vertical LinearLayout in such a way that 2nd ListView will be at the end of last item of first ListView.
If ListView1 is empty, the view should be something like this.
As ListView1.size() increases ListView2 should be pulled down to a limit.
ListView1 reaches a fixed height (or weight) , it should stop expanding and start scrolling.
I think there is no method in android to set maxHeight for View.
I have already tried almost all combinations of layout_height="wrap_content", fixed heights and weights for both ListViews. In all cases,
either 1st requirement(image1) will fail : ListView1 will not be at top
or last requirement(image3) will fail : ListView2 will get pushed out of the screen.
Any help or suggestions will be appreciated. Thanks in advance.
Edit:
my current code is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.6" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.4" >
</ListView>
</LinearLayout>
I have tried with different weight combinations, like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.4" >
</ListView>
</LinearLayout>
But it will fail last requirement.
Edit2: #dangVarmit
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp" >
</ListView>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
If you mean this, It still fails req3.
You can find the below code for right and left listview you can change it to above and bottom listview or as you want to show
main_activity.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">
<LinearLayout
android:id="#+id/main_top_bit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/orange">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="#+id/button_1"
style="#style/landscape_top_button"
android:text="button1" />
<Button
android:id="#+id/button_2"
style="#style/landscape_top_button"
android:text="button2"/>
</LinearLayout>
</LinearLayout>
<View android:id="#+id/main_horizontal_bar"
android:layout_height="3dip" android:layout_width="fill_parent"
android:background="#color/gray" android:layout_marginTop="1dip" />
</LinearLayout>
<LinearLayout
android:id="#+id/lower_linear_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/main_top_bit">
<RelativeLayout
android:layout_below="#+id/lower_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/teal"
android:id="#+id/org_left_linearlayout2">
<Button
android:id="#+id/button_3"
style="#style/landscape_top_button"
android:text="button3"/>
<Button
android:id="#+id/button_4"
style="#style/landscape_top_button"
android:text="button4" />
</LinearLayout>
<ListView
android:background="#color/green"
android:id="#+id/lstLeft"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/org_left_linearlayout2"
android:layout_above="#+id/org_left_bottom_rl"
android:cacheColorHint="#00000000"/>
<RelativeLayout
android:id="#+id/org_left_bottom_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/purple" >
<Button
android:text="button5"
android:id="#+id/button_5"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
</RelativeLayout>
<View
android:layout_width="3dip"
android:layout_height="fill_parent"
android:background="#color/gray">
</View>
<RelativeLayout
android:layout_below="#+id/lower_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/olive"
android:id="#+id/org_right_linearlayout2">
<Button
android:id="#+id/button_6"
style="#style/landscape_top_button"
android:text="button6" />
<Button
android:id="#+id/button_7"
style="#style/landscape_top_button"
android:text="button7" />
</LinearLayout>
<ListView
android:id="#+id/lstRight"
android:background="#color/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/org_right_linearlayout2"
android:layout_above="#+id/org_right_bottom_rl"
android:cacheColorHint="#00000000"/>
<RelativeLayout
android:id="#+id/org_right_bottom_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/red"
android:layout_alignParentBottom="true">
<Button
android:text="button8"
android:id="#+id/button_8"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private final String TAG = this.getClass().getName();
ListView leftList;
ListView rightList;
static final String[] FRUITS = new String[] {"Apple", "Bananna", "Cherry", "Pear", "Strawberry"};
static final String[] VEGETABLES = new String[] {"Asparagus", "Bamboo shoots", "Celery", "Ginger", "Spinach"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
leftList = (ListView) findViewById(R.id.lstLeft);
rightList = (ListView) findViewById(R.id.lstRight);
populateLeftList();
populateRightList();
}
public void populateLeftList(){
leftList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, FRUITS));
}
public void populateRightList(){
rightList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, VEGETABLES));
}
}
for list1, layout_height=wrap_content, remove layout_weight
for list2, layout_height=100dp (any fixed value), remove layout_weight
add <Space layout_weight=1 layout_height=0dp/> at the end of the list.
that should give you the results you are looking for.
I have found one solution by myself. It works perfectly as i wanted. I dont know whether it is a good approach or not. I am searching for an xml solution.
getDeviceHeight() is my method to find device height in pixels.
200 is the approximate height of other ui elements above My ListViews.
myorderList is my first ListView
if(myorderList.getHeight() > ((getDeviceHeight()/2) - 200)) {
LayoutParams params = myorderList.getLayoutParams();
params.height = (int)((getDeviceHeight()/2) - 200);
myorderList.setLayoutParams(params);
}
This should be done when there is any kind of changes happening to the first ListView.
Edit:
This will also fail when items removed after reaching maximum height :-(
What i want in my activity layout is
2 spinners one after another
on the right or left side of spinner i want an imageview
and below the spinner i am adding various edittext and texviews.
I tried with linear layout and also relative layout not getting the desired result.
which layout should i use?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="72dp"
android:layout_height="61dp"
android:src="#drawable/ic_launcher" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
android:entries="#array/category"
android:prompt="#string/country_prompt"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
you should do following:
1) take linear layout with horizontal orientation.
2) put image view and spinner in it...and give the weight for both according to ratio of 10.
3) Same thing for another spinner and imageview.
4) And finally you have added editext below spinner in vertical layout.
You can use this and get your solution:
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_launcher" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
I have some problem with Listview. Listview has list items that consist of imageview and textview. Textview contains clickable links. When I set LinkMovementMethod to textviews, listitems doesn`t receive onclick event. I still cannot find any right solution=(. Can anyone explane how to solve it or give an example of using textview with clickable links inside listitem? Thanks.
Here is textview creation in adapter:
TextView text = (TextView) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.post_content_text, null);
SpannableString sString = new SpannableString(d.getText());
//I have my own class UrlSpan that contain fields startPosition, endPosition and urlLink
for (UrlSpan us : ((TextData) d).getUrls())
{
URLSpan urlSpan = new URLSpan(us.getUrl());
sString.setSpan(urlSpan, us.getStart(), us.getEnd(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
}
text.setText(sString);
text.setMovementMethod(LinkMovementMethod.getInstance());
view.addView(text);
and list item is:
<?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:visibility="visible" >
<LinearLayout
style="?PostListRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/user_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="3dp"
android:clickable="true"
android:src="#drawable/ic_no_photo" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_margin="0dp"
android:orientation="vertical" >
<TextView
android:id="#+id/user_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clickable="true"
android:textSize="#dimen/title_in_list" />
<TextView
android:id="#+id/post_datetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textSize="#dimen/text_size_small" />
</LinearLayout>
<TextView
android:id="#+id/post_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="123" />
</LinearLayout>
<!-- it is a view where textview with links is to be added to -->
<LinearLayout
android:id="#+id/post_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="6" >
</LinearLayout>
<TextView
android:id="#+id/post_view"
style="?PostText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone" />
<LinearLayout
android:id="#+id/topic_edit_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
try to add this attribute
android:descendantFocusability="beforeDescendants"
to your listview
from the documentation:
it defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. it seems to me that when you have some active views like buttons or edittext and etc in your listview you have to define descendantFocusability