Android how to add ScrollView - android-layout

I want to add to my application ScreenView, but i'm only getting error.
My layout:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/bg"
android:id="#+id/mainlayout"
tools:context=".NwActivity" android:baselineAligned="false">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/rlA" >
<TextView
android:id="#+id/teamA"
android:text="#string/teamA"
android:textSize="24sp"
style="#style/Txt" />
<Button
android:id="#+id/pAddA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/teamA"
style="#style/Txt"
android:text="#string/pAdd"
android:onClick="addA" />
<EditText
android:id="#+id/eta0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pAddA"
style="#style/Txt"
android:hint="#string/pHint" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/rlB" >
<TextView
android:id="#+id/teamB"
android:text="#string/teamB"
android:textSize="24sp"
style="#style/Txt" />
<Button
android:id="#+id/pAddB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/teamB"
style="#style/Txt"
android:text="#string/pAdd"
android:onClick="addB" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
Activity:
package some.program;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class NwActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nw);
}
public int skid = 0;
public void addA(View view) {
if(skid < 14) {
skid = skid + 1;
//LinearLayout ll = (LinearLayout) findViewById(R.id.mainlayout);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlA);
EditText et1 = new EditText(this);
et1.setHint(R.string.pHint);
String strID = "eta" + skid;
int nid = getResources().getIdentifier(strID, "id", this.getPackageName());
et1.setId(nid);
int oskid = skid - 1;
int oid;
String resID = "eta" + oskid;
oid = getResources().getIdentifier(resID, "id", this.getPackageName());
RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams
( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.BELOW, oid);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
et1.setLayoutParams(params1);
et1.setTextAppearance(this, R.style.Txt);
rl.addView(et1);
}
}
public void addB(View view) {
}
}
How can i add ScrollView, that will not make mess on my layout? When clicking Button, app is creating EditText forms. Everything works fine, but all EditText fields don't fit on layout and don't appear.

There may be some other issues in your layout, some of the issues which I have found are
Firstly ScrollView only takes one direct child, while you are declaring two views rlA and rlB in ScrollView. (You can make it working by putting ScrollView as your main layout and putting LinearLayout in place of ScrollView)
Secondly you should add layout_width and layout_height attributes for your layouts (I guess you are already declaring in your STYLES).

Related

Adding view dynamically in fragment not persist after moving back from another fragment through navigation

I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.
Initial Fragment Class Code
package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
Button pay, buttonAdd;
LinearLayout itemLayout;
Integer item = 1, emptyChk;
Float total;
ConstraintLayout constraintLayout;
Integer layoutId;
public OrderFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_order, container, false);
pay = root.findViewById(R.id.pay);
pay.setOnClickListener(v -> {
datafetch(root);
});
itemLayout = root.findViewById(R.id.itemLayout);
constraintLayout = root.findViewById(R.id.constraintLayout);
buttonAdd = root.findViewById(R.id.button);
buttonAdd.setOnClickListener(v -> addView());
return root;
}
private void datafetch(View root) {
Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
emptyChk = 0;
total = (float) 0;
for (int i = 0; i < itemLayout.getChildCount(); i++) {
View itemView = itemLayout.getChildAt(i);
TextView number = itemView.findViewById(R.id.number);
AutoCompleteTextView name = itemView.findViewById(R.id.namet);
TextInputEditText price = itemView.findViewById(R.id.pricet);
AutoCompleteTextView type = itemView.findViewById(R.id.typet);
if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() ||
type.getText().toString().isEmpty()) {
Snackbar.make(constraintLayout, "Please fill the required fields",
Snackbar.LENGTH_SHORT).show();
emptyChk += 1;
} else {
total = total + Float.parseFloat(price.getText().toString().trim());
}
}
if (emptyChk == 0) {
Log.i("heettotal", total.toString());
OrderFragmentDirections.ActionOrderFragmentToBillingFragment action =
OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
Navigation.findNavController(root).navigate(action);
((BillingActivity) getActivity()).heading("b");
}
}
private void addView() {
item = item + 1;
String text = "Item " + item;
View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
TextView number = itemView.findViewById(R.id.number);
ImageButton remove = itemView.findViewById(R.id.remove);
AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
number.setText(text);
remove.setOnClickListener(v -> removeView(itemView));
itemLayout.addView(itemView);
}
private void removeView(View itemView) {
Log.i("heetview", String.valueOf(itemView));
itemLayout.removeView(itemView);
}
}
Initial Fragment Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdropcenter"
android:theme="#style/Theme.MaterialComponents.NoActionBar"
tools:context=".ui.billfragment.OrderFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/item_layout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Add Item"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/pay"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Pay"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Dynamically Added Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.MaterialComponents.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Item 1"
android:textColor="#191b20"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/remove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#00000000"
android:src="#drawable/ic_close"
app:tint="#191b20" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/nametil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Item Name"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/namet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/typetil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Type"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/typet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/pricetil"
style="#style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Price"
app:boxBackgroundMode="filled"
app:endIconMode="clear_text"
app:endIconTint="#AA191b20">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/pricet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Snaps of issue
Initial state when first fragment starts
Dynamically view added and information filled
First fragment after returning from another fragment

Align the text to center in ListView

