Advertisement
yo2man

Relative Layouts and Convert DIPs to PX in Java

Jun 2nd, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package com.thenewboston.allison;
  2.  
  3. import android.graphics.Color;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.widget.RelativeLayout;
  9. import android.widget.Button;
  10. import android.graphics.Color;
  11. import android.widget.EditText;
  12. import android.content.res.Resources;
  13. import android.util.TypedValue;
  14.  
  15. public class MainActivity extends ActionBarActivity {
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.  
  21.  
  22.         //Layout
  23.         RelativeLayout buckysLayout = new RelativeLayout(this); //Create Layout
  24.         buckysLayout.setBackgroundColor(Color.GREEN); //set background green
  25.  
  26.         //Button
  27.         Button redButton = new Button(this);  //Create Button
  28.         redButton.setText("Log In");
  29.         redButton.setBackgroundColor(Color.RED); //set button red
  30.  
  31.         //Username input
  32.         EditText username = new EditText(this);
  33.             //Set ID to button in order to reference it later on
  34.             redButton.setId(1);
  35.             //Set ID to username-input in order to reference it later on
  36.             username.setId(2);
  37.  
  38.             //Positioning the widgets(the red button)
  39.         RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams ( //Set container, put button in container
  40.             RelativeLayout.LayoutParams.WRAP_CONTENT,
  41.                     RelativeLayout.LayoutParams.WRAP_CONTENT
  42.             );
  43.         buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); //Center Button horizontally
  44.         buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);   //Center Button vertically
  45.  
  46.  
  47.             //Positioning the username-input(the red button)
  48.         RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams ( //Set container, put button in container
  49.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  50.                 RelativeLayout.LayoutParams.WRAP_CONTENT
  51.         );
  52.  
  53.                  //Give rules to position widgets
  54.                  usernameDetails.addRule(RelativeLayout.ABOVE, redButton.getId()); //position username-input (ABOVE, red Button)
  55.                  usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); //center username-input horizontally
  56.         usernameDetails.setMargins(0,0,0,50); //position button a little bit above "red button" with some margin space (left,top,right,bottom) as per usual
  57.  
  58.         buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); //Center Button horizontally
  59.         buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);   //Center Button vertically
  60.  
  61.  
  62.         //convert DIP to pixels. once you have the px value you can pass it into username.setWidth so it would be the same size on every single device
  63.         Resources r = getResources(); //getResources essentially gets a bunch of info about your app
  64.         int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, //converrt DIP to pixels (convert DIPs, [how many DIPS:) 200,
  65.             r.getDisplayMetrics()
  66.         );
  67.  
  68.         username.setWidth(px);
  69.  
  70.         //Add Widgets to Layout
  71.         buckysLayout.addView(redButton, buttonDetails); //add Button to Layout (Button is now Child of Layout), add button Details
  72.         buckysLayout.addView(username, usernameDetails); //add Username=input to Layout
  73.  
  74.         //Set this activities content/display to this view
  75.         setContentView(buckysLayout);
  76.     }
  77.  
  78.     @Override
  79.     public boolean onCreateOptionsMenu(Menu menu) {
  80.         // Inflate the menu; this adds items to the action bar if it is present.
  81.         getMenuInflater().inflate(R.menu.menu_main, menu);
  82.         return true;
  83.     }
  84.  
  85.     @Override
  86.     public boolean onOptionsItemSelected(MenuItem item) {
  87.         // Handle action bar item clicks here. The action bar will
  88.         // automatically handle clicks on the Home/Up button, so long
  89.         // as you specify a parent activity in AndroidManifest.xml.
  90.         int id = item.getItemId();
  91.  
  92.         //noinspection SimplifiableIfStatement
  93.         if (id == R.id.action_settings) {
  94.             return true;
  95.         }
  96.  
  97.         return super.onOptionsItemSelected(item);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement