GridView Dynamically 2:3 layout data set - android

I am using one one layout and include 5 times on other layout. but i want to user Grid View instance of incluide layout. Here I am sharing Screen shot. please help me to make this layout in android
This is Flipboard layout. and second one is my layout.

GridView does not support this. One has to use a custom linearlayout and add views to it dynamically by setting the layout parameters.
Use the class as layout back bone
public class CompositeView extends LinearLayout {
private ImageView imageView;
private TextView textView;
public CompositeView(Context context) {
super(context);
imageView = new ImageView(context);
imageView.setClickable(true);
imageView.setPadding(3, 3, 3, 3);
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.FIT_XY);
textView = new TextView(context);
textView.setTextColor(getResources().getColor(android.R.color.white));
textView.setClickable(true);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.END);
textView.setPadding(3, 3, 3, 3);
textView.setBackgroundResource(R.drawable.strip_bg);
this.setGravity(Gravity.CENTER);
// this.setClickable(true);
this.addView(imageView);
this.addView(textView);
}
public void setText(int resid) {
textView.setText(resid);
}
public void setText(CharSequence text) {
textView.setText(text);
}
public void setIcon(int resid) {
imageView.setImageDrawable(getResources().getDrawable(resid));
}
public ImageView getImageView() {
return imageView;
}
public TextView getTextView() {
return textView;
}
}
In activity or fragment
names = new String[] { "Cloud Bingo", "New Bingo Games", "Politricks",
"Seam Group", "Regular Cloud Bingo" };
urls = new String[] { "http://www.cloudbingo.co.uk/",
"http://www.newbingogames.co.uk/",
"http://www.politricks.co.in", "http://veenagupta.in/",
"http://www.regularbingo.co.uk/" };
for (int i = 0; i < images.length; i++) {
view = new CompositeView(getActivity());
view.setText(names[i]);
// view.setOnClickListener(this);
view.setOrientation(LinearLayout.VERTICAL);
view.setIcon(images[i]);
view.setId(i); // id of whole linear view
view.getTextView().setGravity(Gravity.CENTER);
view.getImageView().setId(i); // id of just this imageview
view.getTextView().setId(i);
}
Use following logic for your purpose
for (int i = 0; i < images.length; i++) {
flag++;
if (flag % 2 == 0) {
// number is even
mainLayout.addView(compositeList.get(i));
}
else {
Log.v("in", "else");
secondaryLayout = new LinearLayout(getActivity());
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 15, 0, 15);
secondaryLayout.setLayoutParams(params);
secondaryLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout leftLayout = new LinearLayout(getActivity());
leftLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
leftLayout.addView(compositeList.get(i));
secondaryLayout.addView(leftLayout);
i++;
if (i < images.length) {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
rightLayout.addView(compositeList.get(i));
secondaryLayout.addView(rightLayout);
}
else {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
secondaryLayout.addView(rightLayout);
}
mainLayout.addView(secondaryLayout);
}
}
flag is equal to -1 initially , I show one image (with text at bottom) for even rows, two images for odd rows
Here is the layout
<?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:background="#drawable/splash_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Related

Android - How to generate onClick code for ScrollView items?

I have a scrollable layout that shows images vertically :
<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=".chatActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<LinearLayout
android:id="#+id/imageGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</RelativeLayout>
This is how I generate the view in my code :
LinearLayout imageGallery;
File[] fList = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
imageGallery = (LinearLayout) findViewById(R.id.imageGallery);
addImagesToTheGallery();
}
private void addImagesToTheGallery() {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
fList = path.listFiles();
if(fList!=null)
{
int len = fList.length;
for(int i=0; i<len; i++)
{
imageGallery.addView(getImageView(i));
}
}
}
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
return imageView;
}
Now, I want to add an onClick event for each of those images. When someone clicks on an image, I want to display some message specific to that image. So I should either be able to get the image or its position. How can I do that?
Set "imageView" onCLickListener.
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO do something
Toast.makeText(this, "item num" +
image, Toast.LENGTH_LONG).show();
}
});
return imageView;
}

I'm trying to put 2 views on top and 2 two on bottom but doesn't work

