I followed this piece of code written by Aby in another question: https://stackoverflow.com/a/22682248/5915747
The beginning of my text is being cut off on the left side of my screen. I'm missing quite a bit of info and not sure how to fix it, I have tried everything that worked for others but it has not worked for me.
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollviewtest"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.info.PopupActivity"
android:background="#f0f0f0"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/rel_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
android:background="#80000000"
android:layout_centerHorizontal="true">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
init method:
public void init() {
View popupview = getLayoutInflater().inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupview, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
popupWindow.showAtLocation(popupview, Gravity.CENTER, 0, 0);
popup_shown[0] = true;
TableLayout tableLayout = (TableLayout) popupview.findViewById(R.id.table_layout);
TableRow row0 = new TableRow(this);
TextView text0 = new TextView(this);
text0.setText("Test Header1");
row0.addView(text0);
TextView text1 = new TextView(this);
text1.setText("Test Header2");
row0.addView(text1);
TextView text2 = new TextView(this);
text2.setText("Teset Header3");
row0.addView(text2);
TextView text3 = new TextView(this);
text3.setText("Test Header4");
row0.addView(text3);
TextView text4 = new TextView(this);
text4.setText("Test Header5");
row0.addView(text4);
tableLayout.addView(row0);
for (int i = 0; i < 8; i++) {
TableRow tableRow = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setGravity(Gravity.CENTER);
tv0.setText("long test field 0");
tableRow.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.CENTER);
tv1.setText("long test field 1");
tableRow.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.CENTER);
tv2.setText("long test field 2");
tableRow.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.CENTER);
tv3.setText("long test field 3");
tableRow.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.CENTER);
tv4.setText("long test field 3");
tableRow.addView(tv4);
tableLayout.addView(tableRow);
}
}
The longer the strings are for the text views, the more it crops off, however it's a horizontal scrollview, so it should just start at the beginning of the screen and add length to the end, which is scrollable, right?
This is what it ends up looking like with the current strings in vertical/portrait orientation:
Vertical View
This is what it looks like in horizontal, it seems to fix since my screen is large enough for the data:
Horizontal View
If you have any questions, please ask!
Solved this by adding padding to the scrollview. It seems this is a common android bug.
I don't know will it work or not but try removing
android:layout_gravity="center"
line from Relative Layout
Related
Here is what I have:
LinearLayout horizontalLL = new LinearLayout(thisActivity);
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,1));
TextView captionTV = new TextView(thisActivity);
captionTV.setText(element.description);
captionTV.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
but the result is bad: the editText is still very short and does not fill the horizontal linear layout. I've tried to set the weight for captionTV too, but still no result.
Please do the change as below
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
You can use LinearLayout.LayoutParams.MATCH_PARENT or LinearLayout.LayoutParams.WRAP_CONTENT instead of 0 here.
If possible replace all LinearLayoutCompat to LinearLayout.
Try the following code, if you want that your EditText automatically cover the remaining space:
LinearLayout horizontalLL = new LinearLayout(this);
horizontalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
TextView captionTV = new TextView(this);
captionTV.setText("element.description");
captionTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT , 1));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
You need to use below code :
LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);
android.widget.LinearLayout horizontalLL = new android.widget.LinearLayout(this);
horizontalLL.setOrientation(android.widget.LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 150,1f));
TextView captionTV = new TextView(this);
captionTV.setText("Hello textview ");
captionTV.setTextColor(Color.BLUE);
captionTV.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.4f));
horizontalLL.addView(captionTV);
EditText editText = new EditText(this);
editText.setText("Hello edittext ");
editText.setTextColor(Color.BLUE);
editText.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(editText);
ll1.addView(horizontalLL);
because in XML weight define like below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="test"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:text="test"/>
</LinearLayout>
I am new to android and I have used a TableLayout and I am adding two columns of data into it from a string array. But my second column goes out of screen. It seems that all entries of my second column start from where the longest entry in the first column ends. How to make both the columns fit into the screen with like 50% width for both columns?
My code goes like this..
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.findia1);
TableLayout tl = (TableLayout) findViewById(R.id.tablay1);
for (int i = 0; i < s1.length; i += 2) {
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(this);
textview1.setText(s1[i]);
tr1.addView(textview1);
TextView textview2 = new TextView(this);
textview2.setText(s1[i+1]);
tr1.addView(textview2);
tl.addView(tr1, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml code is
<?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" >
<TableLayout
android:id="#+id/tablay1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</TableLayout>
</LinearLayout>
Give weight android:layout_weight="1" for the TextView
i guess i got the answer.. i used
tl.setColumnShrinkable(0, true);
tl.setColumnShrinkable(1, true);
this does the job!
On create i am creating my layout problematically.After that on click on button i want to remove the previous layout content How could i do that i tried that removeView.But it didn't worked for me .Here is my code
void setDate(){
flightResult=(LinearLayout)findViewById(R.id.flightResultData);
LinearLayout.LayoutParams flightDetailsLayout = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout.LayoutParams forUnderLine = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
forUnderLine.setMargins(0,0, 0, 0);
flightDetailsLayout.setMargins(0, 40, 0, 0);
for(int i=0;i < 5;i++){
TextView line=new TextView(this);
line.setBackgroundResource(R.layout.shape_line);
line.setLayoutParams(forUnderLine);
if(i!=0){
flightResult.addView(line);
}
LinearLayout flightInformations=(LinearLayout)inflater.inflate(R.layout.flight_details_layout, null);
flightLogo=(ImageView)flightInformations.findViewById(R.id.flightLogo);
flightCompany = (TextView)flightInformations.findViewById(R.id.flightCompany);
flightLogo.setImageResource(R.drawable.airindia);
flightCompany.setText("AirIndia");
flightResult.addView(flightInformations);
}
TextView dummy=new TextView(this);
dummy.setLayoutParams(flightDetailsLayout);
flightResult.addView(dummy);
}
AFter on create on click of a button i have again call this function and at that time when i calling this it is appending the result .I am want new result on the click of the button what i have to do for this Please help me
And my xml file
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/sortFlightLayouts">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:id="#+id/flightResultData"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
View.setVisibility(View.GONE);
I have a vertical LinearLayout with android:layout_height = "fill_parent".
Now, I add multiple child views to it, sometimes 2 views, at other times 3, based on certain parameters from database, dynamically.
I would like to know how can I, programmatically, distribute these dynamically added childviews in the vertical LinearLayout, so that they get distributed uniformly.
BTW, i cannot use GridView as I have to embed this LinearLayout in horizontal ScrollView, and using GridView inside horizontal ScrollView is highly discouraged.
EDIT: Adding source and xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/serverScreensScreen1LL1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<ScrollView
android:id="#+id/serverScreensScreen1SV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<HorizontalScrollView
android:id="#+id/serverScreensScreen1HSV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/serverScreensScreen1LL2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout> <!-- Main Container -->
SOURCE:
private void drawContent()
{
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(fpfp);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(wcwc);
lp3.weight = 0.3F;
LinearLayout ll1 = (LinearLayout) findViewById(R.id.serverScreensScreen1LL2);
for (int i=0; i<20; i++)
{
LinearLayout ll2 = new LinearLayout(this);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
ll2.setWeightSum(1.0F);
LinearLayout ll3 = new LinearLayout(this);
ll3.setOrientation(LinearLayout.VERTICAL);
ll3.setLayoutParams(lp3);
TextView tv1 = new TextView(mContext);
tv1.setText("Sample Text A: " + i);
tv1.setBackgroundColor(Color.rgb(12, 34, 56 + i * 8));
tv1.setLayoutParams(wcwc);
ll3.addView(tv1);
LinearLayout ll4 = new LinearLayout(this);
ll4.setOrientation(LinearLayout.VERTICAL);
ll4.setLayoutParams(lp3);
TextView tv2 = new TextView(mContext);
tv2.setText("Sample Text B: " + i);
tv2.setBackgroundColor(Color.rgb(34, 12 + i * 8, 56));
tv2.setLayoutParams(wcwc);
ll4.addView(tv2);
LinearLayout ll5 = new LinearLayout(this);
ll5.setOrientation(LinearLayout.VERTICAL);
ll5.setLayoutParams(lp3);
TextView tv3 = new TextView(mContext);
tv3.setText("Sample Text C: " + i);
tv3.setBackgroundColor(Color.rgb(56 + i * 8, 34, 12));
tv3.setLayoutParams(wcwc);
ll5.addView(tv3);
ll2.addView(ll3);
ll2.addView(ll4);
ll2.addView(ll5);
ll1.addView(ll2);
}
}
Any suggestions plz...
I am not sure of how you are trying to distribute the views but you can try playing with weights.
Search for android:weightSum inside the LinearLayout's docs.
I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...
I have an existing layout.xml file that includes:
<RelativeLayout style="#style/bodyLayout">
<ScrollView
android:id="#+id/scrollBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:id="#+id/scrollLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Then I have programatic code as follows:
LinearLayout ll = new LinearLayout(this);
ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(R.dimen.text_size_medium);
tv.setText("test");
tv.setLayoutParams(lp);
ll.addView(tv);
The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...
I have run your code works fine
LinearLayout ll = (LinearLayout)findViewById(R.id.subLinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(15);
tv.setText("test adding");
tv.setLayoutParams(lp);
ll.addView(tv);
if you add new View(anything) from any button click event then add this line
ll.invalidate();
it will refresh it your component