relativelayout wrap_content not working - android

this is my xml 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:id="#+id/tt">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trashParent"
android:visibility="visible"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<LinearLayout
android:background="#drawable/bgtrash"
android:layout_width="match_parent"
android:layout_height="140dp"
android:baselineAligned="false"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/parentTrash"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<
</LinearLayout>
as we can see the parent View with id android:id="#+id/tt"
its height and width its wrap_content i know if some child have android:layout_alignParentBottom="true" so the wrap_conent not work its will be like match_parent
but now i do this in java i have to method will hide and show the child
private void hide(){
trashParent.setVisibility(View.GONE);
parentTrash.setVisibility(View.GONE);
}
private void show(){
trashParent.setVisibility(View.VISIBLE);
parentTrash.setVisibility(View.VISIBLE);
}
now when i hide the child and make it wrap_content its still match parent this the run code
hide();
RelativeLayout.LayoutParams chatHeadParentParamst = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tt.setLayoutParams(chatHeadParentParamst);
tt.setBackgroundColor(Color.YELLOW);
change tt height to wrap_content because its be match parent some time
but after change to wrap_conent is still like match_parent
i think the child still not Gone and i set it to wrap_conent
because when i use delay its work this the delay
hide();
Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = new Runnable() {
#Override
public void run() {
hide(); // i dont no must i call it again to work
RelativeLayout.LayoutParams chatHeadParentParamst = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tt.setLayoutParams(chatHeadParentParamst);
tt.setBackgroundColor(Color.YELLOW);
Log.d("called now delay","wwwwwwwww");
}
};
handler.postDelayed(runnable, 1100);
with delay now its size chnage to wrap_conent without delay it be same like match_parent but all child visibility was gone any solotio without delay i cant use LinerLayout i must use RaltiveLayout becuase i need child one over one

Related

Text jumps right to center when keyboard toggled

I have a RecycleView in which I visualize some text. The text is entered through an EditText view in the bottom. The RecycleView contains a TextView which is has the text centered.
I have also a function that detects if the keyboard is visualised. When the keyboard is visualised, then some layout parameters are changed so that the EditText looks good. I also have a smooth scroll to the last position of the RecycleView.
When I reopen the keyboard again, the text is first drawn to the left of the screen. If I swipe, the text is re-aligned to center. That is very disturbing and I always want the text to be centered. I probably miss something when changing the parameters of the layouts.
KeyBoard openedListener:
// Function to see of keyboard is opened or not.
private void detectKeyBoard(){
final ConstraintLayout rootView = findViewById(R.id.activity_root);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
float heightDiff = rootView.getRootView().getHeight() - rootView.getChildAt(0).getHeight();
if (!isRefSet){
refDiff = heightDiff;
isRefSet = true;
}
if (heightDiff > refDiff) {
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
0);
params.weight = wRSmall;
mRecyclerView.setLayoutParams(params);
LinearLayout.LayoutParams eParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
0);
eParams.weight = wESmall;
mLinearInput.setLayoutParams(eParams);
if (mAdapter.getItemCount() > 0) {
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
}
} else {
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
0);
params.weight = wRStart;
mRecyclerView.setLayoutParams(params);
LinearLayout.LayoutParams eParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
0);
eParams.weight = wEStart;
mLinearInput.setLayoutParams(eParams);
if (mAdapter.getItemCount() > 0) {
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
}
}
}
});
}
xml for RecycleView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/activity_root"
tools:context="com.example.erikbylow.autoflash.TranslateActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/translate_linear"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:scrollbars="vertical"
android:layout_gravity="center_horizontal"
android:id="#+id/translate_recycle"
android:background="#android:color/holo_blue_bright" />
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/linear_input"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="0.75"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:id="#+id/source_input"
android:hint="Type text to translate"/>
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_alignRight="#+id/source_input"
android:text="Translate"
android:clickable="true"
android:onClick="startTranslate"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
</LinearLayout>
TextView for Adapter:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:textSize="#dimen/active_text_size"
android:id="#+id/adapter_textview"/>
After swiping it is re-aligned:
EDIT: It did not work to remove android:layout_gravity="center_horizontal" in the recycle xml part.
Further, if the screen is full without the keyboard being opened, the text is moved to the right:
Can you comment the detectKeyBoard() function? Just to check if the same problem exists or not?
Just a guess -
I see that you are creating a new LayoutParams object and assign it to your mRecyclerView and mLinearInput.
As far as I know, this new LayoutParam object will replace what you have set in XML, including layout_gravity, layout_weight, etc.
Try getting existing LayoutParams from mRecyclerView and mLinearInput, and modify their values instead.
Cannot advise much because I do not really understand the reason behind resetting layout params in your case.

View going out of screen in dynamic image views inside horizontal linear layout

I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen.
See what is happening:
I am using layout_gravity to achieve this
Without layout_gravity it appears like this
But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. You can see the first uploaded image for understanding the scenario. In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely.
Here's the xml structure
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="#+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java manipulation
#Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
Isn't that what you are trying to achieve by using HorizontalScrollView? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView.
Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that.
This is what worked out for me
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="#+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You can remove the parent LinearLayout for imageViewLayout too if you don't need it. Let me know if you need anymore help!

View.getTop() always returns 0

