RelativeLayout's gravity:center won't work - android

I'm trying to make a function, which would return a RelativeLayout, with an ImageView and a TextView in the center.
public RelativeLayout makeKey(String letter, int alfa)
{
final RelativeLayout RelBtn = new RelativeLayout(this);
RelBtn.setGravity(Gravity.CENTER);
RelBtn.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ));
final ImageView ivBg = new ImageView(this);
ivBg.setImageBitmap(bmBtnBg);
ivBg.setPadding(5, 5, 5, 5);
RelBtn.addView(ivBg);
TextView tvLetterSHD = new TextView(this);
tvLetterSHD.setTextSize(22);
tvLetterSHD.setTypeface(null, Typeface.BOLD);
tvLetterSHD.setTextColor(0xFF000000);
tvLetterSHD.setText(letter);
tvLetterSHD.setPadding(0, 3, 0, 0);
RelBtn.addView(tvLetterSHD);
TextView tvLetter = new TextView(this);
tvLetter.setTextSize(22);
tvLetter.setTypeface(null, Typeface.BOLD);
tvLetter.setTextColor(0xFFFFFFFF);
tvLetter.setText(letter);
RelBtn.addView(tvLetter);
return RelBtn;
}
Yet I'm getting something like this, as you can see, the TextView is off center.
Any ideas what might be wrong? Thanks! :)

U can set LayoutParams android:layout_centerInParent for child particles:
RelativeLayout lpCenter = new RelativeLayout.LayoutParams(w, h);
lpCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
tvLetter.setLayoutParams(lpCenter);

Related

Android dynamic table creation with ImageView and TextView design with date at parent right

I am not able to put two textview's to the right of ImageView and aligning the date to parent right is also getting difficult for me in dynamic creation. someone who have knowledge on this please help me. Design should be DYNAMIC.
tl = (TableLayout) view.findViewById(R.id.mainLayout);
TableRow row2 = new TableRow(getContext());
row2.setLayoutParams(new TableRow.LayoutParams(200, 230, 280));
TableLayout posterInfoTable = new TableLayout(getContext());
posterInfoTable.setBackgroundResource(R.drawable.whitegrey);
posterInfoTable.setLayoutParams(new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 250));
TableRow posterNameRow = new TableRow(getContext());
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 30, 0);
posterNameRow.setHorizontalGravity(Gravity.LEFT);
posterNameRow.setLayoutParams(lp);
ImageView posterImage = new ImageView(getContext());
posterImage.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
posterImage.setMaxHeight(100);
posterImage.setMaxWidth(100);
Picasso.with(getContext())
.load(R.drawable.dummyimage2)
.placeholder(R.drawable.dummyimage2)
.error(R.drawable.dummyimage2)
.transform(new RoundedTransformation(50, 4))
.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
.centerCrop()
.into(posterImage);
posterNameRow.addView(posterImage);
final TableRow nameRow = new TableRow(getContext());
nameRow.setPadding(40, 0, 0, 0);
nameRow.setLayoutParams(new TableLayout.LayoutParams(250, 100));
nameRow.setTag(db_id);
TextView posterName = new TextView(getContext());
posterName.setText(postman);
posterName.setTextColor(Color.BLACK);
posterName.setTextSize(18);
posterName.setPadding(10, 0, 0, 0);
posterName.setWidth(240);
// posterName.text
posterNameRow.addView(posterName);
TextView dateview = new TextView(getContext());
dateview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT))
dateview.setText(date);
dateview.setPadding(50, 0, 0, 5);
dateview.setTextColor(Color.DKGRAY);
dateview.setTextSize(10);
dateview.setWidth(300);
dateview.setGravity(Gravity.RIGHT);
nameRow.addView(dateview);
posterInfoTable.addView(posterNameRow);
posterInfoTable.addView(nameRow);
// posterInfoTable.addView(dateRow);
row2.addView(posterInfoTable, new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 200));
// row2.addView(dateview);
tl.addView(row2, new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 200));
you do not need to create all views dynamically. Create XML with your item-layout. try this:
void addNewRow(YourData data) {
View v = getLayoutInflater().inflate(R.layout.your_item, null);
RelativeLayout rl = (RelativeLayout) v; //or other...
TextView textView1 = (TextView) rl.findViewById(R.id.textV1);
ImageView imageView1 = (ImageView) rl.findViewById(R.id.imageV1);
textView1.setText(data.name);
imageView.setImageResource(data.imageRes);
tableLayout.addView(rl);
}

