get parent's view from a layout - android

I have a FragmentActivity with this layout:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/menulabel"
android:textSize="24sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:layout_below="#+id/textView1"
android:hint="#string/search_title_hint" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spinnersearch"
android:layout_below="#+id/editText1" />
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2" >
<Spinner
android:id="#+id/spinner_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="#string/spinnersearch" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset_search" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonnewcat"
android:layout_below="#+id/layout1" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttondelcat"
android:layout_below="#+id/button2" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttoneditcat"
android:layout_below="#+id/button3" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonnewtext"
android:layout_below="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttondeltext"
android:layout_below="#+id/button5" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<side.menu.scroll.ScrollerLinearLayout
android:id="#+id/menu_content_side_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/ab_solid_example"
android:orientation="horizontal" >
<ImageView
android:id="#+id/main_tmp_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/actionbar_background"
android:src="#drawable/download" />
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_groud" />
</side.menu.scroll.ScrollerLinearLayout>
</RelativeLayout>
and the ScrollerLinearLayout class is something like this:
public class ScrollerLinearLayout extends LinearLayout {
// code of the class
}
How to access from ScrollerLinearLayout to the parent layout to get - for example - the editetxt? FragmentActivity and ScrollerLineraLayout are in different packages of the same projects.
I tried to use the getParent() function in this way within the ScrollerLinearLayout but it doesn't work.
RelativeLayout r = (RelativeLayout) this.getParent().getParent();
int w = r.findViewById(R.id.editText1).getLayoutParams().width;
Someone help me? Thanks!

The getParent method returns a ViewParent, not a View. You need to cast the first call to getParent() also:
RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent();
As stated in the comments by the OP, this is causing a NPE. To debug, split this up into multiple parts:
ViewParent parent = this.getParent();
RelativeLayout r;
if (parent == null) {
Log.d("TEST", "this.getParent() is null");
}
else {
if (parent instanceof ViewGroup) {
ViewParent grandparent = ((ViewGroup) parent).getParent();
if (grandparent == null) {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null");
}
else {
if (parent instanceof RelativeLayout) {
r = (RelativeLayout) grandparent;
}
else {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout");
}
}
}
else {
Log.d("TEST", "this.getParent() is not a ViewGroup");
}
}
//now r is set to the desired RelativeLayout.

If you are trying to find a View from your Fragment then try doing it like this:
int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width;

You can get ANY view by using the code below
view.rootView.findViewById(R.id.*name_of_the_view*)
EDIT: This works on Kotlin. In Java, you may need to do something like this=
this.getCurrentFocus().getRootView().findViewById(R.id.*name_of_the_view*);
I learned getCurrentFocus() function from: #JFreeman 's answer