I've created a stopwatch app when the user clicks the Lap button just gives the current time the timer is at. My problem is text in ListView displays at the left corner. I'm finding it impossible to center the text in my listview. Here I attached my XML and Java code.
activity_main.xml
<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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="100dp"
android:text="format"
android:textSize="40dp"
android:textStyle="bold" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="110dp"
android:text="Start" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/start"
android:text="Reset" />
<ListView
android:id="#+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/start"
android:layout_marginLeft="50dp"
android:layout_marginTop="9dp"
android:layout_marginRight="50dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
TextView time;
Button reset, start;
ListView listView;
long startTime, TimeBuff, millisecondsTime, UpdateTime = 0L;
int seconds, milliseconds, minutes;
Handler handler;
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
String[] ListElements = new String[]{};///creating arrays of string type
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.textView);
start = (Button) findViewById(R.id.start);
reset = (Button) findViewById(R.id.reset);
listView = (ListView) findViewById(R.id.listview1);
handler = new Handler();
//Getting the list of array
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
ListElementsArrayList);
listView.setAdapter(adapter);
}
}
To do it You have to create a layout in Your res/layout folder. With name e.g. list_item_layout.xml. In this layout add this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp" />
In this file, You can make custom TextView which You want to display (change font, size etc.). To center text use android:gravity="center"
Next, when You create an ArrayAdapter, use this layout:
adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item_layout, // here You use created layout
ListElementsArrayList
);
Result:

Android Scrollview and layout problems

I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.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/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}

Android - Add Linearlayout above ListView

I would like to add a LinearLayout above a ListView, so that when you scroll the screen, it scrolls the entire layout. In my LinearLayout, I have an AChartEngine GraphicalView, which I want above the ListView. I added a ScrollView, and inside that a Spinner, LinearLayout ListView, but when I run the app, only the ListView and the Spinner show up. The LinearLayout doesn't show up on the screen.
Here is my xml file. Thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/spending_report_graph"
android:layout_width="wrap_content"
android:layou`enter code here`t_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
When I call the method that returns my graph, I am given a View, which I need to add to the Layout. The only way I know of to do this is to add it to a LinearLayout.
graphLayout = (LinearLayout)view.getViewById(R.id.spending_report_graph);
graph = PieGraph.getNewInstance(getActivity(), items, items.size());
graphLayout.addView(graph);
Is is this you want to achieve ?
Test Image http://imageshack.com/a/img43/3327/f091.png
IF yes, then description is like, Item1 is the spinner, Item 2 on the bottom is the ListView and the black line in between is the View and the extire layout is in the linear layout and it has one child (Scroll View) and only one direct child of scroll view that's Linear layout.
Here's the xml for it. I am not sure if this is what you are looking for.
<?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/spending_report_graph"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_gravity="center_horizontal"
android:background="#000000"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here's the edited answer :
Test Image http://imageshack.com/a/img809/7741/ti70.png
Here's I am using fragment just to confirm everything was working fine and yes, everything's working perfect.
as you see, there's the fragment I am using, the spinner is at the top, list view on the bottom and after executing the code, here's how the view looks like :
Test Image 2 http://imageshack.com/a/img713/685/qpbi.png
Here, the view inside two green lines is the fragment. Here's the code for everything:
First : the yourmainlayout.xml
<?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Second : The Fragment1
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mike.stackoverflowquestions.R;
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fragment_1, container, false);
}
}
Third: Main Activity
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.mike.stackoverflowquestions.R;
public class ActivityA extends Activity {
String somevalue1 = "Hello";
String somevalue2 = "World";
ListView mListView;
String[] numbers_text = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen" };
ArrayList<String> mArrayList;
ArrayAdapter<String> mAdapter;
ArrayAdapter<String> spinnerAdapter;
Spinner spinner1;
TextView tV;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view_stack);
mArrayList = new ArrayList<String>();
for (String s : numbers_text) {
mArrayList.add(s);
}
spinner1 = (Spinner) findViewById(R.id.spending_report_cycle_spinner);
tV = (TextView) findViewById(R.id.tv);
mListView = (ListView) findViewById(R.id.spending_report_listview);
mAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, mArrayList);
mListView.setAdapter(mAdapter);
spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item,
mArrayList);
spinner1.setAdapter(spinnerAdapter);
}
public void selectFragment(View view) {
Fragment fr;
if (view == findViewById(R.id.btnSayHi)) {
fr = new Fragment1();
} else {
fr = new Fragment1();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction mFragmentTransaction = fm.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_content_1, fr);
mFragmentTransaction.commit();
}
}
I made the edit. Please let me know if this works. Good luck..:)

Android input boxes on the next line

Okay, so I decided to take the Google classes for learning droid devving. I decided instead of the simple input box and button I would make multiple ones, and have each display a set color. The issue is it is jamming all input boxes on one line, rather than their own separate lines. This is what I have in the activity_color.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
This is the main java class
package com.example.color.texts;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class ColorTexts extends Activity {
public final static String EXTRA_MESSAGE = "com.example.colortexts.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_texts);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_color_texts, menu);
return true;
}
/** Called when the user clicks the Send button for Blue */
public void sendMessageBlue(View view) {
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message_blue);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Red */
public void sendMessageRed(View view) {
Intent intent = new Intent (this, DisplayMessageActivityRed.class);
EditText editText = (EditText) findViewById(R.id.edit_message_red);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Orange */
public void sendMessageOrange(View view) {
Intent intent = new Intent (this, DisplayMessageActivityOrange.class);
EditText editText = (EditText) findViewById(R.id.edit_message_orange);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I guess the question is how to either tell it to start a new line, like android:layout_next="1" or something (I KNOW THAT DOESN'T EXIST), or would I have to add to the public class? Such as make another layout file and reference to that? I highly doubt the latter would work.
EDIT: Per the instructions laid out, I think this is how I was supposed to do it, but it only know shows the first line :(
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
</LinearLayout>
<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"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_red"
android:onClick="sendMessageRed" />
</LinearLayout>
<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"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
</LinearLayout>
Your parent layout is set to be android:orientation="horizontal". This needs to be set to android:orientation="vertical".
EDIT: Here is the reference in the Android docs:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:orientation

Resources