create Load Error View - Android User Interface

Android examples for User Interface:View

Description

create Load Error View

Demo Code


import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class Main{
    //from w  w w  .  j a  v a 2 s .c  o  m
    public static View createLoadErrorView(Context context) {
        ViewGroup view = new RelativeLayout(context);
        RelativeLayout.LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);

        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;

        ImageView iv = new ImageView(context);
        int width = 100;
        int height = 100;
        RelativeLayout.LayoutParams ivParams = new LayoutParams(width,
                height);
        ivParams.addRule(RelativeLayout.CENTER_VERTICAL);
        iv.setId(android.R.id.icon);
        //iv.setImageDrawable(getImageDrawableFromAssets(context,"err.png"));

        TextView tv = new TextView(context);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        params.addRule(RelativeLayout.RIGHT_OF, android.R.id.icon);
        params.setMargins(15, 0, 0, 0);
        tv.setLayoutParams(params);
        tv.setPadding(0, 0, 0, 0);
        tv.setTextSize(16);
        tv.setTextColor(0xAA444444);
        tv.setText("error");
        tv.setId(android.R.id.title);

        int margin = (screenWidth - width - 160) / 2;
        ivParams.setMargins(margin, 0, 0, 0);
        iv.setLayoutParams(ivParams);

        view.addView(iv);
        view.addView(tv);

        return view;
    }
}

Related Tutorials