The RelativeLayout (i.e. the ViewParent) should have a resource Id defined in the layout file (for example, android:id=#+id/myParentViewId). If you don't do that, the call to getId will return null. Look at this answer for more info.

Check my answer here
The use of Layout Inspector tool can be very convenient when you have a complex view or you are using a third party library where you can't add an id to a view

parent as View
fun View.printParentSize() {
log("parentWidth = ${(parent as View).sizeW}")
log("parentHeight = ${(parent as View).sizeH}")
}

This also works:
this.getCurrentFocus()
It gets the view so I can use it.

Related

FrameLayout with two includes only set correctly the first one. Why?

I have a ScollView Layout with a FrameLayout that includes two different layouts. If some condition is met, I set one or the other as visible.
dialog_interface_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/landing_login_view"/>
<include layout="#layout/mobile_login_view"/>
</FrameLayout>
</ScrollView>
My class:
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.INVISIBLE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.INVISIBLE);
}
Inside theese layouts that was set on the include, I have another include:
landing_login_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="#+id/landing_root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:paddingLeft="#dimen/twenty_eight_dp"
android:paddingRight="#dimen/twenty_eight_dp"
android:orientation="vertical">
...
<include layout="#layout/sms_login"/>
...
</LinearLayout>
And mobile_login_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:id="#+id/mobile_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_white"
android:paddingLeft="#dimen/twenty_eight_dp"
android:paddingRight="#dimen/twenty_eight_dp"
android:orientation="vertical">
...
<include layout="#layout/sms_login"/>
</LinearLayout>
This is my sms_login.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/btn_white_enabled">
<br.com.fs.fslogin.ui.support.views.GothamEditText
android:id="#+id/et_phone_number"
style="#style/AppTheme.RectangularEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:digits="0123456789"
android:gravity="center"
android:hint="#string/hint_enter_phone"
android:inputType="number|none"
android:maxLength="11"
android:textColorHint="#color/task_done_color"
android:textSize="#dimen/sixteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<FrameLayout
android:id="#+id/dialog_error_phone"
android:layout_width="match_parent"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/bg_warning_phone"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/eight_dp"
android:layout_marginStart="#dimen/eight_dp"
android:contentDescription="#null"
android:src="#drawable/ic_error_phone_red" />
<br.com.fs.fslogin.ui.support.views.GothamTextView
android:id="#+id/error_phone_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="#dimen/two_sp"
android:textColor="#color/error_login_phone_color"
android:textSize="#dimen/fourteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<FrameLayout
android:id="#+id/btn_sms_login"
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:background="#drawable/btn_login_vivo_purple">
<br.com.fs.fslogin.ui.support.views.GothamButton
style="#style/AppTheme.RectangularButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#null"
android:clickable="false"
android:gravity="center"
android:text="#string/btn_login"
android:textColor="#android:color/white"
android:textSize="#dimen/eighteen_sp"
font:name="#string/font_gotham_bold" />
</FrameLayout>
</LinearLayout>
Programmatically, I set some behaviors, such as a mask for the phone field etc.
But these behaviors that are programmatically configured in sms_login.xml will only occur if the layout is set first in my dialog_interface_login.xml. I have the same include, with the same information, but only works on the first one that is defined at ContainerLayout. Even the onclick event does not trigger.
Do you have any idea why? Is it possible to define two different includes?
----- Edited -----
Including some actions done
v.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (phoneWatcher.isPhoneNumberValid()) {
String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
onSuccessClick.OnSuccess(phoneNumber);
LoginInterfaceVivoSyncDialog.this.dismiss();
}
}
});
You should use Visibility.GONE instead of INVISIBLE
You can directly assign id to include block why you made two extra different layouts to give just an id.
I was shocked to see #dimen/twelve_dp (really shocked). What is use of dimens.xml if you are writing twelve_dp there. You can directly write 12dp. It should be generic like space_small or space_large or text_size_small.
Also removed some extra wrapping layouts from your xml.
dialog_interface_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/landing_root_view"
layout="#layout/sms_login" />
<include
android:id="#+id/mobile_root_view"
layout="#layout/sms_login" />
</FrameLayout>
</ScrollView>
and
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.GONE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.GONE);
}
sms_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_white"
android:orientation="vertical">
<br.com.fs.fslogin.ui.support.views.GothamEditText
android:id="#+id/et_phone_number"
style="#style/AppTheme.RectangularEditText"
android:layout_width="match_parent"
android:layout_height="#dimen/fifty_six_dp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/btn_white_enabled"
android:digits="0123456789"
android:gravity="center"
android:hint="#string/hint_enter_phone"
android:inputType="number|none"
android:maxLength="11"
android:textColorHint="#color/task_done_color"
android:textSize="#dimen/sixteen_sp"
font:name="#string/font_gotham_medium" />
<FrameLayout
android:id="#+id/dialog_error_phone"
android:layout_width="match_parent"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/twelve_dp"
android:background="#drawable/bg_warning_phone"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/eight_dp"
android:layout_marginStart="#dimen/eight_dp"
android:contentDescription="#null"
android:src="#drawable/ic_error_phone_red" />
<br.com.fs.fslogin.ui.support.views.GothamTextView
android:id="#+id/error_phone_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="#dimen/two_sp"
android:textColor="#color/error_login_phone_color"
android:textSize="#dimen/fourteen_sp"
font:name="#string/font_gotham_medium" />
</FrameLayout>
<br.com.fs.fslogin.ui.support.views.GothamButton
android:id="#+id/btn_sms_login"
style="#style/AppTheme.RectangularButton"
android:layout_width="wrap_content"
android:layout_height="#dimen/fifty_six_dp"
android:layout_gravity="center"
android:background="#drawable/btn_login_vivo_purple"
android:clickable="false"
android:gravity="center"
android:text="#string/btn_login"
android:textColor="#android:color/white"
android:textSize="#dimen/eighteen_sp"
font:name="#string/font_gotham_bold" />
</LinearLayout>
Do you have any idea why?
This happens because findViewById(R.id.xyz) returns the first View which has the requested attribute android:id="#+id/R.id.xyz".
Quoting from the documentation on View:
Finds the first descendant view with the given ID, the view itself if the ID matches getId(), or null if the ID is invalid (< 0) or there is no matching view in the hierarchy.
To access the correct View, you'll have to use some property which makes it unique. In your case, for example the value R.id.et_phone_number can be found twice in the FrameLayout. But if you write
View myView = mobileLayout.findViewById(R.id.et_phone_number);
then there will be just one child View of mobileLayout with the requested id.
Your code snippet can be changed as follows:
final View v = inflater.inflate(R.layout.dialog_interface_login, container, false);
View mobileLayout = v.findViewById(R.id.mobile_root_view);
View landingLayout = v.findViewById(R.id.landing_root_view);
if (landingLogin) {
mobileLayout.setVisibility(View.INVISIBLE);
landingLayout.setVisibility(View.VISIBLE);
} else {
mobileLayout.setVisibility(View.VISIBLE);
landingLayout.setVisibility(View.INVISIBLE);
}
setupViewGroup(mobileLayout);
setupViewGroup(landingLayout);
The method setupViewGroup(View layout) should look like this:
private void setupViewGroup(View layout)
{
final EditText etPhoneNumber = layout.findViewById(R.id.et_phone_number);
layout.findViewById(R.id.btn_sms_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (phoneWatcher.isPhoneNumberValid()) {
String phoneNumber = etPhoneNumber.getText().toString().replaceAll("\\D+", "");
// ...
}
}
});
}
By calling findViewById() on layout you will get the desired child Views of the ViewGroups which were added by using <include .../>

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

