What's Happening
I am writing a PopupWindow containing two TextViews, where the second TextView should be centered vertically in the popup, and the first TextView should be directly above it.
The problem is that the RelativeLayout seems to be treating the two TextViews as a single element, and vertically centering the middle of them. I want the lower TextView to be the one centered, though, and the upper one to be resting just above it (hence the android:layout_above="#id/first_name").
XML Layout
Note the apparently unnecessary LinearLayout there because the RelativeLayout refused to completely vertically fill the screen (the PopupWindow is using ViewGroup.LayoutParams.MATCH_PARENT).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TextView
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:text="Christopher" />
<TextView
android:id="#+id/lbl_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/first_name"
android:gravity="center"
android:singleLine="true"
android:text="#string/hello" />
</RelativeLayout>
</LinearLayout>
Java Activity
LayoutInflater inflater = LayoutInflater.from(this);
final View popupView = inflater.inflate(R.layout.<fragment name>,
<parent object>,
false);
final PopupWindow pwindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
true);
pwindow.setTouchable(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}, 100L);
As Sushil said, you can probably remove your LinearLayout completely. The following fix should work; if it does, try removing the linear layout as well.
The issue here is the android:gravity="center" on the RelativeLayout. If you remove that, you can get the placement you desire:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
You can remove your Linearlayout simply. Right now your textview are child views of Relativelayout which is a child view to LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TextView
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:text="Christopher" />
<TextView
android:id="#+id/lbl_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/first_name"
android:gravity="center"
android:singleLine="true"
android:text="#string/hello" />
</RelativeLayout>
Use this xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="90dp">
<TextView
android:id="#+id/inv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/invfam"
android:textColor="#f5f5f5"
android:textSize="20dp" />
<TextView
android:id="#+id/promo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/inv"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="Enjoy an additional 15 AED off your first video consultation. Sign up for a Health at Hand account and you will be credited 30 AED as a new user plus an extra 15 AED! #feel better today"
android:textColor="#f5f5f5"
android:textSize="14dp" />
</RelativeLayout>
Related
I've a layout like design 1 and I want to align cancel button to bottom of the dialog always. recyclerview1 & recyclerview2 have different heights due to display content. if the both recyclerview1 and recyclerview content are small and able to display within screen without scroll it should be like design 2 . If the recyclerviews exceeded the heights dialog should take the device height and align cancel button to bottom of the dialog like design 3.
Current output and final output
Current code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="text1"
android:textSize="10sp" />
<TextView
android:id="#+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/textview1"
android:text="text2"
android:textSize="10sp" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_below="#id/textview2"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutContainer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutContainer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</RelativeLayout>
You will have to create a custom dialog
inside onViewCreated() inflate a layout_bottom_button using gravity bottom, so inflated layout will always be at bottom
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.BOTTOM;
View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
this.getDialog().addContentView(viewM, params);
keep in mind that recyclerView should have bottom margin same as layout_bottom_button layout height, so that recyclerView does not hide below button.
I have this code xml layout that I am trying to implement in a round android wear watch. I want the TextView above the ExpandableListView to scroll with it rather than staying on the top and taking up space.
I tried using ScrollView with it rather than RelativeLayout (ScrollView in RelativeLayout and vice versa as well) but what that does is that the LinearLayout requires a set height for the ExpandableListView to show the list which is not possible as the list dynamically changes (so it can have a random number of group members) and if I use "wrap_content" it only shows one group at a time. Each group has 4 child members as well.
Can someone tell me how to make this layout suitable for me so that the TextView and the ExpandableListAdapter scroll together without having a set height for the ExpandableListAdapter?
round_activity_card_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
tools:deviceIds="wear_round"
android:background="#FF3C00"
android:smoothScrollbar="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewHeader"
android:textSize="17sp"
android:textColor="#color/white"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:paddingRight="5dp"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/round_expandableListView"
android:background="#FF3C00"
android:choiceMode="multipleChoice"
android:groupIndicator="#null"
android:longClickable="true"/>
</LinearLayout>
</ScrollView>
Look into addHeaderView(View v). Create your TextView programmatically or inflate from XML and then add it to your ExpandableListView using that method.
The TextView should then scroll with your ExpandableListView.
Thanks for showing me what to do, Alex. I had to come up with some way to do that which took ages but finally got it right. Thanks for the instructions which really helped me a lot! The code is used is given below:
LinearLayout mLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.expandable_header, expListView, false);
mLayout.setLayoutParams(new AbsListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
expListView.addHeaderView(mLayout);
Also the changed xml files:
expandable_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF3C00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expCardViewHeader"
android:textSize="17sp"
android:textColor="#color/white"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:paddingRight="5dp"
android:paddingBottom="2dp"
android:background="#FF3C00"
android:text="#string/no_results"/>
</LinearLayout>
round_activity_card_view_layout.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="match_parent"
android:layout_height="match_parent"
tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
tools:deviceIds="wear_round"
android:background="#FF3C00"
android:smoothScrollbar="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF3C00"
android:paddingTop="20dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/round_expandableListView"
android:background="#FF3C00"
android:choiceMode="multipleChoice"
android:groupIndicator="#null"
android:longClickable="true"
android:smoothScrollbar="true" />
</LinearLayout>
</RelativeLayout>
This is my xml file. There are 35 xml files like this in my app. Their textview and button numbers are different. Some of them are more than 10 , some of are less than 5. But considering the devices screen, i designed all xml files using scroll view. Also i needed tablelayout and used tablelayout, table row inside it and textviews inside tablerows. When i finished the app everything was ok. Now i added mobile ad banners in xml files and it started to crash.
If i add the banner just before ending of tablelayout , app doesnt crash and ad is displayed at teh end of table but the vision is too bad.Because if there are 3 rows in the xml then the banner takes place after the last textview and this is something like the middle of the page. No matter how many textviews the xml use, i want them to be scrollable and in the end i want the banner take place at the end of the page.
I tried some android:gravity="bottom" or
alignparentbottom="true" things but they didn't work. In my researches i realised that i may need to add some linear or relative layout codes but when i tried them thay dont fit with scrollview and crash. Here is one of the xml files. How should i need to modify this ?
In the images , First one is how it works , the banner takes place where the textview ends , i want the second image , no matter how long the tetviews the banner should be at the bottom of the page.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
// some other buttons or textviews...
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</ScrollView>
If the banner needs to be always shown at the bottom,
you can set the weight of the ScrollView in a LinearLayout to 1.
Example
<?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"
android:background="#drawable/blue">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#drawable/blue">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
// some other buttons or textviews...
</TableRow>
</TableLayout>
</ScrollView>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I can't test the code here because I do not have the Banner component but try wrapping both the TableLayout and the Banner in a LinearLayout.
modified layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
UPDATE
After testing the file locally I realized that crash is being caused by the Banner because a ScrollView can have only one child. I'm surprised that you were able to compile your project with the error in the layout file. So one solution is wrap the TableLayout and Banner in a LinearLayout. See example 1:
example 1
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
After thinking about it, it may be possible to eliminate the TableLayout completely. You can use a LinearLayout to stack items on top of each other. So that would simplify your layout file quite a bit. See example 2.
example 2
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
UPDATE 2
Then your issue must be with your Java file. I have tested the layout file locally and it does render properly. See screenshot below.
UPDATE 3
The solution required updates to both the layout file and the Activity class. The logic is fairly simple. If the number of rows in the table cause the table height to exceed the height of the scrollview, then scrolling is enabled. If scrolling is enabled, the banner ad is appended as child of the table. If scrolling is disabled, the banner ad is inserted as a child of the linearlayout. In order to determine the correct heights it is necessary to subscribe to the OnGlobalLayout event. The following code is a ** working sample ** of what you will need. In order to test both scenarios (scrolling enabled/disabled) just change the value of the ROW_COUNT variable. I used 3 for scrolling disabled and 30 for enabled. You will see the banner placed appropriately.
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e0e0e0">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="TRY" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
TestActivity.java
public class TestActivity extends Activity {
private static String TAG = "TestActivity";
private Activity mActivity;
private LinearLayout mLinearLayout;
private TableLayout mTableLayout;
private RelativeLayout mRelativeLayout;
private static int ROW_COUNT = 30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.d(TAG, "onCreate");
mActivity = this;
mRelativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);
mLinearLayout = (LinearLayout)findViewById(R.id.linearlayout);
mTableLayout = (TableLayout)findViewById(R.id.table);
float density = getResources().getDisplayMetrics().density;
final int padding = (int)Math.floor((density * 20f));
for(int i=0;i<ROW_COUNT;i++){
TextView view = new TextView(mActivity);
view.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
view.setPadding(padding,padding,padding,padding);
view.setText("TableRow");
TableRow row = new TableRow(mActivity);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(view);
mTableLayout.addView(row);
}
ViewTreeObserver observer = mRelativeLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TextView view = (TextView) findViewById(R.id.banner);
if(view == null){
view = new TextView(mActivity);
view.setId(R.id.banner);
view.setBackgroundColor(Color.LTGRAY);
view.setText("BannerAd");
view.setGravity(Gravity.CENTER_HORIZONTAL);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
view.setPadding(0, padding, 0, padding);
if(mTableLayout.getHeight() > mLinearLayout.getHeight()) {
mTableLayout.addView(view);
}else{
mLinearLayout.addView(view);
}
}
}
});
}
}
I can post screenshots if needed, but test this code out and let me know if you have any questions.
This question seems to have been asked a dozen times already bu I haven't been able to deduce the solution from them.
I have a layout called tile.xml that looks like this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
I need to add this one of my views called feed.xml which I've declared as a ScrollView because I want to have multiple objects in it so I can vertically scroll though them. Here's my feed.xml file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:background="#F1F1F1"
android:layout_height="fill_parent" >
</ScrollView>
I'd like to dynamically add my "tile" from tile.xml to my "feed" feed.xml view while changing the text of the TextViews on the fly.
How can I do this? Thanks
The new feed.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
...and the new tile.xml:
<?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:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
First of all, add the 2 TextViews inside a layout.
then, in your code, where you want to dynamically add the TextViews you do the following :
ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scroll.addView(view);
To access the TextViews you have to do the following :
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText("");
Also make sure to give your TextViews different IDs in the XML file. Currently they are both textView1
ScrollView can only have 1 child, meaning the current structure of your "tile" layout can't be added to the ScrollView.
What you need is to add another Layout, let's say LinearLayout to your ScrollView so that it would be the ScrollView's only direct child. To this LinearLayout you can add as many childs as you want.
This can be done by using the LayoutInflater which you've probably encountered in numerous examples.
**TRY THIS**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/tile" />
</LinearLayout>
I'm developing an Android application.
On one activity I have a List with list items. Those list items will have a TextView (I'm using my own layout because probably I will need to add more widgets to it).
This is ListActivity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="469dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp">
</ListView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/orderErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
And this is ListItem xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
</LinearLayout>
And, finally, this is the result:
Why TextView is not centered vertically?
You have "wrap_content" on the layout_height of the TextView = there is no space to center vertically on since the TextView is wrapped tightly.
Try changing the layout_height to "match_parent" or place gravity="center_vertical" attribute in the LinearLayout instead.
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
the answer is really simple: you have to add this to your TextView's XML:
android:gravity="center"
Currently you're not setting the text's position in the TextView but just the View's position.
Best wishes,
Tim
Try this way:
<TextView
android:id="#+id/orderToFillName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity= "center_vertical|center_horizontal"
android:textSize="20dp" />
or
android:gravity="center"