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);
Related
I have function that loads data form a server, like a search then adds these tot the main menu.
To accomplish this I am using a for loop on the JSON results to add the items.
This loop works fine, it reads the data and loops through fine:
Java Loop:
JSONArray teams = result.getJSONArray("teams");
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) mainMenu.findViewById(R.id.team_list_view);
//Log.d("TEAMS",teams.toString());
for(int x = 0; x < teams.length(); x++) {
JSONObject cTeam = teams.getJSONObject(x);
String name = cTeam.getString("name");
String thumb = cTeam.getString("thumb");
String id = cTeam.getString("id");
View custom = inflater.inflate(R.layout.teams_menu_template, null);
int width = LinearLayout.LayoutParams.FILL_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
ImageButton pp = (ImageButton) custom.findViewById(R.id.tempPPbtn);
Button teamName = (Button) custom.findViewById(R.id.tempPPTxtbtn);
teamName.setText(name);
loadImage loadImage = new loadImage("imagebutton",pp);
loadImage.execute(thumb);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
parent.addView(custom);
}
Now this does work fine it loops through and adds the image and text and appends to the parent layout. But instead of stacking the new layouts it places them side by side like in the image below:
After some googling I tried adding params to set the width to FILL_PARENT but the outcome only adds the first item. However it does add it as I want.
I've stuck on this for quite some time, if anyone can help it would be greatly appriated.
My Template XML file i'm using.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/tempDropCont"
android:background="#drawable/drop_down"
android:weightSum="100"
android:baselineAligned="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="70dp"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/tempPPbtn"
android:background="#drawable/profile"
android:layout_marginLeft="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/leader_board"
android:id="#+id/tempPPTxtbtn"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tempDrop"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/view_team"
android:id="#+id/tempTxtBtn1"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/edit_team"
android:id="#+id/tempTxtBtn2"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_settings"
android:id="#+id/tempTxtBtn3"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
At first I did think it was the xml but I have tried using include on a different layout and it includes the file fine as its supposed too.
NOTE from the server there is two items returned.
It would have been nice to have the parent layout as well, or at least the way you defined the parent (LinearLayout with the id listview).
However, there are several culprits for the behavior that you describe:
Make sure that the parent layout has the orientation set to vertical. At this point you can copy paste a couple of template items in your layout and see if they look alright when you define them in xml
When you inflate your item, you need to pass the parent as well, so that the child inherits the layout properties:
View custom = inflater.inflate(R.layout.teams_menu_template, parent, false);
This will create the item with the expected properties as defined in the parent container, but not attach it to the parent just yet.
This line is not used:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
You don't set the parameters once you created them. But I think this will be redundant once you do the inflation properly.
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 am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?
Here is my XML file so far:
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
use the below code. This will satisfy your requirement. I tried this and working for me.
Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="ListView Heading" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="ListView Footer" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_above="#id/textView2"
></ListView>
The UI will like this
Try this way, hope this will help you to solve your problem.
Instead of using header/footer just put as below code in your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/header_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_transaction_string">
</TextView>
</LinearLayout>
Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.
1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="#+id/header" and layout_above="2+id/footer"
I found a possible solution for your problem from a similiar post. Hope this helps you.
For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle
<RelativeLayout ...>
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_view"
android:layout_above="#+id/footer_view">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
</RelativeLayout>
You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".
Get all the elements in a array: Example:- (weatherArray)
Loop through all the elements :-
Example:-
mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
mainLayout.removeAllViews();
for (int i = 0; i < weatherArray.length(); i++) {
View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false);
TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
todayTempStatus.setText("");
}
This is an example without using listview, which we will populate lienarlayout using child view.
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);
}
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);
}
}