0

I am trying to create a Relative Layout with java. I would insert logo on TOP, sharing on FOOTER and some info on middle.

For now, all components are getting on FOOTER, even if I pass the parameters to the header is on top.

My java code is:

package com.clubee.vote;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ResultadoFalho extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultadofalho);

    Bitmap bitmapTop = BitmapFactory.decodeResource(getResources(),R.drawable.bkg_app);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sharing);

    RelativeLayout layoutLogo = (RelativeLayout) findViewById(R.id.ibresultadoFalho);
    RelativeLayout.LayoutParams paramsLogo = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    ImageView bkgLogo = new ImageView(this);
    bkgLogo.setLayoutParams(paramsLogo);
    bkgLogo.setImageBitmap(bitmapTop);

    layoutLogo.setGravity(Gravity.TOP| Gravity.CENTER_VERTICAL);
    layoutLogo.addView(bkgLogo);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.ibresultadoFalho);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    ImageButton sharingButton  = new ImageButton(this);
    sharingButton.setLayoutParams(params);
    sharingButton.setImageBitmap(bitmap);

    layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM);
    layout.addView(sharingButton);

    sharingButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            shareIt();
        }
    });
}

private void shareIt() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "Eu votei! E você, já opinou sobre a atual gestão da presidente do Brasil?";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Vote, Opine, Compartilhe");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Compartilhar"));
    }
}

My activity xml is simple...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ibresultadoFalho"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fffcfffa">


</RelativeLayout>
Barcat
  • 163
  • 16
  • Why do you use height wrap_content for all your elements? And for the future: try to build your layouts in the xml! It's much more clear and easier than building it from java. Usually the view elements should be build in java only when you deal with dynamic display. – Ispas Claudiu Feb 16 '15 at 14:36
  • Hello, @IspasClaudiu. I do it only to study. I am new with android dev. – Barcat Feb 16 '15 at 16:24

2 Answers2

0

You need to use RelativeLayout.LayoutParams.

also checkout these other questions on SO,

Community
  • 1
  • 1
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
  • Hello @Sufiyan Ghori, tks for response. I tried follow your tips, and modify my code, but now the return is a white screen. I will respond my question pasting the new code. – Barcat Feb 16 '15 at 15:57
0

EDITED: I type the param this.addContentView(layout, lp); and now it works. tks a lot 4 help.

After modify the first code, now the return is a white screen. I pulled out some parts of the original code, so I would see better what was doing (right or wrong).

package com.clubee.vote;

import android.app.Activity;    
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ResultadoFalho extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultadofalho);

    RelativeLayout layout = new RelativeLayout(this);

    Bitmap bitmapTop =  BitmapFactory.decodeResource(getResources(),R.drawable.bkg_app);

    ImageView bkgLogo = new ImageView(this);
    bkgLogo.setId('1');
    bkgLogo.setImageBitmap(bitmapTop);


    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(lp);

    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, bkgLogo.getId());

    layout.addView(bkgLogo);
    }
}
Barcat
  • 163
  • 16