When creating TextView dynamically the second TextView is not appearing - android

I'm creating two TextViews in my LinearLayout using the code below. I'm using them this way to catch a touch event when the press this specified area of the screen
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LinearLayout layOut = (LinearLayout)findViewById(R.id.loginView);
LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(520, 70);
loginParams.setMargins(120, 670, 0, 0);
TextView loginBttn = new TextView(this);
layOut.addView(loginBttn);
loginBttn.setBackgroundColor(Color.GREEN);
loginBttn.setLayoutParams(loginParams);
loginBttn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Code touch events
}
});
LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(370, 40);
forgotParams.setMargins(150, 770, 0, 0);
TextView forgotBttn = new TextView(this);
layOut.addView(forgotBttn);
forgotBttn.setBackgroundColor(Color.GREEN);
forgotBttn.setLayoutParams(forgotParams);
forgotBttn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Code touch events
}
});
I'm using Green to locate them. When finished I'll take those lines out. The first works and appears fine but the second does not appear and I can't see why.

I think its out of screen try lower values for margin and position
eg:
LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(50, 50);
loginParams.setMargins(10, 10, 0, 0);
TextView loginBttn = new TextView(this);
layOut.addView(loginBttn);
loginBttn.setBackgroundColor(Color.GREEN);
loginBttn.setLayoutParams(loginParams);
LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(10, 10);
forgotParams.setMargins(20, 20, 0, 0);
TextView forgotBttn = new TextView(this);
layOut.addView(forgotBttn);
forgotBttn.setBackgroundColor(Color.GREEN);
forgotBttn.setLayoutParams(forgotParams);

Related

Removing a view from OnTouchListener from a dynamically created layout

I have a dynamically created Overlay Window that has an OnTouchListener on a certain layout in the Window. When I detect a a touch with the OnTouchListener, I wish to remove this Overlay Window. This overlay is dynamically created and not part of an activity so does not follow the the Activity lifecycle, hence I can't call finish(). How do I remove the view then? The code snippet below is where I create the View, and where my OnTouchListener is. How should I remove the overlay?
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
flags,
PixelFormat.TRANSLUCENT);
LinearLayout l = new LinearLayout(service);
RelativeLayout mainView = new RelativeLayout(service);
RelativeLayout.LayoutParams param_one = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
param_one.leftMargin=0;
param_one.topMargin=0;
l.setTag("first_tag");
l.setOrientation(LinearLayout.VERTICAL);
l.setBackgroundColor(0x00ffffff);
EditText second_test = new EditText(service);
second_test.setBackgroundColor(Color.parseColor("#0b0b0b")); //TODO: not perfect color
second_test.setTextColor(Color.LTGRAY);
second_test.setWidth(440);
second_test.setTag("second_tag");
second_test.setTextSize(20);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
second_test.setLayoutParams(llp);
l.addView(second_test);
mainView.addView(l, param_one);
LinearLayout bottomLayout = new LinearLayout(service);
RelativeLayout.LayoutParams bottomparam = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
bottomparam.width=700;
bottomparam.height=120;
bottomparam.leftMargin=10;
bottomparam.topMargin=260;
bottomLayout.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch");
// Remove the overlay here
return true;
}
});
mainView.addView(bottomLayout, bottomparam);
I tried to call WindowManager.removeView(thisView) but system services such as WindowManager are not available to Activities before onCreate().

Scale button without affect other buttons

I have a LinearLayout, i create(programatically) and adds buttons to it
public void initButtons(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new android.app.ActionBar.LayoutParams(dpiToPixels(context, 100), dpiToPixels(context,100)));
params.setMargins(dpiToPixels(context,20), 0, 0, 0);
button.setLayoutParams(params);
}
layout.addView(button, index);
When user touch(onTouchListener) any button i want to scale it:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) button
.getLayoutParams();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lp.width = (int) (93 * Resources.getSystem()
.getDisplayMetrics().density);
lp.height = (int) (93 * Resources.getSystem()
.getDisplayMetrics().density);
button.setLayoutParams(lp);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
lp.width = (int) (100 * Resources.getSystem()
.getDisplayMetrics().density);
lp.height = (int) (100 * Resources.getSystem()
.getDisplayMetrics().density);
button.setLayoutParams(lp);
}
return false;
}
});
The problem is that when i press one button down it affects every other buttons on the LinearLayout( looks scaled and moving a little left). Anyone has any idea how to solve this?
This is bound to happen in a LinearLayout.
Try using FrameLayout. In a frameLayout, changing one button size won't affect others.
You can also use a RelativeLayout, but you shouldn't position your buttons 'relative' to others in it.

How to change position Textview get using context

TableRow tr1 = new TableRow(context);
TextView hbar = new TextView(context);
tr1.addView(hbar, 0);
tl.addView(tr1,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I need to change the possition of textview.
hbar.setPadding(10, 0, 0, 0); not worked. and also tried with set layoutparams. any suggestions ??
set the layoutperams to the TableRow with LayoutParams.MATCH_PARENT, and then try it once.
TableRow tr1 = new TableRow(context);
TextView hbar = new TextView(context);
tr1.addView(hbar, 0);
tl.addView(tr1,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tl.setPadding(10,0,0,0);
here views are creating dynamically, we unable to change the positions in onCreate method, try to override the onWindowFocusChange() and then do what you want.
try this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//your textview.
tl.setPadding(10,0,0,0);
}

managing visibility of relativelayout children

I had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..

Android dynamic layout click problems

i create a dynamic view ( linearlayout ) with
table.addView(view, layoutParams);
I want when I click on view1 the background color turns white,
when I click on view2 the background color of view1 turns black and the background color of view2 turns white, and if I click on view3 the background color of view2 turns black and the background color of view3 turns white ...
private void addToRow(int rowID, TimeCard timecard, int offset) {
LayoutParams tlparams = new LinearLayout.LayoutParams(TIMECARD_WIDTH,
LinearLayout.LayoutParams.WRAP_CONTENT);
tlparams.setMargins((int) offset, 150-50*rowID, 0, 0);
System.out.println("rowID= "+50*rowID);
View v = timecard.getView(context);
v.setOnClickListener(this);
rows.get(rowID).addView(v, tlparams);
}
private void addRowsToTable() {
LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (LinearLayout row : rows) {
layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//table is an absolutlayout and i add many row in this layout
table.addView(row, layoutParams);
}
tableNavbar.addView(llnavBar, layoutParams);
}
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.cadre_blanc);
TextView t1=(TextView)v.findViewById(R.id.tc_title);
t1.setTextColor(Color.rgb(0, 139, 221));
}
how can i do this please??

Categories

Resources