Without "TextView" in code, "LinearLayout.LinearParams" not increasing margin - android

In this code i just wanted to change the margin when i click on button. but the problem is that this code is working until this line ((TextView)findViewById(R.id.textView)).setText("Changed at runtime!"); is presented in code. if I remove this line the margin is not increasing when i am pressing the button.
This example is from a book "Android Application Development Cookbook", but there is no explanation for this and I didn't find any any thing much helpful about LinearLayout.LinearParams on android website. so any help is appreciated.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((TextView)findViewById(R.id.textView)).setText("Changed at runtime!");
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.leftMargin += 5;
}
});
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/button"/>
</LinearLayout>

You should set the LayoutParams again to the view like this,
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.leftMargin += 5;
view.setLayoutParams(params);

Related

adding view to linear layout not visible

I have a problem adding programmatically buttons to linearLayout, the first button gets added but the others aren't visible.
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(myClass.this);
curButton.setText("" + text);
curButton.setBackgroundResource(R.drawable.tran_mini);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
paginglibrary.addView(curButton);
}
});
this is the xml file where the buttons are being added:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
the first time I click on nextButton the button gets added and is show but the second time I add another button, it doesn't show.
I am sure they are getting added as I checked through the following code:
ViewGroup layout = (ViewGroup)findViewById(R.id.pagingView1);
System.out.println("layout count: "+ layout.getChildCount());
the count shows that the button is getting added but it's not showing.
Any idea what could be the cause?
I tried setting the abscissa of the button to make sure it's not being outside of the view but still the same scenario occurs.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new button"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
The only difference is on the HorizontalScrollView width.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(MainActivity.this);
curButton.setText("generated button");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
ll.addView(curButton);
}
});

Adding a Textview to a Relative Layout in Android Studio

I have a relative layout in my XML file and it contains a button. Now I want that when I press this button, it creates 2 TextViews. Any help please because I am new to Android Studio? I have tried creating the onClickListener for the button but I am having problems in order to get an object of the current relative layout which I have in the XML.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
Intent i = getIntent();
final Button addAlertButton = (Button)findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
}
});
}
The following is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.dentalapp.BrushYourTeeth"
tools:showIn="#layout/activity_main">
<!--ALERT 1-->
<TextView
android:id="#+id/Alert1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 1"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="08:00"
android:textSize="25dp"
android:onClick="showTimePickerDialog"
android:layout_above="#+id/Alert2"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ALERT 2-->
<TextView
android:id="#+id/Alert2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 2"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21:00"
android:textSize="25dp"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ADD ALERT BUTTON-->
<Button
android:id="#+id/AddAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Alert"
android:textAllCaps="false"
android:textSize="25dp"
android:padding="15dp"
android:layout_below="#id/Alert2"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"/>
</RelativeLayout>
Thanks!!
This link may be useful for you:
displaying a string on the textview when clicking a button in android
It is for one string, you may add your second text view into method below:
private void printmyname(){
System.out.println("coming");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
final Button addAlertButton = (Button) findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
int prevTextViewId = 0;
final TextView textView1 = new TextView(this);
final TextView textView2 = new TextView(this);
textView1.setText("Text 1");
textView2.setText("Text 2");
int curTextViewId = v.getId();
textView1.setId(curTextViewId+1);
textView2.setId(curTextViewId+2);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, v.getId()+1);
textView1.setLayoutParams(params);
params.addRule(RelativeLayout.BELOW, v.getId()+2);
textView2.setLayoutParams(params);
layout.addView(textView1, params);
layout.addView(textView2, params);
}
});
}
Not sure but something like this helps you

Set layout margin for element in relativeLayout by code

