Home » Android » Reusing views – how to?

Reusing views – how to?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am something new in android and I would like to be able to do two things:

I want to take a set of views already created and be able to put them in other activities from java.

Create other views from java

Answers:

I just made a public class and some methods that create and return the views

package com.javanigga.lovershaters.lovershaters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.TextPaint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

/**
 * Created by merli on 10/22/2017.
 */

public class views {

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public TextView titulox(Context context, String texto)
    {



        TextView titulo;

        titulo = new TextView(context);
        titulo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        titulo.setText(texto);
        titulo.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        titulo.setTextSize(36);
        titulo.setVisibility(View.VISIBLE);



        return titulo;
    }

    public ImageView imagen (String src, Context context) throws IOException {

        Bitmap bitmap = null;

        try {
            java.net.URL url = new java.net.URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        ImageView imagen = new ImageView(context);
        imagen.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350));
        imagen.setImageBitmap(bitmap);
        imagen.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return imagen;
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public TextView desarrollo(Context context, String texto)
    {


        TextView desarrollo;

        desarrollo = new TextView(context);
        desarrollo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        desarrollo.setText(texto);
        desarrollo.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        desarrollo.setTextSize(14);
        desarrollo.setVisibility(View.VISIBLE);




        return desarrollo;
    }

}