Android create rectangle with four different colors - android

How do I create a colored Rectangle like above one. Is it possible to control the length of each colored rectangle from java code? (above figure has a single rectangle with 4 colored inside it). I've been searching for answers since a few hours & was unable to find a solution.
EDIT:
I'm using AppCompatActivity and here's content_main.xml & activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/parentis"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#a11a7d"/>
</LinearLayout>
</RelativeLayout>
activity_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
Finally java code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout parent = (LinearLayout) findViewById(R.id.parentis);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.15f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.15f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.20f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.50f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);
}

you can display colored rectangles on the screen by simply setting the backgroundcolor of imageviews:
imageview.SetBackground(R.color.mycolor);
Then you just need to put 4 of those ImageViews inside a LinearLayout and change their size programmatically.

first, you should get the screen size:
private Point getScreenSize(){
WindowManager windowManager = (WindowManager) YamsaferApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
}
then write the following code:
int width = getScreenSize().x;
int redWidth = width / 4;
int orangeWidth = width / 4;
int greenWidth = width / 4;
int cyanWidth = width / 4;
LinearLayout container = new LinearLayout(getContext());
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT,70);
container.setLayoutParams(params);
// create the red area:
View redView = new View(getContext());
redView.setBackgroundColor(Color.RED);
redView.setLayoutParams(new LinearLayout.LayoutParams(redWidth,70));
container.addView(redView);
// create the orangeArea:
View orangeView = new View(getContext());
orangeView.setBackgroundColor(Color.RED);
orangeView.setLayoutParams(new LinearLayout.LayoutParams(orangeWidth,70));
container.addView(orangeView);
//create the green area
View greenView = new View(getContext());
greenView.setBackgroundColor(Color.RED);
greenView.setLayoutParams(new LinearLayout.LayoutParams(greenWidth,70));
container.addView(greenView);
create the cyan area:
View cyanView = new View(getContext());
cyanView.setBackgroundColor(Color.RED);
cyanView.setLayoutParams(new LinearLayout.LayoutParams(cyanWidth,70));
cyanainer.addView(cyanView);

If you would like to have a simpler layout with just one view and be able to change the sizes of the rectangles dynamically you can create a simple custom view. Override the onDraw(Canvas canvas) method and inside you can do something like this:
int rectWidth = getWidth() / 4;
canvas.drawRect(0, 0, rectWidth, getHeight(), redPaint);
canvas.drawRect(rectWidth, 0, 2 * rectWidth, getHeight(), orangePaint);
canvas.drawRect(2 * rectWidth, 0, 3 * rectWidth, getHeight(), greenPaint);
canvas.drawRect(3 * rectWidth, 0, getWidth(), getHeight(), pinkPaint);
You can create paints like this:
redPaint = new Paint();
redPaint.setColor(*Your red color here*);
If you need the black border around the smaller rectangles you can just add some margin to the drawRect(...) calls above and add canvas.drawARGB(255,0,0,0) above them. For round rectangles you can use the drawRoundRect(...) methods.

use this for same size rectangle .
rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
and this different size rectangle....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="80dp"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="50dp"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="90dp"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
output like that .......
Note:-set id to each one and change its size using LayoutPrams....

Take a linear layout with android:weightsum="1" .Then create 4 different views inside the linear layout with android:weight=".25"
Set your desired color for each views.
Do not forget to set the orientation of linear layout as horizontal
Sample code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
</LinearLayout>
This will distribute each color equally.If you not want to distribute each color equally then you can redistribute weight for each views.
To set the view length consisting each color:
First create a blank linear layout in your xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/ll_parent"
android:orientation="horizontal">
</LinearLayout>
Then in the java file:
LinearLayout parent=(LinearLayout)findViewById(R.id.ll_parent)‌​;
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.25f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.25f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.25f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.25f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);

Related

how to add close button on the top-center of BottomSheetDialog android?

want to add close button on BottomSheetDialog to close it.
I'm using with custom view(Linearlayout) for BottomSheetDialog.
filterDialog = new BottomSheetDialog(context);
filterDialog.setContentView(rootView);
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
below image is my filterDialog:
And this is what I want to achieve:
Any help would be greatly appreciated.
I have solved this by using Dialog(full screen) with custom(bottom) animations.
Design:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ImageView
android:id="#+id/img_close"
android:layout_width="#dimen/fifty_dp"
android:layout_height="#dimen/thirty_dp"
android:src="#drawable/ic_close_filter"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<!-- All your required widgets here-->
</LinearLayout> </LinearLayout>
Java Code:
Dialog filterDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_filter, null);
final ImageView close = (ImageView) rootView.findViewById(R.id.img_close);
builder.setView(rootView);
filterDialog = builder.create();
WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
Window window = filterDialog.getWindow();
filterDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
lp2.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp2.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp2);
Window dialogWindow = filterDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.BOTTOM );
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
You can do it as -
<!--you bottom sheet linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Layout contains button-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#android:color/transparent"
android:clickable="false"
>
<!--Your button-->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable"
android:background="#android:color/transparent"
android:clickable="true"/>
</LinearLayout>
<!--put Your other views in below linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Note : clickable false is very important point. Doing this you can access view behind your linear layout. Also set background color to transparent

Need Help adding programmatically adding ToggleButton to a FrameLayout with half the layout_weight

