I have this problem where i can not show images (bitmap) from my database in a list on my android studio application.
I can make a picture and save it (base64) in my database and retrieve it.
But i cannot figure out how to show al the images on a display.
I tried to do it with a list adaptor but that doesnt work :/
ArrayList<Bitmap> listData = new ArrayList<>();
while(data.moveToNext()){
//get the value from the database in column 1
//then add it to the ArrayList
byte [] encodeByte =Base64.decode(data.getString(1),Base64.DEFAULT);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap =BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
listData.add(bitmap);
}
This is the shortcut way to show multiple view depending on the size of listData.
First add LinearLayout in your_current_activity.xml.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
</LinearLayout>
In java class
LinearLayout linearLayout = findViewById(R.id.linearLayout);
ArrayList<Bitmap> listData = new ArrayList<>();
for (Bitmap a : listData) {
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
image.setMaxHeight(20);
image.setMaxWidth(20);
image.setImageBitmap(a);
linearLayout.addView(image);
}
Below is my output (Assume I have 5 images in listData)
Related
I have successfully created a listview that contains all the options (text). However I would like to add unique icons next to each of the options. How can I go about doing this, with my excising code?
Here is what I am trying to achieve:
Here is my code:
AccountSettingsActivity.java
//All Options in Account Settings
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options);
listView.setAdapter((adapter));
}
listview_row_adjustmnet.xml (This simply changes the text color and size of the options in the listview)
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listViewAdjustment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textSize="17sp"
android:gravity="fill"
android:textColor="#color/white"/>
Create Array in /res folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="images">
<item>#drawable/img1</item>
<item>#drawable/img2</item>
<item>#drawable/img3</item>
</array>
</resources>
update Account Setting:
//All Options in Account Settings
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
TypedArray images = getResources().obtainTypedArray(R.array.images);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
// add images to adapter
ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options, images);
listView.setAdapter((adapter));
}
Add Image View in listview_row_adjustmnet.xml
in Adapter set the image:
imageView.setImageResource(images.getResourceId(i, -1));
Use a data model with title and icon like below
class Data {
public int imageId;
public String txt;
Data(Drawable imageId, String text) {
this.imageId = imageId;
this.txt = text;
}
}
Data menuItemSearch = new Data(ContextCompat.getDrawable(this, R.drawable.ic_search_orange), resources.getString(R.string.title))
and when set drawable in image use this one
yourImage.setImageDrawable(data.imageId)
Firstly, You have to add in your listview_row_adjustmnet.xml.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:id="#+id/listViewAdjustment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textSize="17sp"
android:gravity="fill"
android:textColor="#color/white"/>
And for Code AccountSettingActivity class:
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
ArrayList<ImageView> images = new ArrayList<>();
//Please add Customized addapter
}
for the customized adapter, you find help in this Link https://www.youtube.com/watch?v=_YF6ocdPaBg.
To set an ImageView in code Please see this answer Using variable to change image in Java.
I'm trying to create a set of 4 dynamically updating listviews of icons.
To do this, I've put my 4 listviews inside a gridview, and I'm using a custom layout for each listview item with a single imageview inside it. I'm handling changing the icon inside a custom adapter. I'm using the Picasso library to handle the icons, and all my icons are stored locally in drawable as .png files.
I'm not getting any errors, but when I run the app, all I get is a single icon that takes up the whole screen. I have a small imageview and a textview above the gridview that contains the lists of icons, but the one icon pushes even that out of the way.
This is my layout preview, and a screenshot of the app running in an emulator. (the result is the same if I run on my phone).
preview,
running
(apologies for not embedding, I'm new to SO, and don't have 10 rep yet haha)
I've tried resizing the image at runtime inside my custom adapter. I've tried setting the maximum width of the listview - both at runtime (since I want to adapt to different screen dimensions), and in the xml files. I've tried resizing the source .png files outside of android studio. Nothing's worked.
My activity xml file:
<?xml version="1.0" encoding="utf-8"?>
<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.sta.tomov0.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tomoFace"
android:src="#drawable/office38"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_below="#+id/tomoFace"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="false"
android:layout_marginBottom="50dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:rowCount="1"
android:id="#+id/gridLayout"
android:layout_below="#+id/textView"
android:columnCount="5">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewA"
android:layout_row="0"
android:layout_column="0" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewB"
android:layout_row="0"
android:layout_column="1" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewC"
android:layout_row="0"
android:layout_column="2" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewD"
android:layout_column="3"
android:layout_row="0" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="4"
android:layout_row="0"
android:onClick="addTask"
android:src="#android:drawable/stat_notify_more" />
</GridLayout>
</RelativeLayout>
My listview layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
and my activity.java file:
public class MainActivity extends AppCompatActivity {
ImageView tomoface;
ListView listViewA;
ArrayList arrayListA = new ArrayList<Integer>();
Boolean aExists = false;
ListView listViewB;
ArrayList arrayListB = new ArrayList<Integer>();
Boolean bExists = false;
ListView listViewC;
ArrayList arrayListC = new ArrayList<Integer>();
Boolean cExists = false;
ListView listViewD;
ArrayList arrayListD = new ArrayList<Integer>();
Boolean dExists = false;
Button addButton;
TextView tomoText;
File tomoFiles;
File taskFiles;
String currentCommunity = "Dep";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paper.init(getApplicationContext());
tomoface = (ImageView) findViewById(R.id.tomoFace);
tomoface.setImageResource(R.drawable.favourite15);
tomoText = (TextView) findViewById(R.id.textView);
listViewA = (ListView) findViewById(R.id.listViewA);
listViewB = (ListView) findViewById(R.id.listViewB);
listViewC = (ListView) findViewById(R.id.listViewC);
listViewD = (ListView) findViewById(R.id.listViewD);
tomoFiles = getDir("TomoFiles", MODE_PRIVATE);
taskFiles = new File(tomoFiles, "taskFiles");
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
ViewGroup.LayoutParams params = listViewA.getLayoutParams();
params.height = width;
params.width = width;
listViewA.setLayoutParams(params);
listViewB.setLayoutParams(params);
listViewC.setLayoutParams(params);
listViewD.setLayoutParams(params);
/*
If there is no data saved, the following code creates a list of (empty) lists to hold TaskClass objects
The 4 sub lists each represent one category.
If there is data saved, the following code retrieves that data, and creates 4 new ArrayLists,
each containing the iconIds of the TaskClass objects in the corresponding position of the corresponding TaskClass arraylist.
These ArrayLists of ids are then used to populate the 4 listviews.
*/
ArrayList<ArrayList<TaskClass>> listList = Paper.book(currentCommunity).read("listList", new ArrayList<ArrayList<TaskClass>>());
if (listList.size() == 0){
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
} else {
for(TaskClass t:listList.get(0)){
arrayListA.add(t.getIcon());
}
for(TaskClass t:listList.get(1)){
arrayListB.add(t.getIcon());
}
for(TaskClass t:listList.get(2)){
arrayListC.add(t.getIcon());
}
for(TaskClass t:listList.get(3)){
arrayListD.add(t.getIcon());
}
}
Integer[] intArrayA = new Integer[arrayListA.size()];
for (int i = 0; i < arrayListA.size(); i++){
intArrayA[i] =(Integer) arrayListA.get(i);
}
ArrayAdapter adapterA = new CustomArrayAdapter(getApplicationContext(),intArrayA);
listViewA.setAdapter(adapterA);
Integer[] intArrayB = new Integer[arrayListB.size()];
for (int i = 0; i < arrayListB.size(); i++){
intArrayB[i] =(Integer) arrayListB.get(i);
}
ArrayAdapter adapterB = new CustomArrayAdapter(getApplicationContext(),intArrayB);
listViewB.setAdapter(adapterB);
Integer[] intArrayC = new Integer[arrayListC.size()];
for (int i = 0; i < arrayListC.size(); i++){
intArrayC[i] =(Integer) arrayListC.get(i);
}
ArrayAdapter adapterC = new CustomArrayAdapter(getApplicationContext(),intArrayC);
listViewC.setAdapter(adapterC);
Integer[] intArrayD = new Integer[arrayListD.size()];
for (int i = 0; i < arrayListD.size(); i++){
intArrayD[i] =(Integer) arrayListD.get(i);
}
ArrayAdapter adapterD = new CustomArrayAdapter(getApplicationContext(),intArrayD);
listViewD.setAdapter(adapterD);
}
//TODO: create a custom adapter that will display icons correctly
public class CustomArrayAdapter extends ArrayAdapter{
private final Context context;
private final Integer[] values;
public CustomArrayAdapter(Context context, Integer[] values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View iconView = inflater.inflate(R.layout.iconlayout, parent, false);
ImageView imageView = (ImageView) iconView.findViewById(R.id.listImageView);
int s =(int) values[position];
imageView.setImageResource(s);
//get the device width, to calculate icon width, and then set icon width accordingly
//TODO: setting icon width not working
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
Uri uri = Uri.parse("android.resource://com.example.sta.tomov0/drawable/"+s);
Picasso.with(context).load(uri).resize(width,width).fit().centerCrop().into(imageView);
return iconView;
}
}
}
A note: I have a custom data management class that returns arraylists. That's tested separately and working fine. The arraylists that are being fed to the adapter are int arraylists of the drawable references.
Help! What am I doing wrong? I've been searching through SO all day and trying different things. The solutions posted in these questions haven't helped T-T:
How to set ImageView width in android ListView?
ImageView in ListView that expands maximum width
(that was a bit difficult to implement, and just created other problems - it seems like overkill to create a custom view class just for displaying an icon?)
Give fixed height to your Imageview in My listview layout:
Say Something like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
I'm assuming #drawable/office38 is the image shown in your screenshot (taking up the whole screen)?
Try setting the dimensions of this image to something small, eg:
android:layout_width="50dp"
android:layout_height="50dp"
I know this may not be what you are actually looking to do, but at least it will show you if the problem is with that first ImageView or not and will free up space below it for the TextView and the GridLayout. It could be that the drawable is massive, and as you aren't specifying a set width/height or any scale options, it takes up all the space it can.
Also, have a look at http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType which describes the different scaling options you have with an ImageView.
GOT IT.
sorry guys. It was a really dumb problem after all.
My listview adapter is working fine it turns out. The problem was in the imageview that was outside of the lists (tomoface) - for some reason, I decided to set it to a different drawable on runtime, and hadn't scaled that at all haha.
imageview on emulator is show but in real device is not, i following tutorial from androidhive
myjava.java
url_get_photo = "http://www.myurl.com/"
String key = getIntent().getStringExtra("key");
int loader = R.drawable.photo_blank;
ImageView image = (ImageView) findViewById(R.id.img_photo);
String image_url = (url_get_photo + key + ".jpg");
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);
myactivity.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/img_photo"
android:layout_width="150dp"
android:layout_height="150dp" />
.......
i don't know why. What is going wrong? Thanks in advance.
Check the URL of your image and Internet Permission.
check your code
int loader = R.drawable.loader;
// Imageview to show
ImageView image = (ImageView) findViewById(R.id.image);
// Image url
String image_url = "http://api.androidhive.info/images/sample.jpg";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, image);
I recommend you use a library like Picasso, Glide, Fresco, ImageRequest(Volley) that has many features can facilitate you this task.
Picasso.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);
Glide.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);
do not forget to import the library in your proyect.
Look this link http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
I'm trying to add Imageviews dynamically and
I would like to create this kind of layout, with ImageViews overlapping, for
my android application.
I don't know how to set the overlapping in java code. I want to use java code becouse I'm adding Imageview dynamically!
this is the code that I did:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
and this is my java code:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mImageView = new ImageView (this);
mImageView.setId(1);
name = "0" + ".jpg";
Bitmap bitmap = BitmapFactory.decodeFile(url_image+name);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_START,1);
params1.addRule(RelativeLayout.ALIGN_TOP,1);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
for (int i = 1;i < num_max; i++){
mImageView = new ImageView (this);
mImageView.setId(i+1);
name = String.valueOf(i) + ".jpg";
int id = mImageView.getId() - 1 ;
bitmap = BitmapFactory.decodeFile(url_image+name);
mImageView.setImageBitmap(bitmap);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params2.addRule(RelativeLayout.BELOW,id);
params2.addRule(RelativeLayout.ALIGN_START,id);
params2.addRule(RelativeLayout.ALIGN_LEFT,id);
layout.addView(mImageView,params2);
}
If you want to images overlapping each other(as i understood from your question) you should remove this line :
params2.addRule(RelativeLayout.BELOW,id);
If you wanted something else feel free to post comment below this answer
Use relative layout for it
use this code
<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" >
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/iv"
/>
</RelativeLayout>
Following is the example of how to achieve it.
MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
//Add first image view
ImageView mImageView = new ImageView (this);
mImageView.setId(1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.color1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(300, 300);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
//Add second image view
ImageView mImageView2 = new ImageView (this);
mImageView2.setId(2);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.color2);
mImageView2.setImageBitmap(bitmap2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(300,300);
params2.setMargins(150, 150, 0, 0);
layout.addView(mImageView2,params2);
}
}
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="com.example.sample.MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
</RelativeLayout>
Output Screenshot:
Required Images:
Ok, I am completely stuck on this. I have looked everywhere for an answer but no solutions I found did me any good. What I am trying to do is add a relative layout to a linear layout and I can do that just fine, the problem is that I can only set the height and the width of the relative layout, and can't align it below any views in the linear layout. I'm guessing this is because the relative layout isn't a child of the linear layout? Only reason I am taking the above approach is because I need to add a relative layout for every record I have in a database table. Anyway here is the code below any help would be great :)
individual_past_test.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/past_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E4EFF5" >
<TextView
android:id="#+id/candidate_name"
style="#style/past_test_candidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp" />
</RelativeLayout>
IndividualPastTest.java
public class IndividualPastTests extends RelativeLayout {
private Context mContext;
public IndividualPastTests(Context context) {
super(context);
mContext = context;
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.individual_past_test, this, true);
}
}
PastTests.java (relevant parts)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
// Not being called?
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
System.out.println(past_tests_label.getId());
past_test.setLayoutParams(past_test_view_params);
this.addContentView(past_test, past_test_view_params);
}
Ok I ended up fixing it by adding a relative layout to my xml file for past tests(which I didnt add to the question), then changed my PastTests.java as follows -
// Added this line to get the RelativeLayout I created in the xml to hold my view
pasts_tests_holder = (RelativeLayout) findViewById(R.id.past_tests_holder)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
past_test.setLayoutParams(past_test_view_params);
// Changed this line to add the past_test view to my new relative layout
past_tests_holder.addView(past_test);
Here are the relevant parts to my past_tests.xml file for reference -
<?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:id="#+id/prev_test_view"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/past_tests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/past_tests_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="227dp" >
</RelativeLayout>
</LinearLayout>
Hope this helps anyone else who is having trouble pragmatically setting views in android