Android scrollview does not work on dynamic content - java

I am dynamic creating table rows and textviews from my JSON data which I get from an api. Currently the data gets displayed correctly, but somehow my scrollview does not work. It does not scroll. I did add a ScrollView but I guess I am doing something wrong there.
My Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tlMarksTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
And the part in which I generate the views looks like this:
jsonArray = new JSONArray(result);
TableRow tableRow = new TableRow(context);
setContentView(tableRow);
tableRow.setOrientation(LinearLayout.VERTICAL);
for(int i= 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("id");
String content = jsonobject.getString("content");
TextView textView = new TextView(context);
textView.setText(content);
if (Build.VERSION.SDK_INT < 23) {
textView.setTextAppearance(getApplicationContext(), R.style.boldText);
} else {
textView.setTextAppearance(R.style.boldText);
}
textView.setBackgroundResource(R.color.highlightedTextViewColor);
textView.setPadding(10, 10, 10, 10);
textView.setTextSize(getResources().getDimension(R.dimen.joke_content_font_size));
tableRow.addView(textView);
}

have your inner component configured like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/tlMarksTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
by the way the outer RelativeLayout ViewGroup doesn't do much

Why don't you use a recyclerview?
You should use retrofit to consume WS

The main problem i see here, is that the LinearLayout that encapsulates your TableLayout, has android:layout_height="match_parent". This means that the inner child of the ScrollView, will have the same height that the parent, resulting in a non-scrolling layout.
Why don't you try removing that LinearLayout, and leaving the TableLayout as the only child? Remember to set the height of that child to android:layout_height="wrap_content".
By the way, i suggest using a ListView for displaying large amounts of data.
One last thing: there is no need of declaring the xml namespace (xmlns:android="http://schemas.android.com/apk/res/android") in each Layout declaration. Only with one declaration at the root element will be fine ;)

Related

How to add dynamic buttons vertically one after another in android?

I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.
RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn;
List<Button> allEds = new ArrayList<Button>();
for(int i=0;i<totalSID;i++){
btn = new Button(FaqList.this);
btn.setId(i);
allEds.add(btn);
if(i==0){}
else
buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());
body.addView(btn,buttonParams);
}
Adding the xml file. For reference you can go through it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:animateLayoutChanges="true">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout
I am not sure where is the problem actually
If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.
int iNumberOfButtons = productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];
LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < iNumberOfButtons; i++) {
ProductType productType = productTypeList.get(i);
dynamicButtons[i] = new Button(getActivity());
dynamicButtons[i].setText(productType.getTitleString());
dynamicButtons[i].setId(i);
dynamicButtons[i].setTextSize(15.0f);
dynamicButtons[i].setOnClickListener(this);
dynamicButtons[i].setLayoutParams(paramsButton);
dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);
you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);

Scroll on duplicated Layout - Android

Here I am using this code to paste my main_activity.xml on top of itself 5 times, making a long list of itself
Here is my code:
for(int i = 0; i < 5; i++){
LayoutInflater loiViewInflater = (LayoutInflater)
getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View mView = loiViewInflater.inflate(R.layout.activity_main,
null);
FrameLayout.LayoutParams params = new
FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0,i * 400,0,0);
addContentView(mView, params);
}
Everything works handy dandy, but how can I make it to where I can scroll down?
The activities go off the screen and I can't think of a way to scroll down. Is it possible?
Thanks, Cooper
You can always use a ScrollView, just set some container, like a linear layout, and copy your layout inside.
The hierarchy should look like:
ScrollView
LinearLayout
YourCopy
YourCopy
YourCopy
For instance, for your container view:
<?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">
<LinearLayout
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
And on your loop (once you set your layout above with setContentView) something like:
LinearLayout container = (LinearLayout)findById(R.id.list_container)
for(int i = 0; i < 5; i++){
// inflate and set up your layout mView
// (...)
container.addView(mView);
}
Basically, you are injecting your copies inside the LinearLayout (which is inside the ScrollView).
As Logain suggested, you can use a ScrollView and copy your layouts inside.Here is a sample.
<ScrollView
android:id="#+id/start_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/fragment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/page_title_text"
android:textSize="32sp"
android:paddingBottom="5dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/fragment_intro"
style="#style/wizard_intro_style"
android:layout_below="#+id/fragment_title" />
</RelativeLayout>
</ScrollView>

TextView moved down when new line

I'm currently working on a server-client android application, and in one of my views I have a HorizontalScrollView that contains a vertical LinearLayout. This LinearLayout will contain a bunch of TextViews that are customized with a background. It works fine until the TextViews get a new row because of the character length, it looks like this:
http://puu.sh/9L127/bf29a86639.jpg
The idea is that the text is supposed to fit in the background, is there any way that I can keep the TextView position like the other ones, even though it has multiple rows?
I hope I made myself clear enough, this is how the TextViews are set up in the code:
public void setupCard(GameCard card) {
final int CARD_WIDTH = 200;
final int CARD_HEIGHT = 100;
m_card = card;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CARD_WIDTH, CARD_HEIGHT);
lp.setMargins(0, 0, 5, 0);
super.setText(m_card.getContent());
super.setBackgroundResource(R.drawable.cardbg);
super.setLayoutParams(lp);
super.setGravity(Gravity.CENTER);
}
And this is how the activity is set up:
<LinearLayout 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:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bng"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/cardLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Thanks in advance!
Thats because the LinearLayout alignes the text baselines of the TextViews.
Try to specify
android:baselineAligned="false"
for the LinearLayout.

