add a imageView below existing textview PROGRAMMATICALLY - android

How to add view programmatically to existing view, for example I have textview in XML and now I need to add imageview below that textview programmatically, here is what I tried,
HorizontalScrollView scroll = new HorizontalScrollView(getApplicationContext());
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
scroll.setLayoutParams(new ViewGroup.LayoutParams(imageLayout.getWidth(),
imageLayout.getHeight()));
scroll.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}but its not adding any imageview, I'm trying to do this in **AlertDialog**

ScrollView cannot have more than one child. You should wrap your layout inside of a LinearLayout and then after you can add any number of views into that LinearLayout. cheers :)

You are doing with wrong way.
Once check with below code snippet.
LinearLayout yourlayout = (LinearLayout)findViewById(R.id.layoutid);//Layout which is inside scrollview
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your LinearLayout below your TextView object
params.addRule(RelativeLayout.BELOW, TextView.getId());
// set the layoutParams on the LinearLayout
imageLayout.setLayoutParams(params);
yourlayout.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}

I solved it by adding
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
in XML
&
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < 10; i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher));
imageView.setScaleType(ScaleType.FIT_XY);
layout.addView(imageView);
}
in MainActivity.java

ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
params.addRule(RelativeLayout.BELOW, imageView.getId());

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;
}

how to set child view at LAYOUT_LEFT OF="+/id" by programically in relative layout

as shown in image i need to set text view LEFT OF RADIO GROUP and i did it using java code....so i don't know to set parameter for LAYOUT LEFT OF="" in java code.....how to solve it ..please guide me this way...
this is class file......
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_me_up);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
layout = (RelativeLayout) findViewById(R.id.linear);
Button btnp = (Button) findViewById(R.id.buttonp);
btnp.setOnClickListener(this);
}
#Override
public void onClick(View v) {
RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
radioGroup.setOrientation(0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (mLastRadioGroup != null)
p.addRule(RelativeLayout.BELOW, mLastRadioGroup.getId());
radioGroup.setId(mRadioGroupId++);
mLastRadioGroup = radioGroup;
layout.addView(radioGroup, p);
RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
radioButtonView.setText("radio1");
radioButtonView.setId(id++);
radioButtonView.setButtonDrawable(R.drawable.e404);
radioButtonView.setChecked(false);
radioGroup.addView(radioButtonView, p);
RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
radioButtonView2.setText("radio2");
radioButtonView2.setId(id++);
radioButtonView2.setChecked(false);
radioGroup.addView(radioButtonView2, p);
RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
radioButtonView3.setText("radio3");
radioButtonView3.setId(id++);
radioGroup.addView(radioButtonView3, p);
radioButtonView3.setChecked(false);
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
layout.addView(txt, p);
radioGroup.setOnCheckedChangeListener(this);
}
Alright you can do it by this way:
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
RelativeLayout.LayoutParams params=
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,radiofroup.getId());
txt.setLayoutParams(params);
layout.addView(txt);
Hope I answered your question. :)
addRule Do something like this..
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

How can I add images to a LinearLayout programmatically?

There is a LinearLayout and some Buttons in my project. I want when each Button clicked add a small image to LinearLayout from drawable. How can I add a image to a LinearLayout programmatically?
This is my LinearLayout in a HorizontalScrollView:
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
and here is my activity:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout notes = (LinearLayout) findViewById(R.id.LinearLayout_notes);
final ImageView notes_do = new ImageView(this);
notes_do.setBackgroundResource(R.drawable.notes_do);
new Thread(new Runnable() {
#Override
public void run() {
final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1_);
img_1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
notes.addView(notes_do);
}
return true;
}
}); // end of ontouch
}
}).start(); // end of thread
} // end of oncreate
}// end of activity
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout1);
for(int i=0;i<5;i++)
{
ImageView ii= new ImageView(this);
ii.setBackgroundResource(R.drawable.ic_action_search);
ll.addView(ii);
}
You can do a findViewById to the linearlayout and add an imageview to it dynamically in the onclicklistener of button
LinearLayout linLay = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = new ImageView(this);
//add image to imageview
...
linLay.addView(image);
ImageView img = new ImageView( this);
LinearLayout rl = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams viewParamsCenter = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(viewParamsCenter);
rl.add(img);
As you want to set the image dynamically, on the button's click. you can add the image view to the linear layout
View LinearLayout1 = findViewById(R.id.Layout1);
ImageView image1 = new ImageView(getApplicationContext());
String uri = "#drawable/myresource.png"; // Here you can set the name of
// the image dynamically
int imageResource = getResources().getIdentifier(uri, null,
getPackageName());
Drawable res = getResources().getDrawable(imageResource);
image1.setImageDrawable(res);
((ViewGroup) LinearLayout1).addView(image1);

Setting gravity for auto generated TextView and Button

How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view

Dynamic TextView in Relative layout

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++) {
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
ll.addView(cb);
}
So how can i put the output on the bottom of the screen linearly.
You should use LinearLayout to automatically add one TextView after another.
Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:
public class HelloWorld extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
Random rnd = new Random();
int prevTextViewId = 0;
for(int i = 0; i < 10; i++)
{
final TextView textView = new TextView(this);
textView.setText("Text "+i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
layout.addView(textView, params);
}
}
}
You've to provide the location of your newly added view. As #Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
If you want to show multiple textviews one after the other, then you should go with LinearLayout.
You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++)
{
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
cb.setId(2000+i);
RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
ll.addView(cb,TextViewLayoutParams);
}

Categories

Resources