I am trying to add a spinner dynamically to a RelativeLayout but I am having problems.
This is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/Relativ"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
And in my class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Relativ);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,1);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
Spinner list = new Spinner(this);
list.setAdapter(adapter);
rl.addView(list);
}
But it does't run. I would like to know what is wrong in my code and how I can fix it.
I have tryed with RelativeLayout params in the RelativeLayout.addView and it doesn't run.
Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = new RelativeLayout(this);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Spinner list = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, arrayList);
list.setAdapter(adapter);
rl.addView(list);
setContentView(rl);
}
main.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
final Spinner list= (Spinner) findViewById(R.id.spinner1);
list.setAdapter(adapter);
}
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.prova.MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
</RelativeLayout>
Check with your parameters.
Related
Ok, I have been on this for 6 hours now and for some reason, I just don't get it.
I am trying to make an app to calculate something using a certain formula.
Now consider it as a grade point average calculating thing. I have a button which should allow the user to add two more spinners to the layout and thereby proceeding further. Now I made a method for this purpose. When I call this method for the first time the spinners are added. But it doesn't work the second time.
It also doesn't work if I put it in the button onClick() method. I have gone through multiple examples posted online and also on stackoverflow, maybe I am making some stupid mistake. Just guide me through this, please.
Here is the code:-
SGPA.class
public class SGPA extends AppCompatActivity {
View v;
Context c;
LinearLayout subjectData;
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
//int id = View.generateViewId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sgpa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
//TODO if 2015 or 2013 regulations
//assuming 2015
fill2015();
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sgpa, null);
subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
c = this;
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addViewForAnotherSubject();
setContentView(v);
}
});
setContentView(v);
}
void addViewForAnotherSubject()
{
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
subjectData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
void fill2013()
{
}
}
activity_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="innominatebit.srmite.SGPA">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_sgpa" />
</android.support.design.widget.CoordinatorLayout>
content_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="6dp"
android:paddingEnd="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingStart="6dp"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="innominatebit.srmite.SGPA"
tools:showIn="#layout/activity_sgpa">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/subjectdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
All help is appreciated. Thanks in advance!
Ok, I got it. i made another test application for debugging purposes until I got it. Seems I did not need a layout inflator at all.
Here's the code:
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
fill2015();
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
final LinearLayout myData=(LinearLayout)findViewById(R.id.myData);
addforanothersubject(myData);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addforanothersubject(myData);
}
});
}
void addforanothersubject(LinearLayout myData)
{
Context c=MainActivity.this;
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
myData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
}
And the xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="aaa.test.MainActivity">
<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">
<LinearLayout
android:id="#+id/myData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Hope it helps someone.
I have two activities. MainActivity.java and HomeActivity.java. I am trying to create a ListView in HomeActivity.java just with "One", "Two", "Three", and "Four" as the List items but the ListView is not showing up at all.
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
String personName;
String email;
String rfid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
populateListView();
Bundle extras = getIntent().getExtras();
if (extras != null) {
personName = extras.getString("Name");
email = extras.getString("Email");
rfid = extras.getString("RFID");
}
String params = (email + "+" + rfid);
new MyAsyncTask().execute(new Pair<Context, String>(this, params));
toolbar.findViewById(R.id.ButtonSettings).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(HomeActivity.this, findViewById(R.id.ButtonSettings));
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.getMenu().add(personName);
popup.getMenu().add(email);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
HomeActivity.this, "You Clicked" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
content_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ie.myy3p.ticktask.HomeActivity"
tools:showIn="#layout/activity_home">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/profile_layout"
android:layout_below="#+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tView"
android:layout_alignBottom="#id/toolbar"
android:layout_alignParentEnd="true" />
<ListView android:id = "#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
show_tasks.xml:
<?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">
</TextView>
EDIT:
So I made a few changes to my code and now my ListView is showing up in content_home.xml and activity_home.xml in Android Studio, which it wasn't doing before. However, when I run the app on my phone the ListView doesn't show up. I've debugged it and checked my populateList method and it seems to be working fine. Debug at the end of populateList method reads:
this = {HomeActivity#20765}
myItems = {String[4]#20825}
adapter = {ArrayAdapter#20841}
list = {ListView#20853} "android.widget.ListView... etc
By giving a custom view to the adapter, you need an extra step to populate how that custom view uses the array adapter data to populate. The easiest solution is just ditch the custom view and use the simple list:
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
myItems);
If you want to continue to use a custom view for your cells, you'd need to create a custom adapter class that overrides getView() to tell the listview how given your string value, how it populates the view, essentially it sets the text on your textView (however this is the default behavior of android.R.layout.simple_list_item_1).
Here's a great example if you want to keep your custom view: https://stackoverflow.com/a/15832564/1316346
EDIT:
Also looks like you are inflating the wrong view:
setContentView(R.layout.activity_home);
is inflating with a resource called activity_home, from what you've posted it should probably read:
setContentView(R.layout.content_home);
Change here.
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
To this
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
Alternative 2
Change set content view from this
setContentView(R.layout.activity_home);
To this
setContentView(R.layout.content_home);
It's Done.
I am trying to inflate inflater_layout.xml and add it to activity_main.xml.
Can anyone let me know how can I assign the gravity for the view object of inflater_layout.xml while i am adding it to activity_main.xml?
inflater_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello I'm inflated"
android:textSize="40sp"
android:id="#+id/toBeInflated"
/>
activity_main.xml (Contains a listview)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id = "#+id/relative"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id = "#android:id/list">
</ListView>
MainActivity.java
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
ListView l;
String[] days = {"Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday", "Sunday"};
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_element, days);
l.setAdapter(adapter);
inflater = getLayoutInflater();
l.setOnItemClickListener(this);
RelativeLayout r = (RelativeLayout) findViewById(R.id.relative);
View inf = inflater.inflate(R.layout.inflater_layout,r);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
This is the outcome:
Can be achieved by adding LayoutParams to the inflated View:
//small change, attachToRoot = false:
View inf = inflater.inflate(R.layout.inflater_layout,r, false);
//LayoutParams always refer to the parent of the actual view:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//example Rule:
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
inf.setLayoutParams(lp);
r.addView(inf) //because we haven't added it yet.
Maybe also look here for an explanation for the attachToRoot = false.
So, i was following the NewBoston series on Android tutorials. So, there is this tutorial on setting up tabs using Tabhost and Tabspecs. Now, i tried exactly what the tutorial does but crashes and it points to a particular line in my code which is setContentView() method in the OnCreate(). I tried even removing it and ran the code but the program just crashes. What could be my mistake? I also tried the code in the following article to see if that works but it still crashes.
http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
This is my code-
Splash.java is the class that starts up the Launcher activity
public class Splash extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = (TabHost)findViewById(R.id.tabHost2);
TabHost.TabSpec tab1 = th.newTabSpec("First Tab");
TabHost.TabSpec tab2 = th.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = th.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this, TabActivity2.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this, TabActivity3.class));
th.addTab(tab1);
th.addTab(tab2);
th.addTab(tab3);
}
}
TabActivity1.java-
public class TabActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
TabActivity2.java:-
public class TabActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
TabActivity3.java
public class TabActivity3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
splash.xml
<?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">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost2"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Change this line
th = (TabHost)findViewById(R.id.tabHost2);
to
th = (TabHost)findViewById(android.R.id.tabHost2);
This will do.
Because, in the layout file, you might have set Tabhost id like this android:id="#android:id/tabhost2">
This is my first Android app, i am trying to put a listview on and i have followed numerous tutorials but i always get the same result, a crash upon starting the app.
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"
android:background="#drawable/background"
android:gravity="top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mattigins.mediacenter.MainActivity$PlaceholderFragment" >
<View
android:id="#+id/view1"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/View01"
android:background="#drawable/semi_trans_box" />
<View
android:id="#+id/View01"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_marginRight="58dp"
android:layout_toLeftOf="#+id/view1"
android:background="#drawable/semi_trans_box" />
<ListView
android:id="#+id/List1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/View01"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/View01" >
</ListView>
Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ListView listView = (ListView) findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);
}
Looks like your ListView belongs to the Fragment layout. Looking at
In onCreateView of Fragment.
ListView listView = (ListView) rootView.findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);