1

My application need to show images from a host url. I am adding the image to the inner layout from class file. But I'm not able to reduce the image size and as you can see it is occupying the complete screen.

Screen Out put

  import beans.MenuItem;
    import android.app.Activity;
    import android.app.ActionBar.LayoutParams;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.TextView;
         public class ImageLoading extends Activity {
                public static final String PHOTOS_BASE_URL = 
                        "http://services.hanselandpetal.com/photos/";
                final  JSONObject cartinproduct= new JSONObject();
                @Override
                protected void onCreate(Bundle icicle) {
                    setContentView(R.layout.imageloding);
                    super.onCreate(icicle);
                Button display=(Button) findViewById(R.id.button1);
                display.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        updateDisplay();
                    }
                });
                     }
                    protected boolean isOnline() {
                    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo netInfo = cm.getActiveNetworkInfo();
                    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                        return true;
                    } else {
                        return false;
                    }
                }
                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    getMenuInflater().inflate(R.menu.main, menu);
                    return true;
                }
                protected void updateDisplay() {
                    Log.v("updateDisplay", "Started");
                    setContentView(R.layout.imageloding);
                    final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
                    final MenuController Controller =  (MenuController) getApplicationContext();
                    MenuItem MenuObject = new MenuItem(); 
                    Log.v("loop", "Started");
                        for(int i=0;i<3;i++)
                        {
                        MenuObject.setMenu_name("Product "+i);
                            Controller.setProducts(MenuObject);
                        }
                        //Product arraylist size
                        int ProductsSize = Controller.getProductsArraylistSize();

                        // create the layout params that will be used to define how your
                        // button will be displayed
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        Log.v("for loop", "Started");
                        for(int j=0;j< ProductsSize;j++)
                        {   
                            // Get probuct data from product data arraylist
                            String pName = Controller.getProducts(j).getMenu_name();
                                // Create LinearLayout to view elemnts
                            LinearLayout ll = new LinearLayout(this);
                            ll.setOrientation(LinearLayout.HORIZONTAL);
                            Log.v("inner loop", "Started");
                            com.example.testapp.LoaderImageView image=new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");
                            image.setImageDrawable("http://developer.android.com/images/MENU_MNU105.jpg");
                            ll.addView(image);
                            TextView product = new TextView(this);
                            product.setText(" "+pName+"    ");
                            ll.addView(product);
                                        lm.addView(ll);
                }
                }
        }

Layout code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/display" />

     <LinearLayout
        android:id="@+id/linearMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

     <ProgressBar
         android:id="@+id/progressBar1"
         style="?android:attr/progressBarStyleLarge"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

</LinearLayout>
kramer65
  • 50,427
  • 120
  • 308
  • 488
PRK449
  • 113
  • 3
  • 11

2 Answers2

0

You can add com.example.testapp.LoaderImageView as child of your LinearLayout in XML and define it dimension, or add LinearLayout.LayoutParams Dynamically with your desire dimensions. Add this line after com.example.testapp.LoaderImageView image=new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");

int desire_width = 50;
int desire_height = 50;
image.setLayoutParams(new LinearLayout.LayoutParams(desire_width, desire_height));
yshahak
  • 4,996
  • 1
  • 31
  • 37
-1

You could resize the image via the BitmapFactory

Already answered here

And a good code snippet for this

Community
  • 1
  • 1
Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23