I programmatically add some text view to a linear layout starting from right, so put
<LinearLayout
android:id="#+id/results"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_gravity="right"
android:orientation="horizontal"
android:paddingLeft="12sp"
android:paddingRight="12sp" />
and coded
LinearLayout results= (LinearLayout)findViewById(R.id.results);
TextView res = new TextView(this);
res.setId(resultsCounter );
res.setText(evaluated);
...............
results.addView(res);
and this works fine but I can't add indefinitely new items, so I added an horizontal scroll view
<HorizontalScrollView
android:id="#+id/scrollresults"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:layout_marginRight="12sp"
>
<LinearLayout
android:id="#+id/results"
......
</HorizontalScrollView>
but filling LinearLayout I can scroll to right only, so I lost leftmost items
Related
I have a horizontalscrollview which inside it has a LinearLayout with TextView.
I am setting dynamically String that are longer every time to the TextView (like a calculator), But when the TextView gets wider, I can't scroll to the begining of it(to the left side in my handy), but only to the right side, which is just some empty space which I have no idea how it came.
In general, just can't scroll to one side(left) to see some of the TextView in the layout.
This is the xml of the scrollview:
<HorizontalScrollView
android:id="#+id/Hscrollbar"
android:layout_width="match_parent"
android:layout_height="35dp"
android:foregroundGravity="right"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:id="#+id/logs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:scrollHorizontally="true"
android:text="v"
android:textColor="#ffffff"
android:textSize="30dp" />
</LinearLayout>
</HorizontalScrollView>
Also in my activity, this is what I modified to the HorizontalScrollView:
final HorizontalScrollView Hscrollbar = (HorizontalScrollView)findViewById(R.id.Hscrollbar);
Hscrollbar.post(new Runnable() {
#Override
public void run() {
Hscrollbar.fullScroll(View.FOCUS_RIGHT);
}
});
How can I make it scroll to the left side (which I want to see the whole TextView)?
Remove android:foregroundGravity="right" from HorizontalScrollView.
Make LinearLayout width wrap_content and same for TextView.
Set TextView android:layout_gravity="right"
That might work.
I have a problem with a linear layout inside a scrollview
<ScrollView
android:layout_width="fill_parent"
android:scrollbarStyle="outsideOverlay"
android:layout_height="420px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom">
<TextView
android:text="1st"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
<TextView
android:text="2nd"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
</LinearLayout>
</ScrollView>
The Textview that has text of "1st" shows on the top of the textview that has text of "2nd", is there a way that the "1st" shows on the bottom without changing the textview? because I plan to add textviews programmatically.
Adding views programmatically to a LinearLayout is fairly easy:
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_layout);
TextView view = new TextView(/* Context and other params */);
yourLayout.addView(view);
Rather than invoking new TextView directly, it is recommended to specify an XML for your text view and use a layout inflater:
TextView view = (TextView) LayoutInflater.from(getBaseContext())
.inflate(R.id.your_textview_layout, yourLayout, false);
I want to create the layout to be like what in the diagram
how can I make the listView Scroll independently and make the textView and ImageView stead?
just replace your xml layout like below code:
<?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:background="#android:color/white"
android:padding="5dp"
android:weightSum="2"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:layout_gravity="right"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/yourimg" />
</LinearLayout>
above xml given output below screenshot:
For the Lower Steady Image View you can use : Create a footer view
layout consisting of text that you want to set as footer and then try
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
For Header: The solution that works for me is to create a TableLayout
that has a row with the heading and a row with the list like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/header" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableLayout>
Please make sure that the ListView has #android:id/list as the ID.
OR
Rather google adding header and footer to listview in android that
will help u much better
Sorry for the pseudo code, I can try a better attempt later on, but you need:
RelativeLayout
TextView - align top
ImageView - align bottom
ScrollView - align above ImageView, align below TextView
Create a linear layout with orientation as vertical. Add textview listview and imageview with the weight as you want. Remember to give layout_height as 0dp for all the three components.
Add Header And Footer To the Listview,Like
ListView mList=getListView();
mList.addHeaderView(View v);//Your textView
and
mList.addFooterView(v);//your imageview
how to avoid overlapping of dynamically positioned views?
I have a RelativeLayout , and i am adding views dynamically(at runtime) at particular position(x,y coordinates) but the problem is the views are overlapping.
How to avoid this.
Thanks in advance.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/ll_mainBottom"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
javacode
ll_main = (RelativeLayout) findViewById(R.id.rl_main);
if (views[3].equals("textView")) {
TextView tv_new = new TextView(TenMinActivity.this);
// location
int x = Integer.parseInt(views[12]);
int y = Integer.parseInt(views[13]);
String bgColor = "#" + views[4];
String fgColor = "#" + Views[5];
tv_new.setBackgroundColor(Color.parseColor(bgColor)); // Bg Color
tv_new.setTextColor(Color.parseColor(fgColor)); // Text color
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
ll_main.addView(tv_new, params);
}else if(views[3].equals("edittext")){
....
}
Give
android:paddingLeft=""
and to each element so that the ones to the left most of the screen will have bit of space. and the ones to the right,give
android:paddingBottom=""
First,check the first 2 elements that is the name text and your text field after that you can proceed to the rest.
I can guide you better,if you post your code
Check this,
<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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name"
android:textSize="18sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:padding="10dp"
android:paddingl="10dp" >
<requestFocus />
</EditText>
</RelativeLayout>
You need to learn how layouts work, first try to make the same layout in xml. Then you will learn that this kind of layout can easily be made using LinearLayout.
You can use parent LinearLayout and add sublayouts to it. Now you say if you use LinearLayout all subviews are shown vertically. It shows vertically because you are adding them one by one. If you take a RelativeLayout and add your TextView and EditText to it and then add that RelativeLayout to your LinearLayout, you will get the desired result.
<LinearLayout>
<RelativeLayout>
<TextView />
<EditText />//use layout_margin or layout_toLeftOf for moving to to right
</RelativeLayout>
<RelativeLayout>
<TextView/>
<EditText/>
</RelativeLayout>
</LinearLayout>
Please use Layout params like layout_below layout_above toRightof and ToLeftof when you add a view to container relative layout
https://stackoverflow.com/a/5191159/1911784
you an use below code to add views in your layout
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView text2 = new TextView(context);
newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newParams.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
text2.setLayoutParams(newParams);
layout.addView(text2);
where layout is your main layout.
you can use other params as well - like rightof, leftof etc
I know I'm close but i just need a little help in inflating my layout.
I have a LinearLayout, containing a ScrollView and a HorizontalScrollView, the ScrollView contains a second LinearLayout which contains a TextView and an ImageView and the HorizontalScrollView contatins a third LinearLayout containing a TextView.
Here's the XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="442dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/contentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="#+id/image_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_gravity="bottom">"
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<TextView
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The issue I am having is twofold.
The HorizontalScrollView should be at the bottom, instead it is slightly up, I am guessing due to the height dimensions on the ScrollView (This should take up all the space except for the 25dp reserved by the HorizontalScrollView.
I am having issues inflating the LinearLayouts within the ScrollViews I currently have the code:
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layoutContent;
LinearLayout lin = new LinearLayout(this);
TextView content = null;
//Content
for (int i =0; i<6; i++) {
layoutContent = (LinearLayout)inflater.inflate(R.layout.details_list, null);
content = (TextView)layoutContent.findViewById(R.id.contentText);
content.setText("MyContent" + String.valueOf(i));
lin.addView(layoutContent);
}
this.setContentView(lin);
//Footer
lin = (LinearLayout) findViewById(R.id.footerLayout);
content = null;
for (int i =0; i<2; i++) {
layoutContent = (LinearLayout)inflater.inflate(R.layout.details_list, null);
content = (TextView)layoutContent.findViewById(R.id.footer);
content.setText("Footer "+String.valueOf(i));
lin.addView(layoutContent);
}
which causes me problems:
As the content layout is declared as lin = new LinearLayout(this); this is not using the one from my layout file e.g. the layout looks like
MyContent0 MyContent1MyContent2MyC
onte
nt3
Footer 0 Footer 1
If i increase the number of footer items the MyContent1... appears to get pushed off the right of the screen, if I change it to
lin = (LinearLayout) findViewById(R.id.contentLayout);
I must remove the line this.setContentView(lin); but when i do that the layout looks like:
TextView
MyContent0
--Lots of blank lines--
Footer 0 Footer 1
MyContent1
--Lots of blank lines--
MyContent2
--Lots of blank lines--
MyContent3
--Lots of blank lines--
MyContent4
--Lots of blank lines--
MyContent5
--Lots of blank lines--
If add more elements to the footer, these appear in a HorizontalScroll where Footer 1 Footer 2 is above and scrolls correctly with the rest of the content below.
Many Thanks for taking the time to read this and please let me know if you require more information.
Kind Regards