how to overlap a layout with image view and text over the other layout as a stack/pile

I am in a weird situation in my existing project. In my project i want to inflate one layout over the other as a stack dynamically. At present in my project i managed to show the layouts one below the other vertically by appending them, this approach has been chosen by me because even though i am trying to show it as stack only one image is appearing at the front during runtime instead of a bunch. In order to understand my problem please go through the following image links and code
desired result
acquired_result_when_tried_to_acheive_desired_result
approached way
Parent layout:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imagegallery"
android:orientation="vertical"></LinearLayout>
child layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/relativeImage"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/rl1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp" >
<RelativeLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:orientation="vertical" >
<Button
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/close" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/right" />
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true"
android:background="#drawable/deelpizza"
android:paddingBottom="15dp"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:scaleType="fitCenter" />
<RelativeLayout
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/image"
android:background="#drawable/text_bg" >
<ImageView
android:id="#+id/imageprew"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/icon_3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageprew"
android:text="This is an exclusive Deel good for today only clime this deel before its close!"
android:textColor="#FFFFFF" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_below="#+id/imageprew" >
</RelativeLayout>
</RelativeLayout>
<!--
<Button
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:background="#drawable/text_bg"
android:layout_alignTop="#+id/image"
/>
-->
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeImage"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/rectangle" >
<TextView
android:id="#+id/locationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/descText"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="woodfire pizza, Los Gatos CA\nLimited time offer, redeemable today only."
android:textColor="#000000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/locationText" />
<TextView
android:id="#+id/descText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="1 large, 2 topping pizza"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="$15"
android:textColor="#FF0000"
android:textSize="25dp" />
</RelativeLayout>
JavaCode:
private void addImagesToThegallery() {
LinearLayout imageGallery = (LinearLayout)findViewById(R.id.imagegallery);
//imageGallery.setPadding(0, 0, 0, 30);
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List views=new ArrayList();
View view;
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Please go through my code and let me know where am i going wrong and let me know if i am unclear anywhere
Change the margin top to make views come down a bit
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
LinearLayout.LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(left,5*j, right, bottom);
view.setLayoutParams(lp);
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Checkout following
Github : Material Recent Layout
Usage
RecentsList recents = (RecentsList) findViewById(R.id.recents);
recents.setAdapter(new RecentsAdapter() {
#Override
public String getTitle(int position) {
return "Item "+position;
}
#Override
public View getView(int position) {
ImageView iv =new ImageView(RecentsActivity.this);
iv.setImageResource(R.drawable.mazda);
iv.setBackgroundColor(0xffffffff);
return iv;
}
#Override
public Drawable getIcon(int position) {
return getResources().getDrawable(R.mipmap.ic_launcher);
}
#Override
public int getHeaderColor(int position) {
return colors[random.nextInt(colors.length)];
}
#Override
public int getCount() {
return 10;
}
}
Note
You need to modify as per your requirement.
Try FrameLayout in your parent layout. Set different margins in child views to achive an effect of stack.
Child views are drawn in a stack, with the most recently added child on top
check out : github.com/kikoso/Swipeable-Cards
As per your response :
hi thank you for your response ur response actually worked. But now i am dealing with another situation where-in i would like to update details based on swipe for eg: if a card is swiped right it should be gestured as like where as if a card is swiped left it should be gestured as dislike so how to actually code these swipe gestures can you please help me out
#ashwin for that you should have a like and dislike drawable or a custom view.
card.setOnCardDimissedListener(new CardModel.OnCardDismissedListener() {
#Override
public void onLike() {
Log.d("Swipeable Card", "I liked it");
}
#Override
public void onDislike() {
Log.d("Swipeable Card", "I did not liked it");
}
});
Use above snippet to show or hide the view of like or dislike in respective methods that is onLike() and onDislike().
Edit 1:
I figured out the problem what you are facing, If you are simply implementing the Sample Code. Then it clearly shows that CardContainer has SimpleCardStackAdapter as adapter and CardModel are added to it using new operator. Except the last element of CardModel that is added separately and cardModel Listener is applied on last element only. If you want to apply it on all elements. Then you should have to add the elements to SimpleCardStackAdapter by ArrayList of CardModel.

How to switch between 2 TextViews places on the layout?

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?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="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?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="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.

WRAP_CONTENT not working after dynamically adding views

I'm trying to create a fragment that lays out a series of custom views dynamically. The main content for this layout is a RelativeLayout nested in a LinearLayout (to center it horizontally), nested in a ScrollView.
The RelativeLayout has a few TextViews and a 9 patch ImageView that is meant to scale with the dynamically added custom views. However, the image (achievements_bgImageView below) is ending up as the size of the screen, and is not respecting the size of its parent RelativeLayout even after I've added the appropriate amount of custom views. The image scales fine when I manually set the size of achievements_mainLayout (see the commented out lines below), but does nothing if I try to let that RelativeLayout's wrap_content handle its own sizing.
The ScrollView IS respecting the size of the RelativeLayout, as all the content is present, it's simply the imageView that isn't stretching to match the content at this point.
Any help would be appreciated... My manual calculations don't seem to be good enough to account for different devices, despite the fact I'm accounting for screen density and I'm manually forcing the RelativeLayout to a constant width.
It's worth noting that the measured size of the RelativeLayout is always equal to the height of the screen, regardless of whether or not the sum of its content is greater or less than that height. So, essentially, WRAP_CONTENT is simply not doing what it's supposed to be doing. I have nothing referencing any edge of of the RelativeLayout, so circular dependencies shouldn't be a problem.
fragment_achievements.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:id="#+id/achievements_mainLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/achievements_bgImageView"
android:src="#drawable/bkg_achievements9"
android:adjustViewBounds="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name Field"
android:id="#+id/achievements_nameTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="30dp"/>
<ImageView
android:layout_width="52dp"
android:layout_height="52dp"
android:id="#+id/achievements_avatarImageView"
android:layout_below="#+id/achievements_nameTextView"
android:layout_alignLeft="#+id/achievements_nameTextView"
android:src="#drawable/achieve_avatar"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Top Moment:"
android:id="#+id/textView2"
android:layout_alignBottom="#+id/achievements_avatarImageView"
android:layout_toRightOf="#+id/achievements_avatarImageView"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:textSize="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Me Overall:"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignLeft="#+id/textView2"
android:layout_marginTop="16dp"
android:textSize="12dp"/>
<TextView
android:layout_width="52dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="153"
android:id="#+id/achievements_totalPointsTextView"
android:gravity="center"
android:layout_alignTop="#+id/achievements_avatarImageView"
android:layout_alignRight="#+id/achievements_bgImageView"
android:layout_alignEnd="#+id/achievements_bgImageView"
android:layout_marginRight="31dp"
android:textColor="#f7a033"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Moment"
android:id="#+id/achievements_topMomentTextView"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_marginLeft="5dp"
android:textSize="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="153"
android:id="#+id/achievements_overallTextView"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_marginLeft="5dp"
android:textSize="12dp"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
AchievementFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = null;
fragmentView = inflater.inflate(R.layout.fragment_achievements, container, false);
ImageView avatarImageView = (ImageView)fragmentView.findViewById(R.id.achievements_avatarImageView);
...
// Basic Achievement List Setup
RelativeLayout mainLayout = (RelativeLayout)fragmentView.findViewById(R.id.achievements_mainLayout);
AchievementRow currentRow = null;
List achievementTypeList = CampaignManager.sharedManager().sortedAchievementTypeList();
int achievementCount = achievementTypeList.size();
for (int i = 0; i < achievementCount; i++) {
AchievementType achievementType = (AchievementType)achievementTypeList.get(i);
// Every third achievement creates a new row.
if ((i % 3) == 0) {
AchievementRow row = (AchievementRow)inflater.inflate(R.layout.widget_achievementrow, null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (currentRow == null) {
layoutParams.addRule(RelativeLayout.BELOW, avatarImageView.getId());
layoutParams.setMargins(10, 70, 10, 0);
} else {
layoutParams.addRule(RelativeLayout.BELOW, currentRow.getId());
layoutParams.setMargins(10, 10, 10, 0);
}
layoutParams.addRule(RelativeLayout.ALIGN_LEFT, backgroundImageView.getId());
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, backgroundImageView.getId());
row.setLayoutParams(layoutParams);
row.setId(i+1);
mainLayout.addView(row);
currentRow = row;
}
// Now setup the Button
AchievementButton achievementButton = currentRow.buttonForIndex(i % 3);
achievementButton.achievementType = achievementType;
achievementButton.setOnClickListener(achievementButtonListener);
achievementButton.setVisibility(View.VISIBLE);
CacheManager.sharedManager().fetchAchievementThumbnail(getActivity(), achievementButton, achievementType);
}
// This is the manual scaling of mainLayout
// float scale = getResources().getDisplayMetrics().density;
// float headerHeight = scale * 150.0f;
// float rowHeight = scale * 78.0f;
// ViewGroup.LayoutParams mainLayoutParams = mainLayout.getLayoutParams();
// mainLayoutParams.height = (int)(headerHeight + (Math.ceil(achievementCount / 3.0) * rowHeight));
return fragmentView;
}
Try calling requestLayout on the children.
I recently had a similar problem and was similarly frustrated that things like invalidate and requestLayout seemed to do nothing. What I didn't understand is that requestLayout doesn't propagate down to its children; it propagates up to its parents. To re-measure something that was previously measured, I had to call requestLayout on the View that changed rather than the View I actually wanted to resize.
Android does NOT refresh layout of views with "wrap_content" once it has been displayed.
So if you add a child view, or modify the content dynamically, you're screwed.
I do agree that this is a nightmare and a real flaw in Android UI!
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content"
The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Enjoy!
A simple way to update the size of a View with WRAP_CONTENT is change the visibility to GONE and back to the old visibility.
int visibility = view.getVisibility();
view.setVisibility(View.GONE);
view.setVisibility(visibility);
TESTED ON ANDROID JELLY BEAN IN 2014
MAY NOT WORK ON NEWER ANDROID VERSIONS
Ok, I solved this by manually measuring the RelativeLayout immediately after adding all the views and setting the mainLayoutParams height explicitly. I wish I was smarter and knew why it wasn't automatically doing this correctly in the first place, but oh well.
...
mainLayout.measure(0, 0);
ViewGroup.LayoutParams mainLayoutParams = mainLayout.getLayoutParams();
mainLayoutParams.height = mainLayout.getMeasuredHeight() + 10;
...
I encountered same issue when working with LinearLayout which has wrap_content and one child as TextView match_parent.
To fix this I did this:
Remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sounds stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent
You are getting this problem because you set your layout first and then add its content dynamically.
You are telling the layout to wrap to the content that is not yet their. Try using your layout inflater after you have grabbed your content
You should use NestedScrollView Instead of simple scrollview.
here is my sample Activity Layout code
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0ECE6"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabIndicatorHeight="6dp"
android:layout_marginTop="-10dp"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<wsit.rentguru.utility.CustomViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
here is code for custom viewpager
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
setup function for viewpager in the activity
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new SampleFragment(), " ");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
here is the sample SampleFragment layout code
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F0ECE6"
android:fillViewport="true"
android:scrollbars="vertical"
android:animateLayoutChanges="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0ECE6"
android:focusableInTouchMode="true"
>
<Spinner
android:id="#+id/product_category"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/edittext_rectangle_box"
android:gravity="center|left"
android:textSize="14sp"
android:paddingLeft="10dp"
android:drawableRight="#drawable/ic_down_arrow"
/>
<Spinner
android:id="#+id/product_sub_category"
android:layout_below="#+id/product_category"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center|left"
android:visibility="gone"
android:paddingLeft="10dp"
android:background="#android:color/white"
android:drawableRight="#drawable/ic_down_arrow"
/>
<EditText
android:id="#+id/product_title"
android:layout_below="#+id/product_sub_category"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:background="#android:color/white"
android:hint="PRODUCT TITLE"
android:singleLine="true"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:textColorHint="#000000"
android:layout_height="40dp" />
<LinearLayout
android:id="#+id/availability_layout"
android:layout_below="#+id/product_title"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/availability"
android:textStyle="bold"
android:paddingBottom="10dp"
android:textSize="14sp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
android:id="#+id/from"
android:background="#android:color/white"
android:hint="FROM"
android:layout_weight="1"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:textStyle="normal"
android:textColorHint="#000000"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"/>
<Button
android:id="#+id/to"
android:background="#android:color/white"
android:hint="TO"
android:layout_weight="1"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:textColorHint="#000000"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/product_location_layout"
android:layout_below="#+id/availability_layout"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/product_location"
android:textStyle="bold"
android:paddingBottom="10dp"
android:textSize="14sp"
/>
<Spinner
android:id="#+id/state_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/edittext_rectangle_box"
android:gravity="center|left"
android:textSize="14sp"
android:layout_marginBottom="10dp"
android:drawableRight="#drawable/ic_down_arrow"
android:paddingLeft="10dp"
/>
<EditText
android:id="#+id/area"
android:background="#android:color/white"
android:hint="Area"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:textColorHint="#000000"
android:layout_width="match_parent"
android:layout_height="40dp"
android:singleLine="true"
android:imeOptions="actionNext"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<EditText
android:id="#+id/zipCode"
android:background="#android:color/white"
android:hint="Zip Code"
android:layout_weight="1"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:textColorHint="#000000"
android:layout_width="0dp"
android:layout_height="40dp"
android:singleLine="true"
android:imeOptions="actionNext"
android:layout_marginRight="10dp"/>
<EditText
android:id="#+id/city"
android:background="#android:color/white"
android:hint="City"
android:layout_weight="1"
android:gravity="center|left"
android:padding="10dp"
android:textSize="14sp"
android:singleLine="true"
android:imeOptions="actionDone"
android:textColorHint="#000000"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/tab1_next"
android:layout_width="150dp"
android:layout_height="40dp"
android:text="NEXT"
android:layout_below="#+id/product_location_layout"
android:layout_margin="20dp"
android:layout_alignParentRight="true"
android:background="#color/next_button"
android:textColor="#android:color/white"
android:layout_marginBottom="20dp"
/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Set the layout parameters again(width and height), right after adding the view. This worked for me.
if the parent View is a FrameLayout, then do something like this:
ImageView view = (ImageView) LayoutInflater.from(activity).inflate(R.layout.image_object_view, null);
imageObjectsHolder.addView(view);
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(param);
My problem was fixed by seting layout_width to some specific dp.
So changing from "wrap content" or "match parent"
to
android:layout_width="300dp"
will fix it, but i know it's not solution for all cases. But maybe you have some parent width, so you can apply the width to the textview.
height leave with wrap_content, and it will work.

Categories

Resources