I've got an activity with the following layout:
<?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">
<com.google.android.maps.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<LinearLayout
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="5dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
I want to be able to hide/show the seperate LinearLayout with id 'view' and then let the MapView resize according to the space left. When I try this the seperate LinearLayout is never shown. I'm not sure what is going on! Is the LinearLayout forever hidden? is it behind the Mapview?
private void showMessage() {
LinearLayout layout = (LinearLayout) findViewById(R.id.view);
layout.setVisibility(View.VISIBLE);
}
private void hideMessage() {
LinearLayout layout = (LinearLayout) findViewById(R.id.view);
layout.setVisibility(View.GONE);
}
Problem seems to be resolved by changing the order of initialization in the XML layout file, and it probably won't occur in future Android versions.
Related
I am inflating a bottom_sheet_layout. It contains a LinearLayout as the root element. It has one child CardView.
I've tried the following:
setting `android:background` property to
1. #00FFFFFF
2. #00000000
3. #android:color/transparent
setting the background color of LinearLayout programatically
`LinearLayout l = container.findViewById(R.id.root_element);
l.setBackgroundColor(Color.TRANSPARENT);`
This is the BottomSheet.Class that is linked to the bottom_sheet_layout
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
LinearLayout l = container.findViewById(R.id.root_element);
l.setBackgroundColor(Color.TRANSPARENT);
return v;
}
This is the full bottom_sheet_layout
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
android:id="#+id/root_element"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp">
<!-- A CardView that contains a TextView -->
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/poiName"
android:fontFamily="#font/comfortaa_bold"
android:text="#string/poiName"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"/>
<!--Location-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:fontFamily="#font/work_sans_medium"
android:text="#string/ui_location"
android:textFontWeight="800" />
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:fontFamily="#font/work_sans"
android:text="#string/ui_location_addr" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/share"
android:scaleType="center"
card_view:backgroundTint="#color/colorPrimary" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is the Map Fragment. Here is where I inflate the bottom_sheet_layout
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
Following several guides, I still could not get the #00FFFFFF method to work. Programmatically setting the color to transparent, crashes the application.
It seems that in earlier questions on StackOverflow, setting the background to the hex code with alpha value used to work. Now I only get a dim gray background.
Screenshots of the application
Image Layout
App ScreenShot
If you are not using MapView, I supposed that you are inflating the Google Map + bottom_sheet_layout in the same fragment. If Yes, I think that your issue is not the LinearLayout transparency but you are inserting bottom_sheet_layout to the bottom of the fragment container, not over the map.
You can use this layout for your Map fragment
(androidx, but very similar if you are using android)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- use this fragment for your google map -->
<fragment
android:id="#+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- use this fragment for your bottom_sheet class -->
<fragment
android:id="#+id/bottom_sheet"
android:layout_margin="16dp" // with this you do not need the LinearLayout root_element
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Then, you can play with your CardView (or LinearLayout) visibility to show the card or not.
Other layouts are possible like using directly the card view instead of a Fragment.
I think I can help you with some sort of solutions, which you can also try and see if that works or not.
Make use of alpha property in your .java file
Range the value as per your ease
LinearLayout l = container.findViewById(R.id.root_element);
l.setAlpha(0.4);
You can make use of alpha in your XML file too:
<LinearLayout
android:id="#+id/l1"
android:alpha="0.5"
android:layout_width="190dp"
android:layout_height="match_parent">
0.0 is for fully transparent and 1.0 is for fully opaque.
Let me know if any of the solutions work for you. However, do more research here, you will definitely get a solution. Happy coding.
Try background null.
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#null">
......
</LinearLayout>
Are you sure that the child layout elements doesn't have backgrounds?
I have the following xml which has Gridview and framelayout below it.
My requirement is if I scroll the screen upwards whole screen should scroll, but for me Only listview scrolls
Below is the xml 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time_card_header"
android:id="#+id/scrollView">
<GridView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</ScrollView>
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
Actually GridView and Listview have already scroll. No need to put the above widgets in scrollview. Firstly, remove the scrollview. I think you need the FrameLayout to be fixed at the bottom and show gridview above. For that first use FrameLayout in the linear layout with align_parentbottom true. after that please place the gridview above FrameLayout. That will fix the problem.
Just put linearlayout below scroll view and then gridview.
<scrollview>
<linearlayout >
<gridview />
</linearlayout >
</scrollview>
Sorry i am giving the ans from the mobile but the concept is same.
You can use RecyclerView with GridLayoutManager and set nestedScrollingEnabled of recyclerView to false.
and instead of ScrollView use NestedScrollView as parent of RecyclerView:
private void setUpRecyclerView(View view) {
recyclerView = view.findViewById(id.repliesList);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),numberOfColumns));
}
and xml file:
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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="match_parent"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<RecyclerView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
</android.support.v4.widget.NestedScrollView>
I'm trying to implement an IM-like application, but I get stuck on problem which really annoys me.
I use recyclerview to show messages in a scrolling manner, so that users can scroll the views to see all of the messages. Messages' base layout is a cardview
widget.
Additionally, I've use another layout within the Messages' layout which is also a cardview to show sub-messages.
To be more detailed, I would like to visualize the layouts which I used. Look at the following picture:
As you see I use two nested cardviews (inside each other). Moreover, as I use recyclerview for sub-messages, I have scrolling behavior. When I scroll sub-messages, the onBindViewHolder method of recyclerview is called. To retrieve each image, I call an API in onBindViewHolder method.
Now the problem arises when I want scroll the sub-messages. It seems that when the API is called something goes wrong. Although the image is retrieved successfully from API, it jumps down and up screen.
For example, If I scroll last message's sub-messages to the right. It ends up with the following result:
This is the onBindViewHolder() method of sub messages adapted, for working with the images, I use Glide android library:
public class SubMenuAdapter extends RecyclerView.Adapter<SubMenuAdapter.ViewHolder> {
.
.
.
public void onBindViewHolder(ViewHolder holder, int position) {
SubMessage subMessage = subMessageList.get(position);
final long mediaFileId = subMessage.getMediaFileId();
holder.ivSubmessageImage.setImageDrawable(null);
if (mediaFileId != 0) {
holder.ivSubmessageImage.setImageResource(R.mipmap.ic_launcher);
holder.ivSubmessageImage.setMinimumHeight(100);
holder.ivSubmessageImage.setMinimumWidth(200);
Glide.with(context).load(Config.FILE_SERVER_URL+"api/file/" + mediaFileId + "/thumb")
.into(holder.ivSubmessageImage);
holder.ivSubmessageImage.setVisibility(View.VISIBLE);
} else {
holder.ivSubmessageImage.setImageBitmap(null);
holder.ivSubmessageImage.setVisibility(View.INVISIBLE);
}
...
}
.
.
.
}
This is the layouts which I've created (at the moment):
channel_log_list.xml (msgCardview)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
app:cardCornerRadius="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyCardViewStyle"
android:layout_marginTop="3dp"
android:padding="2dp"
android:layout_marginBottom="1dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_marginRight="23dp"
app:cardPreventCornerOverlap="false"
android:layout_marginLeft="23dp"
app:cardBackgroundColor="#color/chatMsgBg"
android:id="#+id/channelLogCard"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="#+id/ivMessageImage"/>
<com.example.TextViewPlus
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="messages"
app:customFont="#string/irs"
android:id="#+id/channelLogAppletTitle"
android:textSize="#dimen/content_text_size_small2"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/lstSubMessages"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
sub_message_list_item.xml (subMsgCardview)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
app:cardCornerRadius="3dp"
android:layout_height="300dp"
style="#style/SubMessageCardViewStyle"
android:id="#+id/SubMessageCard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layoutDirection="rtl">
<ImageView
android:layout_width="match_parent"
android:layout_height="110dp"
android:id="#+id/ivSubMessageImage"
android:visibility="invisible"/>
<com.example.TextViewPlus
android:padding="5dp"
android:layout_marginTop="10dp"
android:id="#+id/tvText"
app:customFont="#string/irs"
android:textSize="#dimen/content_text_size_small1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
make your Cardview android:focusableInTouchMode="true" or make that layout which you want to focus
So I have a toolbar and it is the parent of a vertical linear layout containing two text views. When I run the activity without the up button everything looks fine. However, when I add the up button it appears to push the linear layout to the right. It seems like there's an implicit horizontal linear layout containing the up button and my own vertical linear layout.
If I cannot prevent the up button from doing this is there a way to set up the up button instead of doing it programmatically?
Sidenote: The reason I'm not using the ActionBar.SetTitle/SetSubtitle functions is because I can't style them how I want. However these functions don't have the same issue with the spacing.
Here's my activity code in my onCreate method. I've also added the relevant import statements:
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
//Get tool bar
Toolbar toolbar = (Toolbar) findViewById(R.id.thickToolBar);
setSupportActionBar(toolbar);
final ActionBar supportActionBar = getSupportActionBar();
supportActionBar.hide();
supportActionBar.setDisplayHomeAsUpEnabled(true); //If I comment out this line, things display normally. But ofc no up button
final TextView actionBarTitle = (TextView) findViewById(R.id.toolbar_title);
final TextView actionBarSubtitle = (TextView) findViewById(R.id.toolbar_subtitle);
supportActionBar.setDisplayShowTitleEnabled(false);
actionBarSubtitle.setText(query);
Here's my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:orientation="vertical"
android:padding="0dp"
tools:context="com.kreyoldict.kreyoldict.DefinitionActivity">
<RelativeLayout
android:id="#+id/progressBarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ProgressBar
android:id="#+id/progressBarDefinitionActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/thickToolBar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="0dp"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:padding="0dp"
android:textAlignment="center"
android:theme="#style/AppActionBar"
android:visibility="visible"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_marginRight="0dp"
android:orientation="vertical"
android:padding="0dp">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Translated Word"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/toolbar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Original Text"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/white"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<ScrollView
android:id="#+id/scrollViewDefinitionActivity"
android:layout_width="match_parent"
android:layout_height="402dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewWordDefinition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="#string/tv_detail_activity_word_definition"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here's a picture of my
[component tree][1] never mind, I need more rep to post 3+ links.
Here's a pic of the app with the up button and layout bounds:
up button shenanigans
It's kinda hard to see because my toolbar is red, but it seems like the linear layout begins where the up button ends.
Here's a picture with no up button and layout bounds
Add this attributes to toolbar
<android.support.v7.widget.Toolbar
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"/>
I want to display a text over an Image View. I do it like Alesqui suggested here:
Android Text over image
The preview in Android studio looks fine:
But my actual result looks like this with the text unwantedly above:
I have to add the following XML dynamically to a LinearLayout during the execution:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
I add it the following way:
LinearLayout ll = (LinearLayout) findViewById(R.id.layout_story_covers);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < stories.size(); i++)
{
// add imageView
RelativeLayout coverLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_cover, null);
ImageView imageView =(ImageView) coverLayout.findViewById(R.id.imageViewCover);
TextView textView = (TextView) coverLayout.findViewById(R.id.textViewCover);
textView.setText(stories.get(i).title);
imageLoader.displayImage(stories.get(i).cover.fileUrl, imageView);
ll.addView(coverLayout);
}
This must have something to do with that parent LinearLayout. What is the proper way to do get the result?
Try this instead:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Tips:
Romain guy said: "I didn’t say don’t use RelativeLayout, you just must be aware of its (possible) impact on performance if used at the top of the tree." Much better to use a simple FrameLayout if you can.
Be aware of the scaleType property in ImageView's
If you want the ImageView to be the same size as the parent then you need to set it as match_parent height and width. If you use wrap content it will depend on the size of the bitmap and the resolution.
Consider using match_parent instead of the deprecated fill_parent
I would prefer Picasso for bitmaps management.
-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/myImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/myImageSouce"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/spacing_medium"
android:layout_gravity="center"
tools:text="Test"/>
</FrameLayout>