I want something like that
Linear Layout - vertical
Linear Layout - Horizontal
Frame Layout
Frame Layout
Linear Layout - horizontal
Frame Layout
Frame Layout
view1 | view2
view3 | view4
What should I do to fix this code
Code
public class MainActivity extends AppCompatActivity {
public static LinearLayout padrao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
padrao = findViewById(R.id.padrao);
videoUnico();
}
#SuppressLint("ResourceType")
private void videoUnico() {
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
padrao.removeAllViews();
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
view.setLayoutParams(ll);
ll.setMargins(16, 16, 16, 16);
padrao.addView(view, ll);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewDois();
}
});
}
private void viewDois() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);
params.setMargins(16, 16, 16, 16);
padrao.removeAllViews();
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
padrao.addView(view, params);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
padrao.addView(view2, params);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewTres();
}
});
}
private void viewTres() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);
padrao.removeAllViews();
params.setMargins(16, 16, 16, 16);
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
padrao.addView(view, params);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
padrao.addView(view2, params);
FrameLayout view3 = new FrameLayout(this);
view3.setBackgroundColor(Color.GREEN);
padrao.addView(view3, params);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewQuatro();
}
});
}
private void viewQuatro() {
padrao.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearHorizonteBaixo = new LinearLayout(this);
linearHorizonteBaixo.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
linearHorizonteBaixo.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout linearHorizonteCima = new LinearLayout(this);
linearHorizonteCima.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
linearHorizonteCima.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
linearHorizonteCima.addView(view);
linearHorizonteCima.addView(view2);
FrameLayout view3 = new FrameLayout(this);
view3.setBackgroundColor(Color.GREEN);
FrameLayout view4 = new FrameLayout(this);
view4.setBackgroundColor(Color.BLUE);
linearHorizonteBaixo.addView(view3);
linearHorizonteBaixo.addView(view4);
padrao.addView(linearHorizonteCima);
padrao.addView(linearHorizonteBaixo);
}
}
Xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/padrao"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
This is the entire class
I'm using methods to call the other layout
But my problem is when im trying to create two linear layout inside one
and also get this error
java.lang.ClassCastException:
android.widget.LinearLayout$LayoutParams
cannot be cast to android.widget.FrameLayout$LayoutParams
this code in XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="View2" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="View1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="View4" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="View3" />
</LinearLayout>
</RelativeLayout>

Resizing images into LinearLayout and HorizontalScrollView

how can i set the images which are in the HorizontalScrollView
As you can see the images are so much wide, i need them to be of normal size.
This is my code for the HorizontalScrollView xml file:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="5dp"
android:id="#+id/hscrollview"
android:layout_weight="0.2">
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
This is what i have tried programmatically.
private ImageView getImageView(final Integer image, final int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 0);
imageView.setLayoutParams(lp);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
});
imageView.setImageResource(image);
return imageView;
}
Change
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
to
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

Add multiple views in ScrollView?