Tablerow textview above edittext (Android)

I hope you can help. So what i need to do is to have edittext below textview (Very well explained in the picture)
(Picture)
Code:
for(int x=0 ; x < loopCount; x++){
TableRow.LayoutParams params = new TableRow.LayoutParams( headerCellsWidth[x+1],LayoutParams.MATCH_PARENT);
params.setMargins(0, 0, 0, 0); //left? up? right? down?
TextView tv1=new TextView(getContext());
EditText et1=new EditText(getContext());
et1.setHeight(40);
et1.setWidth(40);
tv1.setText("Text");
TextView textViewB = this.bodyTextView(info[x]);
taleRowForTableD.addView(tv1,params);
taleRowForTableD.addView(et1);
}
I hope you can help me out :)
Here is full example http://www.codeofaninja.com/2013/08/android-scroll-table-fixed-header-column.html
Try keeping your EditText and TextView in a RelativeLayout, and then put that RelativeLayout in TableRow's cell.
RelativeLayout layout = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setText("My text");
EditText et = new EditText(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv);
layout.addView(et, lp);
taleRowForTableD.addView(layout);
Hope it helps.

Create multiple linear layout and divider programmatically

In my app. there is activity contain multiple linear layout and divider which created programmatically , its run fine ,
but i have to duplicate the linear layout and divider 5 times ,and all are the same except two things :
1- each linear layout has different string .
2- first divider margin differ than others divider margin .
is there's better approach to do that with more clean and shorter code .
any help will be much appreciated .
public class Dreams extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.trip);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
TextView tv = (TextView) findViewById(R.id.title);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
tv.setText("Dreams");
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// add text view
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.RIGHT);
tv1.setTextSize(30);
tv1.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv1);
tv1.setText(Html.fromHtml(getString(R.string.dreams)));
ImageView divider1 = new ImageView(this);
LinearLayout.LayoutParams lp1 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp1.setMargins(40, 0, 40, 0);
divider1.setLayoutParams(lp1);
divider1.setBackgroundColor(Color.RED);
ll.addView(divider1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setTextSize(30);
tv2.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv2);
tv2.setText(Html.fromHtml(getString(R.string.dream_1)));
ImageView divider2 = new ImageView(this);
LinearLayout.LayoutParams lp2 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp2.setMargins(10, 10, 10, 10);
divider2.setLayoutParams(lp2);
divider2.setBackgroundColor(Color.RED);
ll.addView(divider2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.RIGHT);
tv3.setTextSize(30);
tv3.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv3);
tv3.setText(Html.fromHtml(getString(R.string.dream_2)));
ImageView divider3 = new ImageView(this);
LinearLayout.LayoutParams lp3 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp3.setMargins(10, 10, 10, 10);
divider3.setLayoutParams(lp3);
divider3.setBackgroundColor(Color.RED);
ll.addView(divider3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.RIGHT);
tv4.setTextSize(30);
tv4.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv4);
tv4.setText(Html.fromHtml(getString(R.string.dream_3)));
ImageView divider4 = new ImageView(this);
LinearLayout.LayoutParams lp4 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp4.setMargins(10, 10, 10, 10);
divider4.setLayoutParams(lp4);
divider4.setBackgroundColor(Color.RED);
ll.addView(divider4);
TextView tv5 = new TextView(this);
tv5.setGravity(Gravity.RIGHT);
tv5.setTextSize(30);
tv5.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv5);
tv5.setText(Html.fromHtml(getString(R.string.dream_4)));
ImageView divider5 = new ImageView(this);
LinearLayout.LayoutParams lp5 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp5.setMargins(10, 10, 10, 10);
divider5.setLayoutParams(lp5);
divider5.setBackgroundColor(Color.RED);
ll.addView(divider5);
TextView tv6 = new TextView(this);
tv6.setGravity(Gravity.RIGHT);
tv6.setTextSize(30);
tv6.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv6);
tv6.setText(Html.fromHtml(getString(R.string.dream_5)));
ImageView divider6 = new ImageView(this);
LinearLayout.LayoutParams lp6 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp6.setMargins(10, 10, 10, 10);
divider6.setLayoutParams(lp6);
divider6.setBackgroundColor(Color.RED);
ll.addView(divider6);
}
}
Since all that is changing is the TextView setText() you can make this a for loop with a list of String inputs. For example:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
String[] textEntries = { getString(R.string.dream),
getString(R.string.dream_1),
getString(R.string.dream_2),
getString(R.string.dream_3),
getString(R.string.dream_4),
getString(R.string.dream_5)
};
for ( int i = 0; i < textEntries.length; i++)
{
TextView tv = new TextView(this);
tv.setGravity(Gravity.RIGHT);
tv.setTextSize(30);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv);
tv.setText(Html.fromHtml(textEntries[i]));
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp.setMargins(10, 10, 10, 10);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.RED);
ll.addView(divider);
}
first of all it would be easier if you define your layouts in XML instead of adding them programmatically. You will profit from the benefits of the UI editor as well. :)
Second, you may want to use ListView and an Adapter to fill the list, since you do not want do duplicate the same tasks for each layout.
Maybe these two links are helpful:
1. http://developer.android.com/guide/topics/ui/declaring-layout.html
2. http://developer.android.com/guide/topics/ui/layout/listview.html
So, to finally answer your question, I would do the following:
Create a file, e.g. list_item.xml, with something like:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"><TextView your attributes.../></LinearLayout>
Create another layout, for instance main.xml, which contains a ListView. You can change the color of the divider like described here How to change the divider color in the listview?.
In your code (activity or fragment) add the main.xml as content view via setContentView().
Also in your code you should then add an adapter to the ListView which then populates the list for you. Here is an example How to customize listview using baseadapter
Finally, and since you separate the concerns (design and code), you could achieve what you want with just a few lines in your activity (the layout stuff would be in the xml and the population could be moved to a separated class like MyAdapter.java...)
Hope that helps...