So View.getTop() is, as per the docs:
Top position of this view relative to its parent.
I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scrollviewtest1.MainActivity">
<LinearLayout
android:id="#+id/scroll_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.scrollviewtest1.MyTextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/large_text" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</ScrollView>
I am checking for getTop() for LinearLayout as I scroll up and down and getTop() always returns 0. Shouldn't it change as it moves up and down relative to its parent?.
How I checked it?
I stored reference to the LinearLayout inside the child TextView and called invalidate() every time there was scrolling:
ScrollView root = ((ScrollView) findViewById(R.id.root));
root.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
textView.invalidate();
}
});
and checked inside the onDraw() call of the child TextView
Try this way :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
int top = view.getTop();
}
}, 300);// delay times in milliseconds
You can set delay time as per your requirement.
To get the LinearLayout scrolled position call myScrollView.getScrollY();
getTop(); returns what you entered as marginTop in your xml layout and it not varies when the thing is scrolled, to know the actual current position you need to take in account getScrollY(); too

Layout weight not working as intended

I have a slight problem getting my layout_weight to work. I am creating a custom bottomBar. But i cant make it to stretch as i want it to.
Bottom Bar View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_bar_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_bar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:orientation="horizontal"/>
</RelativeLayout>
This is the big container i am adding my buttons (items) to.
Bottom Bar Item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</LinearLayout>
This is my items that i add dynamically to the view. But somehow the view is not stretched properly.
But when i hardcode it in. It works. So could it be that layout weight does not work dynamically?
How i add the views (items)
private void updateItems(final List<BottomBarTab> bottomBarTabs) {
if (bottomBarTabs.size() < TABS_COUNT) {
throw new RuntimeException("Not enough buttons for bottomBar");
}
for (int i = 0; i < TABS_COUNT; i++) {
bottomBarTabs.get(i).setOnClickListener(this);
bottomBarTabs.get(i).prepareLayout();
container.addView(bottomBarTabs.get(i));
}
}
Screenshot
LayoutWeight is given to inner components of layout only not on parent Linearlayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:weightSum="2">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEXT"/>
</LinearLayout>
public void prepareLayout() {
View view = inflate(getContext(), R.layout.bottom_bar_item,this);
LinearLayout.LayoutParams params =
new LayoutParams(0,LayoutParams.MATCH_PARENT,LAYOUT_WEIGHT);
view.setLayoutParams(params);
if(backgroundColor != null) {
view.setBackgroundColor(backgroundColor);
}
TextView titleText = (TextView) view.findViewById(R.id.bottom_bar_item_text);
titleText.setText(title);
AppCompatImageView imageView = (AppCompatImageView) view.findViewById(R.id.bottom_bar_item_icon);
imageView.setImageResource(iconResId);
}
I changed in my prepareLayout function and put new layoutParams. Because somehow after inflation. The view ignores the weight that was set to it. So i had to force it by code. Maybe this is a android bug?

Animation and layout issue with my new application?

I am trying to make a new layout page where I want to put two buttons, and on the above of each button I need to give a frame animation. so on loading the buttons are looking like inside the bubbles. Following is the code I am using to achieve this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/background_full">
<Button android:id="#+id/btnMusic"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:layout_marginLeft="215dp"
android:layout_marginTop="140dp"
android:background="#drawable/icon"/>
<ImageView android:id="#+id/imgMusic"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:layout_marginLeft="170dp"
android:layout_marginTop="100dp"
android:background="#drawable/button_background"/>
<Button android:id="#+id/btnMovies"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:layout_marginLeft="405dp"
android:layout_marginTop="140dp"
android:background="#drawable/icon1"/>
<ImageView android:id="#+id/imgMovies"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
android:layout_marginLeft="360dp"
android:layout_marginTop="100dp"
android:background="#drawable/button_background"/>
</RelativeLayout>
My jav code is like this:
public class BubbleActivity extends Activity {
/** Called when the activity is first created. */
/** Called when the activity is first created. */
Button btnMusic, btnMovies ;
ImageView imgMusic,imgMovies;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
btnMusic = (Button)findViewById(R.id.btnMusic);
btnMovies = (Button)findViewById(R.id.btnMovies);
btnMusic.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent(PixieActivity.this,Splash.class);
startActivity(intent);
}
});
ImageView imgMusic = (ImageView)findViewById(R.id.imgMusic);
imgMusic.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation =(AnimationDrawable) imgMusic.getBackground();
if (frameAnimation.isRunning()) {
frameAnimation.stop();
}
else {
frameAnimation.start();
}
ImageView imgMovies = (ImageView)findViewById(R.id.imgMovies);
imgMovies.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation1 =(AnimationDrawable) imgMovies.getBackground();
if (frameAnimation1.isRunning()) {
frameAnimation1.stop();
}
else {
frameAnimation1.start();
}
}}
But due to the margins the button layout became distracted in different phone resolutions. Is there any other way to achieve the same layout with device resolution independant. Also I want to add the bubble animation to each of the icons i will make in next pages. Please help.
I would suggest not hard-coding the margins, and instead wrap the Buttons and ImageViews in a LinearLayout each, then set the spacing using layout_weight so it is perfectly scalable.
The actual layout choice depends on if you need the button to be exactly 80x80 and the ImageView to be exactly 150x150.
For instance (pseudo-code: obviously many parameters are left out) :
<RelativeLayout>
<LinearLayout id="buttons" >
<LinearLayout layout_width = "0dp" layout_weight = "1"> <!-- 0 width is important! -->
<Button layout_gravity="center" />
</LinearLayout>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<Button layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<LinearLayout id="images" align with #buttons>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<ImageView layout_gravity="center" />
</LinearLayout>
<LinearLayout layout_width = "0dp" layout_weight = "1">
<ImageView layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And just make sure set the LinearLayouts "buttons" and "images" to the same height and full width so that the buttons and images overlap. More about layout_weight: What does android:layout_weight mean?
If you do not need the Buttons and ImageViews to be an exact size, think about sizing them by weight as well. Then no matter what screen you are on, if you tell a button to take up 1/4 of it through layout_weight, it will never be distorted

Categories

Resources