I am working on an Android App. I have some problem..
Here is my app :
The pink rectangle is a ScrollView. I want to add TextView to the same ScrollView. I know I need a LinearLayout and add my Views inside this layout. After I need to add the LinearLayout to my ScrollView. Here is my ScrollView class with my methods setClassicLabel to implement TextViewand setClassicButton to implement Button in my ScrollView.
Here is my ScrollView.java :
public class MyScrollView extends ScrollView {
private JSONParser jParser;
private JSONObject contenuScrollView;
private Context context;
#SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
super(context);
this.context = context;
}
public void onCreate(Bundle savedInstanceState) throws JSONException {
}
/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, LinearLayout ll) throws JSONException {
if (listLabel.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listLabel.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
TextView tv = new TextView(context);
tv.setText(listLabel.getJSONObject(i).getString("text"));
tv.setTextSize(listLabel.getJSONObject(i).getInt("police_size"));
tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
ll.addView(tv, params);
}
this.addView(ll, paramsll);
}
}
#SuppressWarnings("deprecation")
public void setClassicButton(JSONArray listButton, LinearLayout ll) throws JSONException {
if (listButton.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listButton.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("position_y"));
int dest = listButton.getJSONObject(i).getInt("dest");
MyButton myButton = new MyButton(context, dest);
RoundRectShape rect = new RoundRectShape(
new float[] {50,50, 50,50, 50,50, 50,50},
null,
null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(Color.parseColor(listButton.getJSONObject(i).getString("color")));
myButton.setBackgroundDrawable(bg);
myButton.setTextColor(Color.parseColor(listButton.getJSONObject(i).getString("colortext")));
//BitmapDrawable bdra = new BitmapDrawable(downloadBitmap(listButton.getJSONObject(i).getString("http")));
myButton.setText(listButton.getJSONObject(i).getString("text"));
myButton.getBackground().setAlpha(30);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
MyButton button = (MyButton)view;
Intent intent = new Intent(context, ClassicView.class);
Content content = new Content(button.getDestination());
intent.putExtra("CONTENT", content);
context.startActivity(intent);
}
});
ll.addView(myButton, params);
}
this.addView(ll, paramsll);
}
}
}
MyScrollView heritates from ScrollView. The problem is the next :
When I create my ScrollView with the method setClassicScrollView, i call my methods setClassicLabel and setClassicButton to implements theses two views in my ScrollView. But i can only set Button or Label. I can't set one label and one button. I must have a problem who comes from a layout.. I am on it for two days
Here is my method setClassicScrollView :
public void setClassicScrollView(JSONArray listScrollView, RelativeLayout rl) throws JSONException {
if (listScrollView.length() > 0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
for (int i = 0; i < listScrollView.length(); i++) {
MyScrollView myScrollView = new MyScrollView(this, listScrollView);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("position_y"));
myScrollView.setBackgroundColor(Color.RED);
myScrollView.setLayoutParams(params);
myScrollView.setHorizontalScrollBarEnabled(true);
myScrollView.getMaxScrollAmount();
myScrollView.getBackground().setAlpha(50);
//myScrollView.setClassicLabel(listScrollView.getJSONObject(i).getJSONArray("Label"), ll);
myScrollView.setClassicButton(listScrollView.getJSONObject(i).getJSONArray("Button"), ll);
rl.addView(myScrollView);
}
}
}
Someone has an idea of how I can set TextViews and Buttons in the same ScrollView ?
Here's the layout for your scroll view.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
Here.. In the scroll view, I have one linear layout as one child and after that I have other views inside the linear layout.

Android dynamic RelativeLayout

My layout.xml file looks like this:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
I use the following code to create TextViews and Buttons inside a for loop:
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);
}
I'm getting the Button below the TextView. Can anyone help me with replacing this LinearLayout with RelativeLayout so that I get the TextView and the Button side by side?
Modify your layout so you have a RelativeLayout instead of the LinearLayout:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>
</ScrollView>
Then try this to make the TextViews and Buttons sit on the same row:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
for (int i = 0; i < 10; i++) {
Button button = new Button(this);
button.setText("View" + i);
button.setId(1000 + i);
int width = 90;
int height = 60;
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(rlp1);
rl.addView(button);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1);
button.setLayoutParams(rlp1);
rl.addView(button);
}
}
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);
}
replace the above with below code::
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
}
try this solution::;
have a new xml file as below in res/layout of your project.
Save this as linear.xml
< LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text"
android:layout_width=" wrap_content "
android:layout_height=" wrap_content " />
<Button android:id="#+id/button"
android:layout_width="90dp"
android:layout_height="60dp" />
</ LinearLayout >
and the below code:::
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
for (int i=0 ; i<10; i++){
View view = inflater.inflate(R.layout.linear,null);
view.findViewById(R.id.text).setText("Text view" + i);
view.findViewById(R.id.text).setTextColor(Color.BLUE);
view.findViewById(R.id.button).setText(“View");
view.findViewById(R.id.button).setTextColor(Color.BLUE);
linearLayout.addView(view);
}
You are defining LinearLayout with android:orientation="vertical", so it will tend to be one below another.
Add your textview and button to individual linear layouts, with orientation as horizontal, and add this linearLayout to the original vertical LinearLayout.
Edit: Try the following:
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
View indiLyt = new View(this);
indiLyt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
indiLyt.setOrientation(LinearLayout.HORIZONTAL);
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) indiLyt).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) indiLyt).addView(button);
((LinearLayout) linearLayout).addView(indiLyt);
}

Categories

Resources