How to add view programmatically - java

I would like to do this vertical line programmatically
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
This is how I create the view:
View view = new View(getActivity());
How can I add the width, height and background parameters?

Try this
View v = new View(activityContext);
v.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT), GetPixel(1, activityContext));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
and
// To convert pixels to dp units
public int GetPixel(float f , Context context)
{
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int)((f * displayMetrics.density) + 0.5f);
}

you can also use this:
View mView = new View(Context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myLInearLayout.setOrientation(LinearLayout.VERTICAL);
mView .setLayoutParams(params);
mView.setBackgroundResource(android.R.color.white);
myLInearLayout.addView(mView);

Heres an example :
View mView = new View(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mView.setBackgroundResource(android.R.color.black);
mRelativeLayout.addView(mView, params);

Let me explain it with example.
Here's xml file which contains LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll" android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
Your JAVA file.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
float dpi = 0;
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dpi = (float) (dm.densityDpi / 160.0);
LinearLayout linear = new LinearLayout(
StackOverflowAnswerDemoActivity.this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
(int) (1 * dpi));
linear.setBackgroundColor(Color.WHITE);
linear.setLayoutParams(params);
ll.addView(linear);
}

The best option if you already have an XML file is not to make changes in code but to use LayoutInflater

Related

Android textField fill remaining height of parent

I'm really new to android and I'm trying to do very basic stuff ..
I would like to have a layout which contains 2 textFields
The first one should be 100 dp height
The second one should fill the remaining height of the layout
This is what I have done for now :
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
But I don't how to say that the second one should be juste after the first one and until the the bottom of the layout.
What should I do ?
Thanks
Don't use relative layout for such task. It's slower than LinearLayout and can be easily done with LinearLayout. Use layoutWeight, like in the example below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="100dp" android:layout_width="match_parent"
/>
<TextView
android:layout_height="0dp" android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
In order to set layout_weight from code, do the following:
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(100));
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
setContentView(linearLayout);
}
First of all you should use linear layout for this task.But if you want to put it in RelativeLayout then the solution is below
1-You should set you second textView's height to match_parent
2-RelativeLayout.END_OF will align you layout to right side of textview1.So use RelativeLayout.BOTTOM
3-RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,100); will set your textview's heght to 100 pixels (Pixel size varies with devices).So set height in dp like this
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
TRY this-
//density of device
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
// RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
RelativeLayout.LayoutParams lyp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
lyp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
RelativeLayout.LayoutParams lyp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lyp2.addRule(RelativeLayout.BOTTOM, textView1.getId());
relativeLayout.addView(imageView1, lyp2);
mainLinearLayout.addView(relativeLayout);

How can I create a layout with imageviews overlapping?

I'm trying to add Imageviews dynamically and
I would like to create this kind of layout, with ImageViews overlapping, for
my android application.
I don't know how to set the overlapping in java code. I want to use java code becouse I'm adding Imageview dynamically!
this is the code that I did:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
and this is my java code:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mImageView = new ImageView (this);
mImageView.setId(1);
name = "0" + ".jpg";
Bitmap bitmap = BitmapFactory.decodeFile(url_image+name);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,1);
params1.addRule(RelativeLayout.ALIGN_PARENT_START,1);
params1.addRule(RelativeLayout.ALIGN_TOP,1);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
for (int i = 1;i < num_max; i++){
mImageView = new ImageView (this);
mImageView.setId(i+1);
name = String.valueOf(i) + ".jpg";
int id = mImageView.getId() - 1 ;
bitmap = BitmapFactory.decodeFile(url_image+name);
mImageView.setImageBitmap(bitmap);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params2.addRule(RelativeLayout.BELOW,id);
params2.addRule(RelativeLayout.ALIGN_START,id);
params2.addRule(RelativeLayout.ALIGN_LEFT,id);
layout.addView(mImageView,params2);
}
If you want to images overlapping each other(as i understood from your question) you should remove this line :
params2.addRule(RelativeLayout.BELOW,id);
If you wanted something else feel free to post comment below this answer
Use relative layout for it
use this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/iv"
/>
</RelativeLayout>
Following is the example of how to achieve it.
MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
//Add first image view
ImageView mImageView = new ImageView (this);
mImageView.setId(1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.color1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(300, 300);
mImageView.setImageBitmap(bitmap);
layout.addView(mImageView,params1);
//Add second image view
ImageView mImageView2 = new ImageView (this);
mImageView2.setId(2);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.color2);
mImageView2.setImageBitmap(bitmap2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(300,300);
params2.setMargins(150, 150, 0, 0);
layout.addView(mImageView2,params2);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sample.MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frame1">
</RelativeLayout>
</RelativeLayout>
Output Screenshot:
Required Images:

Android set margin of view programmatically

I wanted to set margin to View programmatically. Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<ListView
android:id="#+id/attendeelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#F6CECE" >
</ListView>
</LinearLayout>
And the method when my button onClick to call out the popup Window view:
private void openPopUp() {
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.event_attendee_pop,
null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 0);
popupView.setLayoutParams(params);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, 350);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
mAdapter = new ListAdapter(getActivity());
attendeeListView.setAdapter(mAdapter);
}
I tried to set margin to the popupView but no luck. The margin is not there. I wonder which part override it.
Manually set width and height of PopupWindow :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
popupWindow.setWidth(width-10);
popupWindow.setHeight(height-10);
try to use layout it will work
LinearLayout.LayoutParams params = new LayoutParams
(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
follow this tutorial http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams

android Layout_gravity (not gravity) and Layout_marginTop(not marginTop) programmatically

I need to programmatically create a Toast that use an ImageView and a TextView, and that appears in the middle of the screen, and I have done it.
THIS IS THE RENDER I WANT
(The black square with the heart is the ImageView image, and the "J'aime" is the TextView)
XML Code :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/toastImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:src="#drawable/like" />
<TextView
android:id="#+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="J'aime !"
android:textColor="#FFFFFF" />
</FrameLayout>
THIS IS THE RENDER I HAVE with JAVA code :
JAVA CODE :
FrameLayout toastLayout = new FrameLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
I have tried with a relativelayout or a linearlayout, but the framelayout works better for this.
So I have a question :
What is the JAVA equivalent of :
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
? Because this is visibly not :
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
Need help.
Try this:
RelativeLayout toastLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
RelativeLayout.LayoutParams toastTextLp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastTextLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
Try this...
LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsOfAnim.gravity = Gravity.center_horizontal;
paramsOfAnim.topMargin = 85;
Let's take that A contains B and B contains C. You set B.layoutParams for describing the placement of B in the A, not for placement of items inside B, such as C.
The other thing is that layout parameters are complex enough, it is difficult not to forget something, and you don't need really to set all parameters by hand. So, it is better to read them, change and set back.
As here.

How to write this xml in java code

Android XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_height="28dp" android:layout_width="180dp"
android:layout_toRightOf="#+id/signinemailtxt"
android:paddingLeft="50dp"
android:layout_marginTop="65dp"
android:background="#ffffff"
/>
Java:
layout= new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView Txt=new TextView(this);
Txt.setText("Name");
How to get layout_marginTop,layout_toRightOf options in java code
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
For layout_margin :
LayoutParams params = new LayoutParams(context, attrs);
params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)
Txt.setLayoutParams(params);
Don't declare new instances of layout and text view, but get the ones you created in the xml:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
TextView Txt = (TextView) findViewById(R.id.TextViewId);
Now you can edit the layout and textview you are seeing on the screen.

Categories