My goal is to dynamically add a toggleButton to a frameLayout (which has it's own weight), but have the button be half the width of the frameLayout. I am able to create what I want in XML, but I'm having trouble trying to make this work programmatically.
Here's my XML:
<?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">
<LinearLayout
android:id="#+id/columns"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="8">
<FrameLayout
android:id="#+id/column_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#android:color/background_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttonLayoutWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<ToggleButton
android:layout_width="0dp"
android:layout_height="360dp"
android:layout_weight="1"
android:checked="true"
android:text=""
android:textOff=""
android:textOn=""/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
Please pay particular attention to the LinearLayout with the id: buttonLayoutWrapper and it's child ToggleButton as those are what I need to add dynamically. For simplicity sake I'm only showing one column, but would repeat this for 7 more.
I tried creating a separate class that Extends LinearLayout and adds the ToggleButton thinking that I could get the weight that way, but no luck.
No matter what I do, I can't seem to get the ToggleButton to obey the layout_weight.
Below is a sample of trying to do this programmatically:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1);
ToggleButton toggleButton = new ToggleButton(this);
LinearLayout.LayoutParams layoutWrapper = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 360, .5f);
toggleButton.setChecked(true);
toggleButton.setText("");
toggleButton.setTextOn("");
toggleButton.setTextOff("");
toggleButton.setLayoutParams(layoutWrapper);
frameLayout.addView(toggleButton);
}
It seems that I need to have a parent LinearLayout that sets weightSum for the layout weight to work in the XML version. However, I can't seem to figure out how to add a LinearLayout parent to this that can then be added to the FrameLayout without getting a strange:
cannot cast LinearLayout to FrameLayout
error when compiling.
Rewrite
Try the following. It adds the ToggleButton to the LinearLayout then adds the LinearLayout to the FrameLayout. I think it is what you are looking for. (Comment out the LinearLayout and ToggleButton in the XML so you can see this work.)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
final float dpToPx = getResources().getDisplayMetrics().density;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linearLayout = new LinearLayout(this);
final FrameLayout.LayoutParams frameLP =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(frameLP);
linearLayout.setId(View.generateViewId()); // API 17+ needed
linearLayout.setWeightSum(2.0f);
final ToggleButton toggleButton = new ToggleButton(this);
final LinearLayout.LayoutParams linearLP =
new LinearLayout.LayoutParams(0, (int) (360 * dpToPx), 1.0f);
toggleButton.setLayoutParams(linearLP);
toggleButton.setChecked(true);
toggleButton.setText("");
toggleButton.setTextOn("");
toggleButton.setTextOff("");
linearLayout.addView(toggleButton);
final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1);
frameLayout.addView(linearLayout);
}
}

Setting layout weight to dynamically added view is not working in Android

I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.xml
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column

Android layout scaling (for elements of a given size in dp)

Three FrameLayout's of different background colors to form a strip.
Each FrameLayout has the size specified in dp.
[
The strip is long and does not fit on the screen.
How to scale the strip to maintain the aspect ratio and without changing the dimensions dp?
[
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:rotation="0">
<FrameLayout
android:layout_width="200dp"
android:layout_height="42dp"
android:background="#508da6"
/>
<FrameLayout
android:layout_width="400dp"
android:layout_height="42dp"
android:background="#494949"
/>
<FrameLayout
android:layout_width="240dp"
android:layout_height="42dp"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
There is no need to inflate three whole FrameLayout just for that.View will do just fine. Also you should not give fixed widths since there is no way to make it work for all screen sizes.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:id="#+id/strip"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layoutDirection="ltr"
android:rotation="0">
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#508da6"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#494949"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
set height on runtime
LinearLayout stripLayout = (LinearLayout)findVieById(R.id.strip);
stripLayout .getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
// TODO Auto-generated method stub
int width = stripLayout .getWidth();
int height = //calculate your height//
FrameLayout.LayoutParams params = stripLayout .getLayoutParams();
params.height = height;
stripLayout .setLayoutParams(params);
if (Build.VERSION.SDK_INT < 16) { stripLayout .getViewTreeObserver().removeGlobalOnLayoutListener(this); }
else { stripLayout .getViewTreeObserver().removeOnGlobalLayoutListener(this); }
}
});

How to set layout margin dynamically on android

my app contains 3 imageviews and i used a textview as a place holder,here is my layout.xml
<LinearLayout 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"
android:orientation="vertical"
android:background="#layout/back"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SplasActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" " />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_sher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_shaer" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/list_fav" />
then i used below codes to change image sizes,it works fine but when i set margins for my textview, everything goes crazy. textview stays but i cant see any imageview !
private void setsize(){
// this codes work fine //
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w=dm.widthPixels;
int h=dm.heightPixels;
int h2=(h/7);
int w2=w-((w*10)/100);
int h3=(h/20)+1;
LinearLayout.LayoutParams pram=new LinearLayout.LayoutParams(w2,h2);
img1.setLayoutParams(pram);
img2.setLayoutParams(pram);
img3.setLayoutParams(pram);
// the problem is here //
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, h3, 0, 0);
ll.addView(txt1,layoutParams);
}
The second parameter of method LayoutParams() should be WRAP_CONTENT, represent height of the layout, and MATCH_PARENT means that the view wants to be as big as its parent, that cause your images disappear:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

Categories

Resources