please help me, how to align the height to width. I've tried everything, it is not working.
Edit: Added full layout.
The code to create the button looks like this
LinearLayout showLayout = (LinearLayout) view.findViewById(R.id.show_full_btn);
showButton = new ImageButton(activityContext);
<LinearLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/count"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/w_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:typeface="normal"/>
<LinearLayout
android:id="#+id/show_full_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:scaleType="fitXY"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Thanks.
You are giving android:scaleType to a LinearLayout which is not applicable, it works with ImageButtons, so declare your view as an ImageButton instead and apply scalyType to it, or if you kinda stuck with a layout, just make as this answer says:
Try using FrameLayout with an ImageView and LinearLayout inside. For
example, try changing the alpha of the image and move it to foreground
in your FrameLayout, thus the LinearLayout stays on background.
If you're finding it difficult to get your results from the XML layout, then try to do it within the onCreate method of your activity. You can easily get it done.
You are talking about an ImageButton in your question, but have not used an ImageButton in your XML, just LinearLayout. Also, in your java, the showLayout and showButton are two separate variables, and not connected.
Related
I have an xml like below.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/etMsisdn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/allView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/msisdn"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:hint="MSISDN"
android:inputType="numberDecimal"
/>
<ImageView
android:layout_width="60px"
android:layout_height="match_parent"
android:src="#drawable/scan"/>
</LinearLayout>
</LinearLayout>
............
Another View
............
</LinearLayout>
How do I add EditText and ImageView programatically inside the horizontal LinearLayout (allView) and add the allView inside Vertical LinearLayout(etMsisdn) while keeping the same attribute as in xml.
The EditText and ImageView r supposed to below the msisdn edittext
here is your solution
LinearLayout allview=(LinearLayout)findViewById(R.id.allView);
EditText edt=new EditText(this);
ImageView img=new ImageView(this);
allview.addView(edt);
allview.addView(img);
put this in your activity
You have to use the addView method on the layout object.
But, if you want to add more times the same "sub layout" in the main layout it's better to create an xml layout with the "sub part" and add it programmatically. Let me know if the second case is what you need to provide the code.
Find LinerLayout and add views:
LinearLayout root = (LinearLayout)findViewById(R.id.allView);
root.addView(someView);
and add the attributes like this on your view:
How to programmatically set textview-s and their properties?
You need to get a reference to the outermost LinearLayout (the first one in your layout), so the best idea is to give it an Id:
<LinearLayout
android:id="#+id/myContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
Then, you need to get the reference to this layout and add the children views, like so:
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.myContainer);
containerLayout.addView(yourView1);
containerLayout.addView(yourView2);
To set the desired layout alignment, you can either manually set the required LayoutParams (see this answer in SO) or you could inflate a layout and add it to your current layout, instead of two individual views (EditText and ImageView) (see this answer in SO).
I tried Expand ListView method from using the code from the following blog, https://wirasetiawan29.wordpress.com/2016/01/20/membuat-expand-listview-material-design-di-android/
Everything works fine. But if I add a button in FrameLayout then the touchevent for listview item not works properly. Also I tried changing FrameLayout to Relative & also to Linear, but still no success.
<FrameLayout
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Share"/>
</FrameLayout>
Thanks in advance.
According to Frame Layout description by Google's documentation...
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
Hence, your original TextView is actually overlapping by the Button (Share). You can use android:layout_gravity="right" to position the button in the right end of the screen, however, then you will have to fix the maxium length of string for TextView, so that it doesn't get overlap by the Button on the right.
If you don't have any problem, might I suggest you to use LinearLayout? It's easier to handle and render by the GPU (As far as I know). Here's an example code of your item...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:text="This is a big chunk of description for your listView item. you can write as much as you want...."
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:layout_gravity="right|center"/>
</LinearLayout>
You can also use RelativeLayout and GridLayout here.
I hope it answers your question. Cheers!
I have a TextView whose width should not exceed the ImageView above it. Both image and text are downloaded from server and I don't know their dimensions (can't make assumptions either). I went through the logic to wrap the text content using this SO post.
Here is my layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLL"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:id="#+id/LL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:width="0dp"
android:text="This is a string whose width may or may not be more than the image downloaded" />
</LinearLayout>
<TextView android:background="#android:color/holo_red_dark"
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second Text"/>
</LinearLayout>
With this code, the TextView at the end (text2) does not even show up. There are 2 solutions to this issue :
Apply android:maxLines="5" to the text1. Problem with this approach is that Text1 view would always be 5 lines high (I understand 'lines' is not a unit of height, but that's what I see visually). So if the text content is just one word, there would be a big white space below. And then text2 shows up.
Change topmost linear layout (parentLL) to RelativeLayout. text2 can then be used with alignBelow=LL1. This works as expected. But I cannot migrate the topmost view to RelativeLayout, because this view is from a library not in my control. I can only modify LL1 and it's children. Due to my code, other views below (like text2) are suffering (by not showing up).
There is a third approach for setting the textview as a compound drawable on ImageView. I guess that might work (haven't tested), but my requirement is to show the TextView if image download has failed (which can be detected only after a while). So I need to have a TextView. Also, my LinearLayout LL1 can have other children too.
I would request for some help understanding :
Why is my code not showing up the content below the textview 'text1'? With width=0 on textview it seems to set the height of the parent to be match_parent.
How is RelativeLayout able to handle this smoothly ? Can I replicate any of that behavior in TextView's onMeasure ? Assume I have callbacks to detect image has been downloaded, and I can get image width also.
I think what you are running into is a conflict of setting the width and height but not setting the layout weight, which is a key factor in how Linear Layouts work. If you add one more vertical LinearLayout in there and then move #id/text2 into it, you should be set. You'll need something like the following (obviously modified to your specs, this was just a quick test). Note my use of android:layout_weight,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2" />
</LinearLayout>
</LinearLayout>
Which splits the screen in half vertically as shown in this picture,
Photo of resulting layout
I had to wrap the TextView in a RelativeLayout, which was wrapped by a LinearLayout. Not happy with this solution, but this is the only way for now.
I have this LinearLayout that is going to be placed on the bottom of an activity layout. I want this LinearLayout to have a 4dp elevation, just like the top toolbar should have, however, since android:elevation places the shadow below the ui component and this specific component (linearLayout) is going to be on the bottom of the screen, I won't see any elevation at all..
This is my LinearLayout code, and an image of it with the default elevation implemented:
<?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:weightSum="3"
android:background="#android:color/transparent"
android:orientation="horizontal"
android:elevation="4dp"
android:layout_alignParentBottom="true">
<ImageButton
android:id="#+id/playButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_play"
style="?android:attr/borderlessButtonStyle" />
<ImageButton
android:id="#+id/stopButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_stop"
style="?android:attr/borderlessButtonStyle" />
<ImageButton
android:id="#+id/bookmarkButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_bookmark"
style="?android:attr/borderlessButtonStyle" />
</LinearLayout>
Is there a way, using elevation to place a shadow on top of the ui component?
Thanks in advance!
You can't theoretically do it with android:elevation, in the sense that you can't choose the direction where the shadow is going to be cast.
There are two solutions.
1. Drawables
You could, for instance, put an ImageView right above your layout and set android:src="#drawable/shadow". This should be a vertical GradientDrawable defined in XML, as explained here.
2. Workaround
While most of the shadow is actually below the view, a subtle shadow is also above. A workaround might be using a very high value for elevation, like 40dp: the bottom part is going to be hidden due to your layout, while the top is going to be expanded and look like a common shadow.
In either case, you do not have control over the elevation value in dp, in the sense that you can't be sure your shadow is equivalent to the one cast by android:elevation=4dp.
use like as;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="205dp"
android:background="#color/white"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="-5dp"
android:background="#color/white"
android:elevation="3dp"
android:paddingTop="5dp">
// CHILD VIEWS
</RelativeLayout>
</LinearLayout>
Add this code above of view in which you want top elevation or add android:layout_marginTop="-4dp" and add below of above view
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0.01dp"
android:layout_below="#+id/scrollbar"
app:cardBackgroundColor="#android:color/transparent"
app:cardCornerRadius="0dp"
app:cardElevation="#dimen/dp_4" />
Try this i am unable to set background through xml then i tried the programmatic way to solve that and i work perfectly refer this if facing problem on show shadow in Linear Layout
lprofile.setBackgroundResource(R.drawable.background_with_shadow);
lprofile is my ref of Linear layout and background_with_shadow is xml for applying shadow i hope i will for ur requirmement...thank u..
I'm getting two differents behavior for a ListActivity row. Eclipse Graphical Layout show the right behavior, but at runtime, on the device, the layout:weight doesn't seem to work properly and the Textview is resized to the minimum width, depending on the text property.
This is how I had setup the layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#color/defaultBg"
android:orientation="horizontal">
<ImageButton
android:id="#+id/myButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="2"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/myText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="#2288EE"
android:layout_weight="6" android:text="Hello" android:textColor="#android:color/white"/>
</LinearLayout>
EDIT: I just found that the LinearLayout doesn't fill the listRow width. I'm still trying to find how to solve
Any help will be very appreciated.
I fix the issue myself. The problem wasn't in the table row layout posted above. The problem was in the android:layout_width of the parent ListView which was set to 'wrap_content' instead of 'fill_parent'.
Thanks to everyone who offered his contribute!
In your LinearLayout put android:weightSum="8". It should do the trick, on my device works fine.
Try adding android:scaleType="fitCenter" to your ImageButton. If I'm not mistaken, your source Drawable is too big, therefore making the ImageButton take too much horizontal space.