Trying to add multiple views but get a blank screen

I created a custom xml file with a LinearLayout and TextView. I have a for loop that i want go through an ArrayList and add the view to a ScrollView for each object. Below is the code i tried to achieve this, but it just gives me a blank screen. I tried to see if the ArrayList was empty, but that's not it. I didn't really know what i was doing when i tried this. I just wanted to see if it worked, but it didn't so what's the problem?
for(int x = 0; x < runs.size(); x++){
inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());
TextView name = (TextView) layout.findViewById(R.id.tvRunName);
name.setText(runs.get(x).getName());
llRuns.addView(layout);
}
Heres the run_layout 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="#drawable/run_background" >
<TextView
android:id="#+id/tvRunName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center"
android:layout_marginTop="10dp" />
Here's the Activities xml....
<LinearLayout 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=".YourRuns"
android:background="#181818"
android:id="#+id/llYourRunsLayout">
<ScrollView
android:id="#+id/svRuns"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llRuns"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Change (ViewGroup) getCurrentFocus() to null, becouse inflater.inflate() second parameter is parent of view, and when you start your activity for default no elements with focus. And you indicate two parents for view first on inflate, second when you say to add layout into llRuns.
for(int x = 0; x < runs.size(); x++){
inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());
TextView name = (TextView) layout.findViewById(R.id.tvRunName);
name.setText(runs.get(x).getName());
llRuns.addView(layout);
}

Adding custom relative layout to one single liner layout

I have made following relative layout in an xml file lets say add_relative_layout.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:orientation="vertical"
android:id="#+id/addAccountLinearLayout">
</LinearLayout>
Above is the main layout in which i want to add copies of below Code file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/UIContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white" >
<TextView
android:id="#+id/amountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="Amount"
android:textColor="#android:color/black"
android:textStyle="bold" />
<EditText
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" >
</EditText>
I have another android xml file named as Show_all.xml. It is a linear layout xml
I want to add this relative layout above as many times as i want in this show_all layout
currently i am using this code
private void callOnCreate()
{
linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout.
layout = (RelativeLayout) findViewById(R.layout.ui_relative_layout_style); // name of xml File of above code.
for (int i=0; i < 4; i++)
{
Account account = accountArray.get(i);
linear.addView(layout, i);
}
}
I am getting Null point exception. Please tell me what to do .
Best Regards
Hello Umar I have no idea how you can use xml layout to add Dynamically But you can use Below Code to get Your Required thing add in to LinearLayout
public RelativeLayout createViewTOAdd(){
lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout mRelativeLayout=new RelativeLayout(this);
mRelativeLayout.setBackgroundColor(Color.WHITE);
TextView mTextView=new TextView(this);
RelativeLayout.LayoutParams Textview_lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
mTextView.setText("Amout");
Textview_lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Textview_lp.addRule(RelativeLayout.CENTER_VERTICAL);
Textview_lp.leftMargin=10;
mTextView.setTextColor(Color.BLACK);
mTextView.setTextAppearance(this, R.style.TextStyle);
//mTextView.setLayoutParams(Textview_lp);
EditText mEditText=new EditText(this);
RelativeLayout.LayoutParams EditText_param=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
EditText_param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
EditText_param.addRule(RelativeLayout.CENTER_VERTICAL);
EditText_param.rightMargin=10;
//mEditText.setLayoutParams(EditText_param);
//mRelativeLayout.addView(mTextView, 0);
//mRelativeLayout.addView(mEditText, 1);
//mRelativeLayout.addView(mTextView);
//mRelativeLayout.addView(mEditText);
mRelativeLayout.addView(mTextView, Textview_lp);
mRelativeLayout.addView(mEditText, EditText_param);
return mRelativeLayout;
}
Now How to add View in to LinearLayout is Below
mLinearLayout=(LinearLayout)findViewById(R.id.mainLinearView);
mLinearLayout.removeAllViews();
for(int i=0;i<4;i++){
mLinearLayout.addView(createViewTOAdd(), i);
}
Hey your are using findViewById to get the instance of relativelayout which is not available in your current layout show_all.xml .thats why you are getting null pointer exception.So create separate layout for that specific relativelayout and names that xml layout file as UIContianer then try to use below code
private void callOnCreate()
{
linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout.
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.ui_relative_layout_style, null);
for (int i=0; i < 4; i++)
{
Account account = accountArray.get(i);
linear.addView(vi, i);
}
}

Categories