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);
}
Related
After trying to create a CardView programmatically, a NullPointerException but I've used an if statement and the warning is gone, but I'm not sure what needs to go in else part of the if statement.
Method invocation 'inflate' may produce java.lang.NullPointerException
Java
//start of CardView
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
if(inflater1 != null) {
View inflatedCardviewLayout = inflater.inflate(R.layout.my_cardview, new RelativeLayout(getContext()), false);
inflatedCardviewLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.green));
TextView cv_tv1a = inflatedCardviewLayout.findViewById(R.id.cardview_titleA);
cv_tv1a.setText(getString(R.string.titleA));
ImageView imgPictogram = inflatedCardviewLayout.findViewById(R.id.image_pictogram);
imgPictogram.setImageResource(R.drawable.ic_pictogram);
TextView cv_tv1b = inflatedCardviewLayout.findViewById(R.id.cardview_titleB);
cv_tv1b.setText(getString(R.string.titleB));
} else {
//do something else
}
//end of CardView
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/tools"
android:id="#+id/my_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/cardview_titleA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image_pictogram"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/cardview_titleB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
You should fix the NullPointerException since getting an inflater is a required use for your code. I mean fix by using LayoutInflator.from(parent context) instead of asking the system for one.
You should make a class so that you extend CardView, then use the context of when you create the view object to get the inflater. Also make sure to merge the CardView with the inflated layout!
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 ;)
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>
I made a custom xml that has a LinearLayout with the layout_margins set. If i view the graphic layout it shows the margins, but in another activity when i try to add this view to a LinearLayout within a ScrollView none of the margins are there. Here's the custom xml code....
<?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"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp">
<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 code that adds the view....
for(int x = 0; x < runs.size(); x++){
inflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.run_layout, null);
TextView name = (TextView) layout.findViewById(R.id.tvRunName);
name.setText(runs.get(x).getName());
llRuns.addView(layout);
}
How can i have the views spaced out like i want?
The LayoutParams are not being included while inflating the layout. Try adding a third parameter while inflating the layout:
View layout = inflater.inflate(R.layout.run_layout, null, false);
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);
}
}