I have codes of a RelativeLayout activity.xml
<RelativeLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/relativeLayout"
android:background="#686868"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
And here is code of Activity:-
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
}
}
}
I don't know the way to get element and set margin in relativeLayout by code..Can you help me?
This may helps you
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.RIGHT_OF, 1001);
params.addRule(RelativeLayout.LEFT_OF, 1000);
Here id 1001 and 1000 is my EditText id which also dynamic added to Relative
To access UI Elements within Android code, you use the findViewById method. For example:
Button btnBack = (Button)findViewById(R.id.btnBack);
Once you have access to all of the elements you need, you can use the setter functions provided in the API to change the layout as need-be. Since most (if not all) UI elements are a child class of the View class, the methods you will need will most likely be defined there. Here is a good reference:
http://developer.android.com/reference/android/view/View.html
All View objects inherit the setLayoutParams method. So you can apply the method suggested by Gunaseelan to your Button and your TextView as well.
Try this code....
<LinearLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/linearLayout"
android:background="#686868"
android:orientation="horizontal"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</LinearLayout>
and in java code...
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button btn = (Button) findViewById(R.id.btnBack);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
btn.setText("backPreviousPage");
}
}
}
Try this way.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.setMargins(80, 0, 0, 0); //left, top, right, bottom
params.height = 60; //set height dynamically
params.width = 200; // set width dynamically
relativeLayout.setLayoutParams(params);
I hope this will help you.

setting layouts visiblity on button click event

I am new to android and i am unable to solve my simple problem.I have a parent Tablelayout and inside it i have two tablelayouts with ids tbl1 and tbl2 respectively in my xml file.In tbl1 layout i have three textviews and three edittext controls similarly i have some views in tbl2 layout.Now i want that my tbl1 layout is visible when my activity starts but on click of my button2 which is in tbl1 layout my tablelayout tbl1 gets invisible and my tablelayout tbl2 becomes visible.Actually i know i can achieve this in asp.net with the help of panels but in android i am not able to achieve the same thing.Please help
You are going to want to look at the setVisibility() method. In the on click listeners for button 2, put the following;
Button.setVisibility(View.INVISIBLE)
TextView.setVisibility(View.INVISIBLE)
etc...
This will make the views invisible, but they will still take up space. If you don't want them to take up space you should use
setVisibility(Veiw.GONE);
Finally, to make you buttons and textview and edittexts in the second table, appear, you need to do the following;
setVisibility(View.VISIBLE);
Java Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visibility_1);
// Find the view whose visibility will change
mVictim = findViewById(R.id.victim);
// Find our buttons
Button visibleButton = (Button) findViewById(R.id.vis);
Button invisibleButton = (Button) findViewById(R.id.invis);
Button goneButton = (Button) findViewById(R.id.gone);
// Wire each button to a click listener
visibleButton.setOnClickListener(mVisibleListener);
invisibleButton.setOnClickListener(mInvisibleListener);
goneButton.setOnClickListener(mGoneListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.VISIBLE);
}
};
OnClickListener mInvisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
OnClickListener mGoneListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.GONE);
}
};
}
XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#drawable/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_1"/>
<TextView android:id="#+id/victim"
android:background="#drawable/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_2"/>
<TextView
android:background="#drawable/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_3"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/vis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_vis"/>
<Button android:id="#+id/invis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_invis"/>
<Button android:id="#+id/gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_gone"/>
</LinearLayout>
</LinearLayout>

Android setlayout visibility on button click

In the following code on click of the button i want to hide the relative layout rl1 and show rl2 but my app crashes down after on button click.What am i doing wrong
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/welcome_1">
<ImageView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="360dp"
android:layout_marginRight="4dp"
android:src="#drawable/button1" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="#drawable/menu_2"
></RelativeLayout>
</RelativeLayout>
Java code
public class mockupsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ImageButton img1 = (ImageButton)findViewById(R.id.button1);
ImageView img1 =(ImageView)findViewById(R.id.button1);
img1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
// Toast.makeText(TouchpointmockupsActivity.this, "test", Toast.LENGTH_SHORT).show();
LinearLayout rl1 = (LinearLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
LinearLayout rl2 = (LinearLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);
}
});
}
}
Change LinearLayout to RelativeLayout in your code. You might be getting a classcastException.
Edit:
Change OnClick method as below
RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
rl1.setVisibility(View.INVISIBLE);
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
rl2.setVisibility(View.VISIBLE);

Categories

Resources