Lazy
Lazy

Reputation: 1837

Android custom view drawing twice

In my activity A, this view called twice, but in my activity B, there is no problem.

Activity A is very simple layout with a few linearLayout. I'm about to go crazy, what can be the problem?

Here is I have my AdBannerView:

public class AdBannerView extends LinearLayout {

    public ImageView adIcon, adInstall;
    public TextView_ adTitle, adDesc;
    public ProgressBar adProgress;
    RelativeLayout adWrapperLay;
    private boolean impSent, adLoaded = false;

    public AdBannerView(Context context) {
        super(context);
    }

    public AdBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AdBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View wrapper = inflater.inflate(R.layout.view_ad_banner, this, true);

        adIcon = (ImageView) wrapper.findViewById(R.id.adIcon);
        adInstall = (ImageView) wrapper.findViewById(R.id.adInstall);
        adTitle = (TextView_) wrapper.findViewById(R.id.adTitle);
        adDesc = (TextView_) wrapper.findViewById(R.id.adDesc);
        adProgress = (ProgressBar) wrapper.findViewById(R.id.adProgress);
        adWrapperLay = (RelativeLayout) wrapper.findViewById(R.id.adWrapperLay);

        Log.d("AdBannerView", "before loadAd()");

        if(NativeAdManager.getInstance().isAdEnabled)
            loadAd();
    }

    public void loadAd(){

        if(adLoaded)
            return;

        adLoaded = true;

        Log.d("AdBannerView", "loadAd() request");

        NativeAdManager.getInstance().getAd(getContext(), new NativeAdManager.AdListener() {
            @Override
            public void adLoaded(final NativeAdResponse.Ads[] ads) {

                /* load img */
                Picasso
                        .with(getContext())
                        .load(ads[0].adIc)
                        .into(adIcon);

                /* load title */
                adTitle.setText(""+ads[0].adTit);
                adDesc.setText(""+ads[0].adDesc);

                /* click listener */
                adInstall.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        App app = (App) getContext().getApplicationContext();
                        app.getUiController().loadUrlWithoutAdBlocker(ads[0].adClk);

                    }
                });

                /* show this layout */
                showAd(ads[0].adBeacons);

                Log.d("AdBannerView", "loaded with size = " + ads.length);

            }
        });
    }

    private void showAd(final NativeAdResponse.adBeacons[] adBeacons) {

        adProgress.setVisibility(GONE);
        adIcon.setVisibility(VISIBLE);

    }
}

I'm including to layout like this:

<.... AdBannerView match_parent etc  />

Logs that proves drawing twice:

10-29 20:28:19.219    6698-6698/pack D/AdBannerView﹕ before loadAd()
10-29 20:28:19.219    6698-6698/pack D/AdBannerView﹕ loadAd() request
10-29 20:28:19.295    6698-6698/pack D/AdBannerView﹕ before loadAd()
10-29 20:28:19.295    6698-6698/pack D/AdBannerView﹕ loadAd() request
10-29 20:28:19.636    6698-6698/pack D/AdBannerView﹕ loaded with size = 1
10-29 20:28:19.852    6698-6698/pack D/AdBannerView﹕ loaded with size = 1

Problematic activity A:

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


    <package.AdManager.AdBannerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        />


</RelativeLayout>

Activity A.java (I deleted everything in layout and class except of AdBannerView but still same):

package package.Activity;

public class NewsRead extends Base {

    ToolBarView toolBarView;
    RelativeLayout backgroundLayForMainBgColor;

    ImageView imageView;
    TextView_ titleText, contentText, sourceText;
    LinearLayout wrapperLay /* for homeViewRowBg */, relatedNewsLay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());

        /*
        this.toolBarView = (ToolBarView) findViewById(R.id.toolBarView);
        this.backgroundLayForMainBgColor = (RelativeLayout) findViewById(R.id.backgroundLayForMainBgColor);

        this.imageView = (ImageView) findViewById(R.id.imageView);
        this.titleText = (TextView_) findViewById(R.id.titleText);
        this.contentText = (TextView_) findViewById(R.id.contentText);
        this.sourceText = (TextView_) findViewById(R.id.sourceText);
        this.wrapperLay = (LinearLayout) findViewById(R.id.wrapperLay);

        changeTheme();

        toolBarView.hideDeleteButton().setToolBarClickListener(new ToolBarView.ToolBarClickListener() {
            @Override
            public void backButtonClick() {
                finish();
            }

            @Override
            public void deleteButtonClick() {

            }

        });

        Intent intent = getIntent();

        if(intent == null)
            return;

        loadNewsDetail(intent);

        */

    }

    private void loadNewsDetail(Intent intent) {
        String neTi = intent.getStringExtra("neTi");
        String neCo = intent.getStringExtra("neCo");
        String neSi = intent.getStringExtra("neSi");
        String neIm = intent.getStringExtra("neIm");
        String neUr = intent.getStringExtra("neUr");

        /**/

        Picasso
              .with(this)
              .load(neIm)
               //.placeholder(R.drawable.icon_placeholder)
              .into(imageView);

        titleText.setText(neTi);
        contentText.setText(neCo);
        sourceText.setText("Source: "+ Html.fromHtml("<u>"+neSi+"</u>"));

    }

    private void changeTheme() {
        ThemeModel curTheme = ThemeController.getInstance().getCurrentTheme();

        if(curTheme.hasBgImage()) {
            backgroundLayForMainBgColor.setBackground(curTheme.mainBgDrawable);
        } else {
            backgroundLayForMainBgColor.setBackgroundColor(Color.parseColor(ThemeController.getInstance().getCurrentTheme().mainBgColor));
        }

        wrapperLay.setBackgroundColor(Color.parseColor(curTheme.homeViewRowBg));
    }

    protected int getLayoutResourceId() {
        return R.layout.activity_news_read;
    }

    @Override
    protected void onSoftInputShown() {

    }

    @Override
    protected void onSoftInputHidden() {

    }

    @Override
    protected String getActivityName() {
        return "news_read";
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

}

Base:

public abstract class Base extends Activity {

    private boolean isKeyboardOpened;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(getLayoutResourceId());
        keyBoardListener();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                TrackingController.onActivityOpen(getActivityName());
            }
        }, 50);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected abstract int getLayoutResourceId();

    public void Toast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public void Log(String str) {
        Log.d("act_name:"+getActivityName(), str);
    }

    private void keyBoardListener(){
        final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100 ) { // 99% of the time the height diff will be due to a keyboard.

                if(isKeyboardOpened == false){
                    onSoftInputShown();
                }
                isKeyboardOpened = true;
            }else if(isKeyboardOpened == true){
                onSoftInputHidden();
                isKeyboardOpened = false;
            }
            }
        });
    }

    public String getString_(int resId){
        return getResources().getString(resId);
    }

    protected abstract void onSoftInputShown();
    protected abstract void onSoftInputHidden();

    protected abstract String getActivityName();

}

Upvotes: 1

Views: 1182

Answers (1)

kirotab
kirotab

Reputation: 1306

Here's your bug :) Hopefully

You have in your base a call to setContentView and then in your deriving class you have call to super create (which calls the setContentView [which creates the adElement]) but after that you call again setContentView(getLayoutResourceId()); (this time from your derived class which overrides the layout but even if it didn't it's calling actually the same content I imagine so that's why it looks normal :)

So the fix should be easy - remove the setContentView(getLayoutResourceId()) from your activity A because it's already called from the base activity

Upvotes: 3

Related Questions