I've tried following code to add two layouts within main layout
but this gives error while setting layout to frame and while adding it into view
public class MainActivity extends Activity {
FrameLayout rootLayout;
FrameLayout frame1,frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout=new FrameLayout(getApplicationContext());
frame1=(FrameLayout)findViewById(R.layout.frame1);
frame2=(FrameLayout)findViewById(R.layout.frame2);
LayoutParams param1=new FrameLayout.LayoutParams(180,300);
LayoutParams param2=new FrameLayout.LayoutParams(180,300);
param1.gravity=Gravity.LEFT;
param2.gravity=Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
}
}
You have to remove both frame1 and frame2 Views from their parent View...
and this may help you...
FrameLayout rootLayout;
FrameLayout frame1, frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootLayout = new FrameLayout(this);
frame1 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame1,null);
frame2 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame2, null);
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(180, 300);
FrameLayout.LayoutParams param2 = new FrameLayout.LayoutParams(180, 300);
param1.gravity = Gravity.LEFT;
param2.gravity = Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
setContentView(rootLayout);
}
Related
I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
I've create new application with Android Studio and altered onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.activity_main, new PlaceholderFragment())
.commit();
}
int width = 400;
int height = 20;
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(
width,
height
);
params.leftMargin = 20;
params.topMargin = 200;
TextView textView = new TextView(this);
textView.setText("Text Added dinamically");
textView.setLayoutParams(params);
FrameLayout activityMain = (FrameLayout) findViewById(R.id.activity_main);
activityMain.addView(textView);
}
I expecteted to see "Text Added dinamically" at 200 distance from top. Indeed it appears snappet to top. How can I fix this?
I have a RelativeLayout with 4 ImageViews that should have 2 on top, one on each side of the Layout, and 2 ImageViews below the first two, one on each side of the screen.
I have 2 problems, the two ImageViews that are supposed to be below, do not go below. And The 2 ImageViews that are aligned to be on the right of the screen should be one on top of the other but seem to be about 25dp out.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout)v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(getActivity());
imageTopL.setImageResource(R.drawable.bell_dl_256);
imageTopL.setPadding(50, 0, 0, 0);
imageTopL.setId(0);
ImageView imageBottomL = new ImageView(getActivity());
imageBottomL.setImageResource(R.drawable.bell_dl_256);
imageBottomL.setPadding(50, 0, 0, 0);
imageBottomL.setId(1);
ImageView imageBottomR = new ImageView(getActivity());
imageBottomR.setImageResource(R.drawable.bell_dr_256);
imageBottomR.setPadding(0, 0, 50, 0);
imageBottomR.setId(2);
ImageView imageTopR = new ImageView(getActivity());
imageTopR.setImageResource(R.drawable.bell_dr_256);
imageTopR.setPadding(0, 0, 50, 0);
imageTopR.setId(3);
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageTopL.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageTopR.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params = (RelativeLayout.LayoutParams)imageBottomL.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageBottomR.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.RIGHT_OF, imageBottomL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
return v;
// try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(this);
imageTopL.setId(1);
imageTopL.setImageResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageTopL.setAdjustViewBounds(true);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.setMargins(50,10,0,0);
imageTopL.setLayoutParams(params1);
imageTopL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageTopR = new ImageView(this);
imageTopR.setId(2);
imageTopR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params2.setMargins(0,10,50,0);
imageTopR.setLayoutParams(params2);
imageTopR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Right",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomL = new ImageView(this);
imageBottomL.setId(3);
imageBottomL.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params3.addRule(RelativeLayout.BELOW, imageTopL.getId());
params3.setMargins(50,10,0,0);
imageBottomL.setLayoutParams(params3);
imageBottomL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomR = new ImageView(this);
imageBottomR.setId(4);
imageBottomR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params4.addRule(RelativeLayout.BELOW, imageTopR.getId());
params4.setMargins(0,10,50,0);
imageBottomR.setLayoutParams(params4);
imageBottomR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Right",Toast.LENGTH_SHORT).show();
}
});
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
return v;
}
I have an action bar which is created programmatic-ally:
public class ActionBarPdfView extends Activity{
public ImageView action_bar_home_icon, action_bar_back_icon, action_bar_star, action_bar_search;
public RelativeLayout relative;
public ActionBar action;
public RelativeLayout.LayoutParams params;
private DatabaseManager dbManager;
public static int VIEW_ID = 3;
public int getViewId(){
return VIEW_ID;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
action_bar_home_icon = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
action_bar_home_icon.setLayoutParams(params);
action_bar_home_icon.setId(1);
action_bar_star = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
params.setMargins(0, 0, 20, 0);
action_bar_star.setLayoutParams(params);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
action = getActionBar();
relative = new RelativeLayout(getApplicationContext());
action_bar_back_icon = new ImageView(getApplicationContext());
action_bar_search = new ImageView(getApplicationContext());
dbManager = new DatabaseManager(getApplicationContext());
dbManager.waitForTurn();
ObjectsDBAdapter objects = dbManager.getObjectsDbAdapter();
objects.setupImageView(R.drawable.home_android, action_bar_home_icon);
// action_bar_home_icon.setImageResource(R.drawable.home_android);
// params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
// params.setMargins(0, 0, 20, 0);
// action_bar_back_icon.setImageResource(R.drawable.back_android);
// action_bar_back_icon.setLayoutParams(params);
// action_bar_back_icon.setId(2);
objects.setupImageView(R.drawable.sternbutton_android, action_bar_star);
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, 0, 20, 0);
objects.setupImageView(R.drawable.searchtext_android, action_bar_search);
action_bar_search.setLayoutParams(params);
action_bar_search.setId(3);
action.setDisplayShowCustomEnabled(true);
action.setDisplayShowHomeEnabled(false);
action.setDisplayShowTitleEnabled(false);
relative.addView(action_bar_home_icon);
relative.addView(action_bar_back_icon);
relative.addView(action_bar_star);
relative.addView(action_bar_search);
relative.setGravity(Gravity.CENTER);
action.setCustomView(relative);
dbManager.closeObjectsDbAdapter();
return true;
}
}
I have some icons on the left side and some icons on right side. What I want is, when there are more icons from right and they cover some icons on left side, to hide this icons in menu.
I know that in xml I can do that using android:showAsAction. I would like to know how I can do that programmatic-ally? Any ideas?
I am trying to keep two customviews and i kept fadein fadeout but i am not able to see both custom views am able to see only one custom view if i click on fadein i m able to see only one view but not both. but i am not able to see both views at a time please tell me where i did mistake.
MyView myview;
MyView1 myview1;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview=new MyView(this);
myview.setBackgroundColor(color.white);
setContentView(myview);
myview = new MyView(this);
myview.setBackgroundColor(Color.WHITE);
myview1=new MyView1(this);
myview1.setBackgroundColor(color.white);
setContentView(myview1);
myview1 = new MyView1(this);
myview1.setBackgroundColor(Color.WHITE);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(0);
relativeLayout.setBackgroundColor(Color.WHITE);
myview = new MyView(this);
myview.setId(004);
RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview.setLayoutParams(lp6);
relativeLayout.addView(myview,lp6);
myview1 = new MyView1(this);
myview1.setId(8);
RelativeLayout.LayoutParams lp008 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview1.setLayoutParams(lp008);
relativeLayout.addView(myview1,lp008);
Button button1 = new Button(this);
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
button1.setText("click");
relativeLayout.addView(button1,lp1);
button1.setOnClickListener(this);
setContentView(relativeLayout);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
protected void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(),R.drawable.apple);
canvas.drawBitmap(_scratch, 840, 450, null);
}
}
public class MyView1 extends View{
public MyView1(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Bitmap scratch=BitmapFactory.decodeResource(getResources(), R.drawable.ant);
canvas.drawBitmap(scratch, 840, 450, null);
}
}
public void onClick(View v) {
if(myview1.getVisibility() == View.VISIBLE) {
Animation out = AnimationUtils.makeOutAnimation(this, true);
myview1.startAnimation(out);
myview1.setVisibility(View.INVISIBLE);
} else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
myview1.startAnimation(in);
myview1.setVisibility(View.VISIBLE);
}
}
}
Use Views setVisibility method