Skip to content

Instantly share code, notes, and snippets.

@ankit-lakhanpal
Created August 12, 2015 18:01
Show Gist options
  • Save ankit-lakhanpal/54fa43b29f8d63f78bc1 to your computer and use it in GitHub Desktop.
Save ankit-lakhanpal/54fa43b29f8d63f78bc1 to your computer and use it in GitHub Desktop.
package com.lakhanpal.ankit.alison;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.widget.EditText;
import android.content.res.Resources;
import android.util.TypedValue;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//converting px tp dip
Resources r=getResources(); //get information about phone
int px=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,r.getDisplayMetrics());
//Button
Button redbutton= new Button(this);
RelativeLayout.LayoutParams buttonDetails= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
RelativeLayout AnkitsLayout=new RelativeLayout(this);
redbutton.setText("login");
redbutton.setBackgroundColor(Color.CYAN);
redbutton.setId(1);
//Text
EditText username = new EditText(this);
RelativeLayout.LayoutParams usernameDetails =new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
usernameDetails.addRule(RelativeLayout.ABOVE, redbutton.getId());
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
usernameDetails.setMargins(0, 0, 0, 50);
username.setWidth(px);
AnkitsLayout.setId(2);
AnkitsLayout.addView(redbutton, buttonDetails);
setContentView(AnkitsLayout);
AnkitsLayout.setBackgroundColor(Color.BLUE);
AnkitsLayout.addView(username, usernameDetails);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment