I have a Java class and two XML files which are basically just a side by side listView which takes in some values. I want to put a header or a text view above each list view ( they should stay side by side ) however every time I try to add a textView the whole project messes up and following online instructions is not working to implement a header.
public class Leaderboard extends AppCompatActivity {
private ListView UsernameList;
private ListView ScoreList;
LocalDatabase localDatabase;
/** Called when the activity is first created */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
ArrayAdapter<String> listAdapter;
ArrayAdapter<Integer> list2Adapter;
//Find the ListView resources
UsernameList = (ListView) findViewById(R.id.UsernameList);
ScoreList = (ListView) findViewById(R.id.ScoreList);
//Create and populate the list of usernames.
//This is where the information from the Database would need to be entered. and the String[] removed
String[] usernames = new String[]{"Andy", "Marie", "George"};
ArrayList<String> usernameList = new ArrayList<String>();
usernameList.addAll(Arrays.asList(usernames));
//Create and populate the list of scores
//This is where the information from the Database would need to be entered. and the Integer[] removed
Integer[] scores = new Integer[]{7, 4, 1};
ArrayList<Integer> scoreList = new ArrayList<Integer>();
scoreList.addAll(Arrays.asList(scores));
//adds the users details to the leaderboard
localDatabase = new LocalDatabase(this);
Contact contact = localDatabase.getLoggedInUser();
//Set a string to have the value of the users name.
String s = contact.name;
//Create Array Adapter using the username list
listAdapter = new ArrayAdapter<String>(this, R.layout.activity_row, usernameList);
//Add more users
listAdapter.add(s);
//Set the Array Adapter as the ListView's adapter
UsernameList.setAdapter(listAdapter);
//Create Array Adapter using the username list
list2Adapter = new ArrayAdapter<Integer>(this, R.layout.activity_row, scoreList);
//Add more users
list2Adapter.add(0);
//Set the Array Adapter as the ListView's adapter
ScoreList.setAdapter(list2Adapter);
}
}
Then the two xml files
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/colorPrimaryDark"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textStyle="bold"
android:padding="10dp"
android:textSize="16sp">
</TextView>
<?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="fill_parent"
android:background="#color/colorPrimaryDark"
android:layout_height="fill_parent"
android:padding="20dp"
android:orientation="horizontal"
tools:context=".Leaderboard">
<ListView
android:id="#+id/UsernameList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight=".60"
android:drawSelectorOnTop="false"
android:headerDividersEnabled="false">
</ListView>
<ListView
android:id="#+id/ScoreList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight=".40">
</ListView>
</LinearLayout>
Try encapsulating the two TextViews in a LinearLayout. The linear Layout should span the entire width (match_parent), but only be as high as need be; wrap_content.
What you then want to do to have them neatly divided amongst the width of the screen, is, first off, set the LinearLayout orientation to horizontal. Then, you make the width of and both TextViews 0dp (that's right, 0dp), and their height wrap_content. Then, give both TextViews a weight of 1. The views now both take up half the space (in width) of the LinearLayout. Finally, centre the TextViews for a good looking header.
Ive kept it very broad and non-codey, but I think you should be able to google all the concepts explained above if you run into any trouble. If you really cant figure it out,i'll help you out in the comments.
Related
How do I dynamically generate a RelativeLayout based on a xml layout (posted below)? The relative layouts are being put in a linear layout that is the child of a scroll view.
<RelativeLayout
android:id="#+id/post_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="#+id/op_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="Username" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/op_username"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="31dp"
android:layout_marginLeft="31dp"
android:layout_marginTop="15dp"
app:srcCompat="#android:drawable/btn_star_big_on"
tools:srcCompat="#android:drawable/btn_star_big_on" />
</RelativeLayout>
Let's suppose that you've all the Images and their TextView's texts in your Database, then create a RecyclerView (if there are a relatively huge number of items) or a simple ListView, and then create a CustomAdapter class to add your RelativeLayout to your main ListView/ ScrollView/ RecyclerView.
To completely implement this first you need to have an Item class which can store a user's Username and Image ( please note that you need to pass Image URI as a string) in an Object.
public class Items {
private final int id;
private final String image;
public Items(int id,String image){
this.name=name;
this.image=image;
}
//Include all the Getters and Setters here
}
Then assuming you've all your elements into an ArrayList<Items>, then you need to implement an Adapter class, called CustomAdaper which extends ArrayAdapter and need to write a function for getView to get a particular view to fill in your ScrollView/RecyclerView/ListView
I've found a link to help you implement a CustomAdapter here.
And finally in the MainActivity, you've to add few lines,
final ListView listView = (ListView) findViewById(R.id.list_view); //name of list view file
final ArrayList<Items> arrayList = dbHelper.retrieveFromDB(); // Name of the Database class and the function to retrieve all the elements into an ArrayList in that class
CustomAdapter adapter = new CustomAdapter(this /* The current Context */, R.layout.list_item, arrayList); // Create an object of custom adapter initialize it with the desired data (in this case the arrayList) and the layout required ( layout mentioned in the question)
listView.setAdapter(adapter);
If it is a dynamic list that can have a large number of items use a recyclerview and place that relative layout inside a cardview.
If the dynamic list has a maximum potential number of items that are also few in number, you could add the relative layout and hide them, then populate and then show them as needed, but i advice you lean toward the former.
I want to use AutoCompleteTextView in android and read the official developer.android documentation about it.
There is a code snippet which looks like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
I do not understand what the second parameter (android.R.layout.simple_dropdown_item_1line) in the constructor of ArrayAdapter means, where does this come from?
Is it a layout which is available from android or do I have to replace this layout with a layout created on my own and how to define this layout file in that case?
Concrete my code lokks like
xml:
<AutoCompleteTextView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
java:
AutoCompleteTextView search =(AutoCompleteTextView) findViewById(R.id.search);
String[] vocabs = new String[1001];
//fill the String array
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line ,vocabs);
search.setAdapter(adapter);
They are calling the constructor of ArrayAdapter with 3 arguments (documentation): ArrayAdapter(Context context, int resource, T[] objects)
The resource R.layout.simple_dropdown_item_1line is just one of the default android framework layouts for dropdowns. See here a list of other default layouts.
EDIT to answer your 2nd question:
You can either use the default android layout (the example you provided should work) or a custom one defined by you. If it's this last case then just create a xml layout for this layout:
layouts/dropdown_custom_view.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="vertical">
<TextView
android:id="#+id/vocab_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="vocab"/>
</LinearLayout>
Then you can use the ArrayAdapter constructor ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) pointing to your custom layout and to the TextView you want to be populated with vocabs:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_custom_view, R.id.vocab_text ,vocabs);
search.setAdapter(adapter);
Also check that you're populating the vocabs array well.
This layout which is available within Android System that's the reason you use android.R.It is used to show an item from the array adapter.It is basically a textview with some styling
You can use the custom layout for AutoCompleteTextView like
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text_title, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);
custom_layout.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e4e4e4"
>
<TextView
android:id="#+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
tools:text="AA"
android:padding="15dp"
/>
</LinearLayout>
Firstly, I'm extremely unsure on the majority of Android coding, this is only my second day doing it, so please excuse any mistakes I make in my explanations!
I've got a database via SQLite and currently display my data via a listView. Currently, it works fine displaying the data and I can scroll through and click on it no issues. However, I'd like to place my data within the "Scrolling Activity" template you can find in Android Studio. From Googling around, I understand somewhat that you can't place a listView in a nestedScrollView, which is what the activity uses.
I am unsure, however, how to display my data from the database without using a listView. Could somebody please help me either convert the listView into something compatible, or explain a way to combine them (Hacky methods are fine for now!)
I've displayed all of the necessary code below.
MainActivity.java:
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
obj = (ListView)findViewById(R.id.listView1);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
obj.setEmptyView(emptyText);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
content_scrolling.xml:
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
</android.support.v7.widget.LinearLayoutCompat>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/emptyText"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</android.support.v4.widget.NestedScrollView>
Apologies for any mistakes, as I say, this is my second day; I'm learning as quick as I can!
You can create views programmatically and add them to LinearLayout on the fly. You can use any kind of view, but in this example I wanted to keep things clear so I assumed that your contacts are Strings and you want to just show them as TextViews. You can try something like this:
mydb = new DBHelper(this);
List<String> contacts = mydb.getAllCotacts();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
for(String data : contacts) {
//Create a view for an LinearLayout
TextView textView = new TextView(this);
//Do whatever you like with a view - add listener, adjust style, add text etc.
textView.setOnClickListener(...);
textView.setText(data);
textView.setGravity(Gravity.CENTER);
//Add textView to linearLayout
linearLayout.addView(textView);
}
xml (not very pretty, customize it if you like :))
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/linear_layout"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>
I am making a yelp-like app in which users can leave reviews for various places, but I want other users to be able to see all of the reviews left. Currently, I am using a list to store all of my reviews in, but I can't seem to figure out how I can make a textview for each item in the list, seeing as the length of the list may vary. Also, piggy backing on this question, would there be anyway to format the textview (i.e. layoutbelow or margins)?
Thanks in advance,
Jacob
Should be able to do this with an adapter and listview:
Activity.java:
public class YourActivity extends Activity
{
ArrayList<String> reviewsArray = new ArrayList<String>();
ListView reviewList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
reviewList = (ListView) findViewById(R.id.yourListViewId);
//fill your reviewsArray...
ArrayAdapter<String> reviewsAdapter = new ArrayAdapter<String>(context, R.layout.reviewLayout, reviewsArray);
reviewsList.setAdapter(reviewsAdapter);
}
}
activity_layout.xml: (change size of the listview here)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/yourListViewId"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
reviewLayout.xml (change the reviews padding etc here)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/gridCell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
Without seeing your code it's hard to give you an exact example, but this contains all the basics.
I'm having a problem with a custom ListView I am using in the ListActivity of my application. My problem is that all TextView items added to the ListView through an ArrayAdapter show up with a gray bar above them. I would include an image of the displayed ListView, but am unable since I do not have a reputation of 10 required by the stackoverflow site.
The layout file (index.xml) used to produce the ListView is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<ListView
android:divider="#color/mg_red"
android:dividerHeight="1sp"
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:title="text_view"
android:background="#drawable/listitemback"
android:cacheColorHint="#drawable/listitemback"
android:id="#+id/listItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10sp"
android:textColor="#color/listitemtext"
android:textSize="16sp" />
</LinearLayout>
The ListActivity code used to display the list is as follows:
public class IndexListActivity extends ListActivity
{
private ListView m_listView = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
try
{
if (MGApplication.DEBUG_BUILD)
Log.i("TMG", "IndexListActivity.onCreate");
super.onCreate(savedInstanceState);
// Get our global data object.
MGApplication mgApp = (MGApplication) getApplication();
// Set view layout
SetContentView(R.layout.index);
// Get references to our ListView and AdView objects
m_listView = (ListView) findViewById(android.R.id.list);
// Create a new ArrayAdapter object that will be used to initialize
// the underlying ListView object.
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.index,
R.id.listItem,
mgApp.m_strAZRhymeNames);
// Assign array adapter
m_listView.setAdapter(aa);
}
catch (Exception e)
{
}
return;
}
}
Any help is greatly appreciated as I am at my wits end with this issue. I think I've tried every suggestion I could find on the web and I am unable to remove the gray bar.
Thank You,
Bob
you have to set this property in test.xml file with.
<ListView
android:id="#+id/listview_middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="1"
android:background="#ffffff"
android:cacheColorHint="#android:color/transparent"
android:choiceMode="singleChoice"
android:scrollingCache="false" />
and set some property in listview object.
lv.setVerticalFadingEdgeEnabled(false);
In my side that's work perfect.