I am having a LinearLayout whose visibility is directly affected by the click of a TextView. This LinearLayout has more TextViews dynamically added inside. My LinearLayout viewQuickLinks starts out with a visibility of gone. In my oncreate I call addQuickLinks which then adds several TextViews to the LinearLayout. None of these TextViews have a set visibility. I click on the TextView to change the LinearLayout to visible and space is added, but there are no TextViews.
My xml file (just to add a note this is all in a scrollview):
<TextView
android:id="#+id/textQuickLinksTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/navigation_expand"
android:text="#string/quick_links_title"
android:textSize="25sp"
android:visibility="visible" />
<LinearLayout
android:id="#+id/viewQuickLinks"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:visibility="gone"
android:orientation="vertical" />
Changing the LinearLayout to visible and gone:
private void setUpQuickLinks() {
final TextView quickLinksTitleText = (TextView) findViewById(R.id.textQuickLinksTitle);
quickLinksTitleText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout viewQuickLinks = (LinearLayout) findViewById(R.id.viewQuickLinks);
if (viewQuickLinks.getVisibility() == View.VISIBLE){
viewQuickLinks.setVisibility(View.GONE);
quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_expand, 0);
}
else{
viewQuickLinks.setVisibility(View.VISIBLE);
quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_collapse, 0);
}
}
});
quickLinksClickListeners();
}
Why are the TextViews not appearing when the LinearLayout is Visible?
Thank you for any help!
Try changing android:layout_height to fill_parent. Why is it 1 dip?
Related
Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !
I have the following LinearLayout in which I'm dynamically inserting views:
<LinearLayout
android:id="#+id/loyaltycardlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#00f"
android:orientation="vertical"/>
and the Java code:
LinearLayout loyaltyCardLL = (LinearLayout)findViewById(R.id.loyaltycardinfo);
for(MyCard thisCardNode : mLoyaltyCards)
{
LinearLayout item = (LinearLayout)getLayoutInflater().inflate(
R.layout.activity_view_node_details_loyaltycard, null);
item.setTag(thisCardNode);
TextView tv = (TextView)item.findViewById(R.id.loyaltycard_item_title);
tv.setText(thisCardNode.getTitle());
... etc
loyaltyCardLL.addView(item);
}
loyaltyCardLL.invalidate();
The containing linear layout never expands to hold the layouts I've added, even though I'm calling invalidate().
And tips on why this is happening?
I have a layout which at start has '0dp' width but when a button is clicked it animates from Left to right and its width is increased accordingly. In this layout I am initializing a custom view. The view is initialized correctly when I place it at right or at centre but when I place it on the left it's not shown.
Here is my XML:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="#drawable/bg">
<com.example.Wheel
android:id="#+id/wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
</com.example.Wheel>
</RelativeLayout>
And here is what I do in java:
MenuListLeft = (RelativeLayout) findViewById(R.id.ControlLayout);
openButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isExpandedLeft) {
isExpandedLeft = false;
MenuListLeft.startAnimation(new CollapseAnimationLTR(MenuListLeft, 0,(int)(screenWidth*1), 5));
}
else {
isExpandedLeft = true;
MenuListLeft.startAnimation(new ExpandAnimationLTR(MenuListLeft, 0,(int)(screenWidth*1), 5));
init();
}
}
});
You would be better of using weights in a combination of Relative and Linear Layout rather than just in a Relative Layout.
Here...
weight = 0 gets you no space expansion. Try setting it to 1.
In case you want to understand more on how weight works:
http://teamdroid.wordpress.com/2012/06/06/android-layout-weight-explained/
Also, for your case, wouldn't a View.Visibility GONE, VISIBLE...with animation may work better.
I'm having a problem with adding elements in code. I want to add two buttons to a horizontal linear layout. My code works, but the second button partially covers the first button.
Question: How can I make it so that the second button doesn't cover the first button?
This is the code:
public class MainActivity extends Activity {
Button buttonFirst, buttonSecond;
LinearLayout lau;
LayoutParams params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonFirst = new Button(getApplicationContext());
buttonSecond = new Button(getApplicationContext());
lau = (LinearLayout) findViewById(R.id.layoutmadafaka);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(-30, 0, 0, 0);
buttonSecond.setLayoutParams(lp);
buttonSecond.setBackgroundColor(Color.BLACK);
lau.addView(buttonFirst,params);
lau.addView(buttonSecond);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<LinearLayout
android:id="#+id/layoutmadafaka"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
This must be done programmatically. When I use the bringToFront() method the buttons change positions but I don't want to do that. I want to first button on left side and second button next to first button.
I think its because of the negative margin you set :
lp.setMargins(-30, 0, 0, 0);
remove it or set them at 0 (its equivalent since 0 is the default value):
lp.setMargins(0, 0, 0, 0);
thoses negativbe margins displace the button2 to the left so its behind the other button.
You can't manage overlapping in LinearLayout (that's why a bringToFront action change order in the line and not overlapping order).
you need to use a RelativeLayout, set the button 2 to be on the right of button1, let the margin to -30 and call bringToFront on the second button it shoulc work ;)
You can set for your #id/layoutmadafaka the property android:weightSum=1.0 in xml.
And when you add the buttons, set for each layout_weight=0.5, layout_width=0.
Example here: Linear Layout and weight in Android
This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/blue"
android:gravity="center">
Is possible to set color from my Activity file?
For example I can do that for a button:
ImageButton x = (ImageButton) this.findViewById(R.id.btn1);
x.setBackgroundColor(color);
Yes, you can do it by
public class MainActivity {
private LinearLayout ll;
#Override
public void onCreate(...) {
super.onCreate(...);
setContentView(R.layout.your_layout_name);
ll = (LinearLayout) findViewById(R.id.linear_layout_id);
// You can set Background Color for your Linear Layout
ll.setBackgroundColor(Color.RED);
// You can set Image Also
ll.setBackgroundResource(R.drawable.imagename);
}
}
In XML file :
<LinearLayout
android:id="+#id/linear_layout_id"
android:width = "fill_parent"
android:height = "fill_parent"
android:orientation = "vertical"
/>
Hmm there is also setter for LinearLayout
setBackgroundColor()
Is inherited from the View class
Just like you did it for the button you can do the same for the the LinearLayout also. Give you Linearlayout an id and set the color:-
((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);