I am trying to create multiple layers of linearlayouts one underneath the other but the result shows only the first linearlayout and I think that the other linearlayouts are to the right of each other rather than stacked on top of each other. Can you help?
My code is as follows:
public void actionBrowse(){
LinearLayout HolderLL = new LinearLayout(browseActivity);
LinearLayout picLL1 = new LinearLayout(browseActivity);
LinearLayout picLL2 = new LinearLayout(browseActivity);
LinearLayout picLL3 = new LinearLayout(browseActivity);
LinearLayout picLL4 = new LinearLayout(browseActivity);
LinearLayout picLL5 = new LinearLayout(browseActivity);
HolderLL.addView(picLL1);
HolderLL.addView(picLL2);
HolderLL.addView(picLL3);
HolderLL.addView(picLL4);
HolderLL.addView(picLL5);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<State.getInstance().getDesignUnit().getAllSavedImages().size(); i++){
if(i<4) {
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL1.addView(newImage, imageParams);
}
else if (i>= 4 && i <8){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL2.addView(newImage, imageParams);
}
else if( i >=8 && i<12){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL3.addView(newImage, imageParams);
}else if (i >= 12 && i<16) {
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL4.addView(newImage, imageParams);
}else if (i >=16 && i<20){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL5.addView(newImage, imageParams);
}
}
browseActivity.setContentView(HolderLL);
}
Related
I'm on working on a process for adding icons to the home menu from a icon selection window. if the user selected some icons ,in the next time user click on add icon button , the window should be appear with ticked icon .
here is my code
private void getAlreadyTicked() {
Log.d("getAlreadyTicked", "getAlreadyTicked");
//getting already ticked values for selection page
String icon_name_val = null, icon_link_val = null, icon_flag_val = null;
List<HomeIconset> icondetails = dbh.geticondetails();
for (HomeIconset sp : icondetails) {
icon_name_val = sp.getIcon_name();
icon_link_val = sp.getIcon_link();
icon_flag_val = sp.getIcon_flag();
intSelected = null;
intSelected = new Integer[19];
String[] stringIconName = {"paymoneyimg", "recieve",
"load", "buysell",
"gas", "electricity",
"mobileimg", "movie",
"buyforex", "sellforex",
"travelcard", "send",
"flight", "bus",
"hotel", "holiday",
"gold", "vehicleloan",
"personalloan", "insuranceimg"};
for (int i = 0; i < 19; i++) {
String imgName;
Drawable drawableImgName = null;
Bitmap result;
Drawable[] drawableImgNameArray = new Drawable[19];
if (icon_name_val.equals(stringIconName[i])) {
int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName());
Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
// Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
c.drawBitmap(resized, 0, 0, null);
c.drawBitmap(bitmap2, 0, 0, null);
drawableImgName = new BitmapDrawable(getResources(), resultBitmap);
result = resultBitmap;
ImageView im = new ImageView(getActivity());
//im.setTag(icon_name_val+i);
im.setImageDrawable(drawableImgName);
// String name = getActivity().getResources().getResourceEntryName(drawableImgName);
Log.d("tickedimage", String.valueOf(String.valueOf(drawableImgName)) + " ..." + im.getResources());
} else {
int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName());
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
drawableImgName = getResources().getDrawable(getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName()));
result = bitmap1;
}
// drawableImgNameArray[i]=drawableImgName;
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableImgNameArray[19]);
intSelected[i] = getResources().getIdentifier(drawableImgName, "drawable", myContext.getPackageName());
}
for (int j = 0; j < intSelected.length; j++) {
Log.d("imgarraysf", String.valueOf(intSelected[j]));
}
}
}
If I correctly understood your code and the question you have to use icon_name_val.
That is,
intSelected[i] = getResources().getIdentifier(icon_name_val, "drawable", myContext.getPackageName());
I am creating a application where I need to add imagebutton dynamatically,
under that imagebutton I want to set some text. I tried bellow posted code but I am getting exception at L.addView(btn);
Plz suggest something......
for (int i = 1; i <= SplashScreenActivity.num; i++)
{
LinearLayout L = new LinearLayout(this);
LayoutParams params_1 = new LayoutParams(241,137);
params_1.width=(int)(scaleX*241);
params_1.height=(int)(scaleY*137);
L.setLayoutParams(params_1);
ImageButton btn = new ImageButton(this);
File file= new File(Environment.getExternalStoragePublicDirectory("Android/data") + "/" + getApplicationContext().getPackageName()+"/CoverImages/skc_"+i+".jpg");
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
btn.setImageBitmap(bm);
btn.setScaleType(ScaleType.FIT_XY);
btn.setBackgroundColor(80000000);
btn.setId(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(241, 137);
layoutParams.width=(int)(scaleX*241);
layoutParams.height=(int)(scaleY*137);
layoutParams.setMargins(24, 0, 24, 0);
ll.addView(btn, layoutParams);
TextView tv = new TextView(this);
tv.setGravity(Gravity.BOTTOM);
tv.setTextColor(Color.BLUE);
name = "purchased";
tv.setId(i);
tv.setText(name);
L.setOrientation(LinearLayout.HORIZONTAL);
btn.setMaxWidth(mButtonWidth);
btn.setMaxHeight(mButtonHeight);
L.addView(btn);
L.addView(tv);
ll .addView(L);
final int id_ = btn.getId();
btn = ((ImageButton) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
utils.playTapSound();
File myDirectory = new File(Environment.getExternalStoragePublicDirectory("Android/data") + "/" + getApplicationContext().getPackageName() + "/ComicBook/comic"+MainPage.get_id+"/skc"+MainPage.get_id+".zip");
if(myDirectory.exists())
{
Intent intent = new Intent(MainPage.this,
FullScreenViewActivity.class);
startActivityForResult(intent, 0);
}
else
{
startService(new Intent(getApplicationContext(), BillingService.class));
Intent intent = new Intent(MainPage.this,InAppActivity.class);
startActivityForResult(intent, 0);
}
}
});
I have code to create view programmatically like this ,
splash.post(new Runnable() {
#Override
public void run() {
processFromDatabase();
}
});
splash is my parent view , and this is processFromDatabase Code
private void processFromDatabase() {
createLayoutBook("food_name", "80000", "food_des","", 2);
Bitmap bmp = merge();
saveToLocal2(bmp);
}
and this is my code for createLayoutBook ,
private void createLayoutBook(String food_name, String food_price,String food_des, String path, int tag) {
Log.e("create layout book", "Create layout book");
RelativeLayout root_2book = new RelativeLayout(c);
RelativeLayout.LayoutParams rlp_a = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, ((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 120, c.getResources()
.getDisplayMetrics())));
rlp_a.setMargins(0, 10, 0, 0);
root_2book.setLayoutParams(rlp_a);
root_2book.setBackgroundColor(c.getResources().getColor(R.color.krem));
RelativeLayout layout_detail_2book = new RelativeLayout(c);
RelativeLayout.LayoutParams rdet = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rdet.addRule(RelativeLayout.RIGHT_OF, layout_a_2book.getId());
rdet.setMargins(20, 3, 0, 0);
layout_detail_2book.setLayoutParams(rdet);
TextView tv_nama_2book = new TextView(c);// nama
tv_nama_2book.setId(id_book);
id_book = id_book + 1;
RelativeLayout.LayoutParams tv_nama = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv_nama_2book.setLayoutParams(tv_nama);
tv_nama_2book.setTextSize(20);
tv_nama_2book.setTypeface(Typeface.DEFAULT_BOLD);
tv_nama_2book.setText(food_name);
Log.e("food name", food_name);
getSharedPreferences("myprefs", 0).edit().putString("food_name", food_name).commit();
TextView tv_des_2book = new TextView(c); // des
RelativeLayout.LayoutParams tv_des = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv_des.addRule(RelativeLayout.BELOW, tv_nama_2book.getId());
tv_des.setMargins(0, 3, 5, 0);
tv_des_2book.setLayoutParams(tv_des);
tv_des_2book.setTextSize(12);
tv_des_2book.setTextColor(c.getResources().getColor(R.color.hitam));
tv_des_2book.setText(food_des);
Log.e("food des", food_des);
getSharedPreferences("myprefs", 0).edit().putString("food_des", food_des).commit();
layout_detail_2book.addView(tv_nama_2book);
layout_detail_2book.addView(tv_des_2book);
root_2book.addView(layout_detail_2book);
splash.addView(root_2book);
and this is code for change this layout to bitmap
public Bitmap merge() {
View v1 = splash;
v1.setDrawingCacheEnabled(true);
Bitmap merge = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
return merge;
}
But why , it return with black screen , like my layout that I created before not save or missing , any solution?? thanks
I believe what you want something like this:
public Bitmap merge() {
Bitmap bitmap = Bitmap.createBitmap(...);
Canvas canvas = new Canvas(bitmap);
splash.draw(canvas);
return bitmap;
}
I'm using the following code to make a custom listview. I have to show image dynamically, so all the images are added to this in Linearlayout. The problem is that these dynamic images add multiple times. Below is the code of my getView().
LinearLayout fbpiclayout = null;
if (convertView == null)
vi = inflater.inflate(R.layout.popular_event_list, null);
fbpiclayout = (LinearLayout) vi
.findViewById(R.id.fbpic_layout);
ArrayList<String> list= mDbManager
.getAllValuesComingSoonFriendList(facebookEventList
.get(position).getEventId());
int height = 0;
for(int i=0;i<list.size();i++)
{
if(i<3)
{
String friendPics = "http://graph.facebook.com/"
+ list.get(i)
+ "/picture?type=large";
Log.d("Friends","list no "+i+" "+mDbManager
.getFacebookFriendName(list.get(i)));
ImageView fbFriendimage = new ImageView(getActivity());
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
vp.setMargins(3, 0, 3, 3);
fbFriendimage.setLayoutParams(vp);
fbFriendimage.setAdjustViewBounds(true);
fbFriendimage.getLayoutParams().height = height;
fbFriendimage.getLayoutParams().width = width_iv;
fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
// image.setImageBitmap(bm);
imageLoader.DisplayImage(friendPics, fbFriendimage);
fbpiclayout.addView(fbFriendimage, vp);
}
}
Kindly suggest me on that issue.
Thanks in Advance.
Try the following code:
LinearLayout fbpiclayout = null;
if (convertView == null)
vi = inflater.inflate(R.layout.popular_event_list, null);
fbpiclayout = (LinearLayout) vi
.findViewById(R.id.fbpic_layout);
//if the convertView was not null it would have the child view you added the
// last time liner layout was used. So remove the added child view.
fbpiclayout.removeAllViews();
ArrayList<String> list= mDbManager
.getAllValuesComingSoonFriendList(facebookEventList
.get(position).getEventId());
int height = 0;
for(int i=0;i<list.size();i++)
{
if(i<3)
{
String friendPics = "http://graph.facebook.com/"
+ list.get(i)
+ "/picture?type=large";
Log.d("Friends","list no "+i+" "+mDbManager
.getFacebookFriendName(list.get(i)));
ImageView fbFriendimage = new ImageView(getActivity());
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
vp.setMargins(3, 0, 3, 3);
fbFriendimage.setLayoutParams(vp);
fbFriendimage.setAdjustViewBounds(true);
fbFriendimage.getLayoutParams().height = height;
fbFriendimage.getLayoutParams().width = width_iv;
fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
// image.setImageBitmap(bm);
imageLoader.DisplayImage(friendPics, fbFriendimage);
fbpiclayout.addView(fbFriendimage, vp);
}
}
EDIT1: Try to use smart image view http://loopj.com/android-smart-image-view/ to improve performance....
don't use that loop. Put that list's size in getCount(). It will work properly
image take space when i load image i don;t understand what is the problem why is take space after first iamge other image is display normallly u can see my activity java class which have imagedisplay method i am alos load image here in this image is take space before and after first image
myactivity.java
public void imagedisplay()
{
itable = (TableLayout)findViewById(R.id.itable);
String chekplace=null;
Log.e("MyList Size", "Counted--->"+mylist.size());
int size=mylist.size();
s:
for(int j=0;j<mylist.size();j++)
{
trstate = new TableRow(this);
trstate.setGravity(Gravity.CENTER_HORIZONTAL);
tstate = new TextView(this);
tstate.setGravity(Gravity.CENTER);
String tempstate = mylist.get(j).get(KEY_PLACENAME).intern().toString();
tstate.setText(tempstate);
trstate.addView(tstate);
itable.addView(trstate);
chekplacename=mylist.get(j).get(KEY_PLACENAME).intern().toString();
int counter=1;
String nextplace=chekplacename;
chekplace=nextplace;
m:
while(nextplace==chekplace)
{
trimage = new TableRow(this);
trimage.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
for (int m = 1; m <= 4; m++)
{
chekplace=mylist.get(j).get(KEY_PLACENAME).intern().toString();
if(nextplace==chekplace)
{
iimage = new ImageView(this);
iimage.setPadding(2,2,2,2);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
String loadimageurl=mylist.get(j).get(KEY_PHOTOURL).intern().toString();
counter++
bm = LoadImage(loadimageurl, bmOptions);
int height=bm.getWidth();
int width=bm.getHeight();
Log.e("newheight","Before--->" +height);
Log.e("new width","Before--->"+width);
bm=resize(bm, bm.getHeight(), bm.getWidth());
height=bm.getWidth();
width=bm.getHeight();
iimage.setLayoutParams(new TableRow.LayoutParams( height, width));
iimage.setImageBitmap(bm);
trimage.addView(iimage);
}
}
itable.addView(trimage);
}
}