I have a problem while creating the dynamic layouts in Android. I can't get any output while testing the code, although I provide background color, it shows blank on the device. I have used weight according to JSON size, which was initially 3, and want to create blocks accordingly.
XML layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/mainlinear"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="fill_parent"/>
</LinearLayout>
java code
public class offers extends Activity {
int len;
String offerjson;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offers);
offerjson = getIntent().getExtras().getString("offersjson");
try {
JSONArray jsonArray = new JSONArray(offerjson);
len = jsonArray.length();
} catch (Exception e) {
}
///////////////////////////////////////////////////////////////////////////
final LinearLayout lm = (LinearLayout) findViewById(R.id.mainlinear);
lm.setWeightSum(len);
try {
JSONArray jsonArray = new JSONArray(offerjson);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject job = jsonArray.getJSONObject(i);
//////////////////////////////// outer Layout
LinearLayout l0 = new LinearLayout(offers.this);
l0.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 1.0f));
l0.setBackground(getResources().getDrawable(R.drawable.black_border));
l0.setOrientation(LinearLayout.VERTICAL);
l0.setWeightSum(4);
////////////////////////////////////inner 1 layout
LinearLayout l1 = new LinearLayout(offers.this);
l1.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 1.0f));
l1.setBackgroundColor(Color.parseColor("#FFE7E7E7"));
l1.setOrientation(LinearLayout.VERTICAL);
l0.addView(l1);
///////////////////////////////Inner 2 LLayout
LinearLayout l2 = new LinearLayout(offers.this);
l2.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 2.0f));
l2.setOrientation(LinearLayout.VERTICAL);
l0.addView(l2);
/////////////////////////Inner 3 LLayout
LinearLayout l3 = new LinearLayout(offers.this);
l3.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 0.9f));
l3.setWeightSum(4);
l3.setOrientation(LinearLayout.HORIZONTAL);
l3.setBackgroundColor(Color.parseColor("#FFABABA"));
l0.addView(l3);
//////////////////////////////////////
LinearLayout l4 = new LinearLayout(offers.this);
l4.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, 0, 0.1f));
l4.setBackgroundColor(Color.parseColor("#FF52caf7"));
l4.setOrientation(LinearLayout.VERTICAL);
l0.addView(l4);
lm.addView(l0);
}
} catch (Exception e) {
}
}
}
Related
I have a checkbox and EditText that generated dynamically by a query
This is the method that generates the checkbox and EditText and adds it to LinearLayout
private void create_form() {
JSONObject json = null;
int count = 0;
lm = (LinearLayout) findViewById(R.id.linearMain);
int seccion = Integer.parseInt(conf.get_id_seccion());
int tipo_solicitud = Integer.parseInt(conf.get_tipo_solicitud());
JSONObject jobj = obj_sqlite.get_form(seccion, tipo_solicitud);
try {
count = Integer.parseInt(jobj.getString("cont"));
json = new JSONObject(jobj.getString("json"));
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
for (int x = 1; x <= count; x++) {
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
CheckBox chb = new CheckBox(this);
chb.setText(json_row.getString("pregunta"));
chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
chb.setTextSize(10);
chb.setPadding(8, 3, 8, 3);
chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(chb);
EditText et = new EditText(this);
et.setHint("observaciones");
et.setId(json_row.getInt("pregunta_verificacion_supervision"));
et.setTextSize(10);
et.setPadding(8, 3, 8, 3);
et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
et.setInputType(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(et);
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
}
}
Now I need to get all this checkbox selected along with your EditText to keep them in the table
this is my xml
<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"
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="com.example.php_mysql_sqlite.Formulario_verificacion_supervision" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_activity_preguntas_revision" android:id="#+id/textView1"/>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="285dp"
android:layout_height="330dp"
android:layout_marginTop="30dp" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/bt_regresar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/scrollView1"
android:onClick="regresar"
android:text="#string/bt_regresar" />
<Button
android:id="#+id/bt_guardar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bt_finalizar"
android:layout_alignBottom="#+id/bt_finalizar"
android:layout_alignLeft="#+id/scrollView1"
android:text="#string/bt_guardar"
android:onClick="guardar" />
<Button
android:id="#+id/bt_finalizar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bt_regresar"
android:layout_alignBottom="#+id/bt_regresar"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/bt_guardar"
android:text="#string/bt_finalizar"
android:onClick="finalizar" />
and this is one image as is currently
http://i61.tinypic.com/2uo2hi8.jpg
by a method that is called on a button click, making the action to get the data
Thanks to all
PS: If you give me negative points, leave a comment of that because it has happened to me in the past and can not do wrong
You have two options.
Set a unique id to each checkbox and EditText and keep their ids in an ArrayList. When you want to do the check
for(int i = 0 ; i < listOfCboxIds.size() ; ++i) {
CheckBox cbox = (CheckBox) findViewById(listOfCboxIds.get(i));
if(cbox.isChecked()) {
// Do something
}
}
Keep the references to CheckBoxes and EditTexts in an ArrayList and just iterate over it. This is less CPU intensive.
List<CheckBox> yourCheckBoxes = new ArrayList<CheckBox>();
List<EditText> yourEditTexts = new ArrayList<EditText>();
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
CheckBox chb = new CheckBox(this);
chb.setText(json_row.getString("pregunta"));
chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
chb.setTextSize(10);
chb.setPadding(8, 3, 8, 3);
chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(chb);
EditText et = new EditText(this);
et.setHint("observaciones");
et.setId(json_row.getInt("pregunta_verificacion_supervision"));
et.setTextSize(10);
et.setPadding(8, 3, 8, 3);
et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
et.setInputTyp
e(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(et);
yourCheckBoxes.add(chb);
yourEditTexts.add(et);
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
Once you need them
for(int i = 0 ; i < yourCheckBoxes.size() ; ++i) {
CheckBox cbox = (CheckBox) yourCheckBoxes.get(i);
EditText et = (EditText) yourEditTexts.get(i);
if(cbox.isChecked()) {
// Do something
}
}
Like Bojan Kseneman said the best option is to store your Views in a Data Structure. This could look like the following:
List<CheckBox> checkBoxes = new ArrayList<>();
List<EditText> editTexts = new ArrayList<>();
Then you add each box and editText to these Lists and later on you iterate over them like this:
for(int i = 0; i < checkBoxes.size(); i++){
CheckBox box = checkBoxes.get(i);
EditText editText = editTexts.get(i);
doSomeThingWithThem();
}
Hope this helps.
I want XML for the TextViews and EditTexts created dynamically. Some blogs suggest that there are some Third Party Libraries that can do that but I wasn't able to find one. I am basically creating TextViews & EditTexts dynamically in my code on a button click.
Code:
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
for (int x = 0; x < 1; x++) {
Display display = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth() / 3;
TextView et1 = new TextView(this);
et1.setBackgroundColor(color.transparent);
et1.setText("Untitled");
et1.setGravity(Gravity.LEFT);
EditText et = new EditText(this);
et.setHint("Click to add");
et.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LayoutParams lp2 = new LayoutParams(width,
LayoutParams.WRAP_CONTENT);
// lp1.addRule(RelativeLayout.BELOW, et1.getId());
linearLayout1.addView(et1, lp2);
linearLayout1.addView(et, lp2);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/addImage" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/addEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/scrollView1"
android:text="Edit" />
The question is, how do I achieve the XML for the TextView & EditText as a String value? Do I give them tags and ids statically in the code or is there any other way?
public static final void writeMapXml(Map val, String name, XmlSerializer out)
throws XmlPullParserException, java.io.IOException
{
if (val == null) {
out.startTag(null, "TextView");
out.endTag(null, "TextView");
return;
}
Set s = val.entrySet();
Iterator i = s.iterator();
out.startTag(null, "TextView");
if (name != null) {
out.attribute(null, "name", "TextView");
}
out.endTag(null, "TextView");
}
XMLSerializer provided by android itself is enough to make dynamic XMLs.
I have written this piece of code. But it is not giving the proper result. Please let me know where is the mistake. And I don't want to use Linear Layout.
Here is the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/custom_relativeLayout1"
android:orientation="horizontal"
android:background="#ffffff">
</RelativeLayout>
</LinearLayout>
String[] but = {"Hello", "Bye"};
int buttonCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customLayout = (RelativeLayout) findViewById(R.id.custom_relativeLayout1);
//customLayout is object of relativelayout.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (butArray[i].getId() != 1)
{
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
else
{
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
}
RelativeLayout.RIGHT_OFhoryzontally or vertically align?
so for vertically
Btnparams.addRule(RelativeLayout.BELOW, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
or for horizontally
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
instead of
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
LinearLayout is very nice to use if you want do dynamically add Views and scale them the same. For example if you want to add buttons and have max 3 buttons per row, you can also use another LinearLayout to make a new row.
I 'abuse' the LinearLayout row and a Button for layoutparams templating.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll_row1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
</LinearLayout>
Example code:
// Get LL and template params
LinearLayout root = (LinearLayout) findViewById(R.id.ll_root);
LinearLayout row1 = (LinearLayout) findViewById(R.id.ll_row1);
ViewGroup.LayoutParams llRowParams = row1.getLayoutParams();
ViewGroup.LayoutParams btnParams = findViewById(R.id.btn1).getLayoutParams();
// Make new button
Button newBtn = new Button(context);
newBtn.setLayoutParams(btnParams);
newBtn.setText("Button 3");
// Add button to row1
row1.addView(newBtn);
// Add new row
LinearLayout row2 = new LinearLayout(context);
row2.setLayoutParams(llRowParams);
root.addView(row2);
// TODO: add buttons to new row
// TODO: some logic to decide between adding to row or creating new row and adding button
Note: code is not tested, but you should get the idea.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
customLayout.addView(butArray[i], Btnparams);
}
I was trying to create something like this: http://s28.postimg.org/a8zi7d4rx/Stop.jpg
My original idea is to create a CanvasImage and X textviews on the right. Then draw with Canvas some dots.
Now i'm stuck at the beginnig; I can't add 2 textview dynamically, 1 below the first.
Here is my code and the screenshot of the result.
What should i do?
Does someone knows if there is any complete example?
Thanks!
public class MainActivity extends Activity {
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout l = (RelativeLayout) findViewById(R.id.lLayout);
//fist txtview
final TextView rowTextView1 = new TextView(this);
rowTextView1.setText("This is the first row");
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p1.addRule(RelativeLayout.RIGHT_OF, findViewById(R.id.imageView1).getId());
l.refreshDrawableState();
l.addView(rowTextView1,p1);
l.requestLayout();
myTextViews[0] = rowTextView1;
for (int i = 1; i < N; i++) {
final TextView rowTextView = new TextView(this);
rowTextView.setText("This is row #" + i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF, findViewById(R.id.imageView1).getId());
//p.addRule(RelativeLayout.ALIGN_BOTTOM, myTextViews[i-1].getId());
p.addRule(RelativeLayout.BELOW, rowTextView1.getId());
p.setMargins(10, 10, 10, 10);
l.addView(rowTextView, p);
l.refreshDrawableState();
myTextViews[i] = rowTextView;
}
}
}
<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"
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=".MainActivity" >
<RelativeLayout
android:id="#+id/lLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#AEEEEE"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:background="#000000"
android:contentDescription="Base" />
</RelativeLayout>
and the result is here: http://s18.postimg.org/e1z3yvlpl/Result.png
I have an android activity which contains a horizontal list view. Below is the layout for it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="#+id/topMostLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/grey">
<HorizontalScrollView
android:id ="#+id/horScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<LinearLayout
android:id ="#+id/dateRibbon"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/line"
android:paddingTop="5dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background ="#37000000"
android:layout_below="#id/horScrollView"
/>
<LinearLayout
android:id="#+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_control_bar"
android:layout_below="#id/line" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/bottom_control_bar"
android:layout_below="#id/horScrollView"
android:text="#string/tasks_empty" />
</RelativeLayout>
The problem is when I add views to the linearlayout inside the horizontalscrollview, it will not scroll fully to the right, so the rightmost child view is invisible to the user. I am adding textviews to the linearlayout. Below is the code for where the linear layout gets populated:
LinearLayout dateRibbon = FindViewById<LinearLayout>(Resource.Id.dateRibbon);
dateRibbon.RemoveAllViews();
TextView tView;
m_dateRibbonTextViews = new List<TextView>();
m_dateRibbonDates = new List<DateTime>();
int currentJobId = Telecetera.Connect.JobLibrary.JobData.Job.GetCurrentJobID();
int defaultCurrentJobID = Telecetera.Connect.JobLibrary.JobData.Job.SYSDIR_CURRENTJOB_DEFAULT;
Telecetera.Connect.JobLibrary.JobData.JobDetailsList jobDetailsList;
int i = -1;
foreach (DateTime date in jobsByDay.Keys)
{
++i;
jobDetailsList = jobsByDay[date];
tView = new TextView(this);
tView.Gravity = GravityFlags.CenterHorizontal;
tView.Text = GetDateDisplayString(date);
tView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
tView.SetPadding(10, 0, 10, 0);
if (currentJobId != defaultCurrentJobID && jobDetailsList.GetByJobID(currentJobId) != null)
{
m_indicesDaysWithCurrentJob.Add(i);
tView.SetBackgroundColor(Android.Graphics.Color.Cyan);
}
else
{
tView.SetBackgroundColor(Android.Graphics.Color.White);
}
tView.Click += new EventHandler(tView_Click);
dateRibbon.AddView(tView);
m_dateRibbonTextViews.Add(tView);
m_dateRibbonDates.Add(date);
tView = new TextView(this);
tView.SetPadding(2, 0, 2, 0);
tView.SetBackgroundColor(Resources.GetColor(Resource.Color.grey));
dateRibbon.AddView(tView);
}
Any hints as to why it does not completely scroll to the right would be appreciated!
Thanks.
I was able to solve it by removing the
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
lines.
you can set padding to linear layout here's my code which show's the textview at center and give some space at start and at the end of view.
lLayTwo.setPadding((center - 2 - centerScreenChildLeft), 0, (center - 2 - centerScreenChildRight), 0);
private void addSubMenu(ArrayList<String> list,String mainMenu){
this.mainMenu = mainMenu;
lLayTwo.removeAllViews();
mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(mViewPlain));
menu = list;
Log.d(TAG, "meulist "+menu.size());
range = list.size() * 2 + 1;
Log.d(TAG, "range "+range);
int menuItem = 0;
for ( int i = 0; i < range; i++ ) {
if ( i % 2 == 0 ){
View txt = new View(MCSDistrictMainActivity.this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setBackgroundResource(R.drawable.line);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 0, 0);
lLayTwo.addView(txt,params);
}else{
TextView txt = new TextView(MCSDistrictMainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(parentChildWidh, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 8, 0, 5);
params.gravity = Gravity.CENTER;
txt.setText(menu.get(menuItem));
txt.setTag(menu.get(menuItem));
txt.setTextColor(getResources().getColor(R.color.violet));
txt.setTextSize(15f);
txt.setGravity(Gravity.CENTER_HORIZONTAL);
menuItem += 1;
lLayTwo.addView(txt,params);
}
}
Log.d(TAG, "llayout Two "+lLayTwo.getChildCount()+" width "+parentChildWidh+" LEFT "+parentChildLeft);
int left = parentChildLeft;
int width = parentChildWidh;
int centerTab = 1 + hsvLowerTab.getWidth() / 2;
Log.d(TAG, "Measures left "+left+" width "+width+" center "+centerTab+" all "+( left + width -centerTab ));
//(left + width/2)-centerTab
hsvLowerTab.scrollTo((left + width/2)-centerTab, 0);
hsvLowerTab.dispatchTouchEvent (MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,(left + width/2)-centerTab, 0, 0));
}