How can I change ImageButton size programmatically? - android

I'm generating ImageButtons programmatically and I'm not able to change the size of the items using XML. So, I would like to do it programmatically too.
How can I do that?
My code :
String[] separated = result.split("%");
ImageButton [] txt =new ImageButton[20];
for(int i=0;i<txt.length;i++)
{
String getname = separated[i];
txt[i]=new ImageButton(hphotos.this);
Picasso.with(getBaseContext()).load("http://www.mywebsite.com/" + getname).into(txt[i]);
txt[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
scroll.addView(txt[i]);

Have you tried
yourView.setLayoutParams(new LayoutParams(width,height));
?

Related

Q - Display Text like Command Prompt in Android

Is there a way to display text strings on the screen in a similar look to command prompt for windows, i.e. text fills the bottom-most line and stays on the screen as new lines are added below it? I feel like Canvas.drawTextOnPath can be used for this?
There's probably a bunch of ways to do this, depending on what you're using it for. Here's what I would do:
Add a LinearLayout with android:gravity="bottom", and add your TextView to that layout.
When you add text to it, use something like:
public ArrayList<String> text = new ArrayList<String>();
int lines = 20;
TextView textField = (TextView)findViewById(R.id.textview);
private String concatLines(){
String txt = "";
for(String s: text){
txt += s+"\n";
}
return txt.substring(0, txt.length()-1);
}
private void appendLine(String line){
text.add(line);
if(text.size() > lines){
text.remove(0);
}
textField.setText(concatLines());
}
appendLine("> A new line");
The android:gravity="bottom" should keep your text field aligned at the bottom of the layout. Make sure the TextView's height is wrap_content.

Dynamic layout for textview - android

I am getting number of string from server.
I want to show it on layout like below image
Everytime I'll be getting different type of string. so how to create dynamic layout for textview?
May be you are looking for a Flow Layout.
https://github.com/ApmeM/android-flowlayout
using this code, we can able to create linear layout dynamically with inside textview. if you want, just modify the code to as you like, above asked..
onCreate() Method :
private Button[] btnAdd ,btnQuestionPalete ; // GLobally
private LinearLayout layout_button ;
layout_button = (LinearLayout) findViewById(R.id.button_layout);
btnAdd = new Button[100] ;
LayoutParams param2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
btnQuestionPalete = new Button[100];
for (int i = 0; i < noOfButton; i++) {
linearQuestionPalettesRow = new LinearLayout(getApplicationContext());
linearQuestionPalettesRow.setOrientation(LinearLayout.VERTICAL);
linearQuestionPalettesRow.setLayoutParams(param2);
linearQuestionPalettesRow.setPadding(10, 10, 10, 10);
textView[i] = new TextView(getApplicationContext());
textView[i].setId(i+1);
textView[i].setText("test");
textView[i].setTextColor(Color.BLACK);
textView[i].setGravity(Gravity.CENTER_HORIZONTAL);
linearQuestionPalettesRow.addView(textView[i]);
layout_button.addView(linearQuestionPalettesRow);
}
xml code :
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:orientation="horizontal"
>
</LinearLayout>
Its easy just add the stings to the array/arraylist and make arrayadapter out of it .Then pass it listView or DridView
You can use Table Layout to achieve this dynamically.
but i think you want your layout in zigzag manner like:
------------------
------------------
------------------
------------------
so you can do it using ListView(efficient)
but if you don't want this pattern and you don't have rows more than 5-10 then you can go for TableLayout

How to assign values from an array to textviews in android

I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);

How to reference an array of EditText created in main.xml

My app is getting a little bloated, I would like to 'compress' some code by changing some of my variables into arrays and using some for loops to process my variables.
My first target is initializing my EditText boxes
My original code (working fine) included:
EditText miBox1, spBox1 ;
EditText miBox2, spBox2 ;
...
miBox1 = (EditText)this.findViewById(R.id.miBox1);
spBox1 = (EditText)this.findViewById(R.id.spBox1);
miBox2 = (EditText)this.findViewById(R.id.miBox2);
spBox2 = (EditText)this.findViewById(R.id.spBox2);
I actually have twenty mi boxes and 20 sp boxes to setup
I am trying to create an array, and loop through them
In my main.xml I have renamed my EditText boxes to
miBox[1] spBox[1], etc.
I'm declaring in my activity with:
EditText[] miBox = new EditText[20] ;
EditText[] spBox = new EditText[20] ;
and putting this into a for loop
miBox[i] = (EditText)this.findViewById(R.id.miBox[i]);
spBox[i] = (EditText)this.findViewById(R.id.spBox[i]);
but Eclipse cannot resolve R.id.miBox[i]
How do I properly create an array of EditText in my xml so that
it is recognized?
Thanks
JD
int resID = getResources().getIdentifier("miBox" + i,
"id", getPackageName());
where i is your index
EditText editText = (EditText)findViewById(resId);
if you want to put it in an array, simply do this :
miBox[i] = (EditText)findViewById(resId);
the problem here is you have wrongly modified a few things.
the code should be like:
EditText[] miBox = new EditText[20] ;
EditText[] spBox = new EditText[20] ;
and putting this into a for loop
miBox[i] = (EditText)this.findViewById(R.id.miBox1);
spBox[i] = (EditText)this.findViewById(R.id.spBox1);
Here, the msitake is using miBox[i] since thats not the id you have used in your layout xml.
hope this helps.

Changing image view background dynamically

I have a a set of 10 imageviews in my layout. I have given them sequential id's also as
android:id="#+id/pb1"
android:id="#+id/pb2"
Now I want to change background dynamically.
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(R.drawable.progressgreen);
}
Now inside the for loop I want to set the image view background dynamically. i,e if the currentpoi value is 3, background of 3 image views should be changed. What ever the times the for loop iterates that many image view's background should be changed. Hope the question is clear now.
Note : I have only 1 image progressgreen that need to be set to 10 image views
Finally I did this in the following way,
I placed all the id's in the array as
int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};
Now:
int pindex = 0;
for (pindex; pindex <currentPoi; pindex++) {
ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
img.setImageResource(R.drawable.progressgreen);
}
Now, I am able to change the images dynamically.
#goto10. Thanks for your help. I will debug your point to see what went wrong in my side
Create an ImageView array:
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);
Now iterate the loop to set the background of images like this:
for (i=1;i<=currentPoi;i++)
{
views[i-1].setBackgroundResource(R.drawable.progressgreen);
}
you can do this by setting the name of drawables something like:
img_1, img_2, img_3...
for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable", getPackageName()));
}
Try this code.....
Create image Array..
private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };
And than modify your code
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(mThumbIds[i]);}
You could make an array of your ImageViews and then change them in your for loop.
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);
and then change your for loop to:
for (i=1;i<=currentPoi;i++) {
views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}
Arrays start at index 0, so make sure there's not an off-by-one error in here.
You'll need to give your ImageViews sequential ids, such as "#+id/pb1" and "#+id/pb2", etc.. Then you can get each of them in the loop like this:
for (i=1;i<=currentPoi;i++) {
// Find the image view based on it's name. We know it's pbx where 'x' is a number
// so we concatenate "pb" with the value of i in our loop to get the name
// of the identifier we're looking for. getResources.getIdentifier() is able to use
// this string value to find the ID of the imageView
int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");
// Use the ID retrieved in the previous line to look up the ImageView object
ImageView imageView = (ImageView) findViewById(imageViewId);
// Set the background for the ImageView
imageView.setBackgroundResource(R.drawable.progressgreen);
}
Replace com.your.package.name with your application's package.

Categories

Resources