Search code examples
javaandroidandroid-activitylayoutprogrammatically-created

Creating relative layout (programmatically) with lot of fields


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>

Solution

  • You need to use RelativeLayout.LayoutParams.

    also checkout these other questions on SO,