How to overlay text on image programmatically

I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.

Android Relative layout component allignment

Plz validate the below relative layout
RelativeLayout objRLActionBar=new RelativeLayout(this);
objRLActionBar.setId(2534);
RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,(int) (screenHeight*layoutHeights[1]));
objRLActionBarParams.addRule(RelativeLayout.BELOW,objRLTitleBar.getId());
objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));
ImageView objIVActivityIcon = new ImageView(this);
objRLActionBar.setId(25324);
RelativeLayout.LayoutParams objIVActivityIconParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objIVActivityIconParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,objRLActionBar.getId());
objIVActivityIcon.setLayoutParams(objIVActivityIconParams);
objIVActivityIcon.setImageResource(R.drawable.home_def);
objIVActivityIcon.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objIVActivityIcon);
ImageView objIVSeperator = new ImageView(this);
objIVSeperator.setId(25342);
RelativeLayout.LayoutParams objIVSeperatorParams=new RelativeLayout.LayoutParams(1,LayoutParams.FILL_PARENT);
objIVSeperatorParams.addRule(RelativeLayout.RIGHT_OF,objIVActivityIcon.getId());
objIVSeperator.setLayoutParams(objIVSeperatorParams);
objIVSeperator.setImageResource(R.drawable.separator);
objIVSeperator.setBackgroundColor(Color.parseColor("#1f3449"));
objRLActionBar.addView(objIVSeperator);
TextView objTVPageName = new TextView(this);
RelativeLayout.LayoutParams objTVPageNameParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
objTVPageNameParams.addRule(RelativeLayout.RIGHT_OF,objIVSeperator.getId());
objTVPageName.setLayoutParams(objTVPageNameParams);
objTVPageName.setTextColor(Color.WHITE);
objTVPageName.setTextSize(TypedValue.COMPLEX_UNIT_PX,18+sizeAdjust);
objTVPageName.setText("House Details");
objTVPageName.setTypeface(null, Typeface.BOLD);
objTVPageName.setPadding(2, 0, 2, 0);
objRLActionBar.addView(objTVPageName);
objRLBody.addView(objRLActionBar,objRLActionBarParams);
And the output is is as shown below
the image overlapped with text and the 'separator image' comes first! I need the components in this order objIVActivityIcon, objIVSeperator,objTVPageName. What is wrong with the above code plz help...
objRLActionBar.setId(25324);
should be
objIVActivityIcon.setId(25324);

Categories

Resources