Adding unique buttons dynmaically - java

This is what I have right now:
When I click Add a Class, I want another button to appear directly above the 'Add a Class' Button. This newly created button, when clicked on, will switch to an activity that shows all the assignments for that class(new button). I think I have an idea how to do this, but only in its most basic form.
I guess there's really 3 things I'm unsure about:
1) How to add a button at a specific location within an xml file through java?
2) How do I add buttons with unique identifiers?
3) How do I click on the newly created buttons so that a new activity opens which only shows the assignments for that button?
This is how my xml layout currently looks:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/mLlayoutBottomButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="Classes"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/roundedcorners"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:drawableRight="#drawable/rightarrow"
android:gravity="left|center_vertical"
android:text=" Math" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/roundedcorners"
android:textStyle="bold"
android:padding="10dp"
android:text="Add a Class" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

You want to add the button the the linearLayout I guess (id mLlayoutBottomButtons). In your activity you do this:
LinearLayout buttonsLayout = (LinearLayout) findViewById(R.id.mLlayoutBottomButtons);
You will dynamically create buttons, you can give it an id (button.setId(int id)), but it is not necessary. Just create and add it like this
Button newButton = new Button(this);
newButton.setText("New button");
buttonsLayout.addView(newButton, parentView.getChildCount() - 2); //see Edit at end of post
Now we created the button and added it to the layout. As last step you just set an OnClickListener to this button. Inside the onClick-method you can start the activity with an intent and add Extras in order to determine which assignment should be shown in the next Acitivty.
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra("assignmentType", "theNewButtonsAssignments");
startActivity(intent);
in the new Activity call getIntent().getStringExtra("assignmentType") to retrieve which assignment should be shown.
Edit:
You can use addView(View child, int index) for inserting it in a certain position inside the linearLayout.

Related

Toggle child TextView inside CardView based on visibility

I have the following CardView:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="expandCollapse">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:text="Heading Goes Here" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_action_down" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Description Goes Here"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
Now, there are many of these cardviews, so I want to make my expandCollapse function work with all of them.
So basically, when the CardView is clicked, I want it to show or hide the Description Goes Here TextView depending on it's current visibility. It will either be GONE or VISIBLE. I also want it to change the ImageView arrow based on the current visibility.
Basically I just want this card to expand/collapse the textview inside of it. I know how do to this, but I don't know how to select the child TextView dynamically inside of my function.
How exactly do I select this TextView when the card is clicked, considering I will not be specifying an id for it, since I want this to work with many different CardView having the exact same layout?
I figured it out.
I simply added a tag to the TextView with android:tag="desc"
Then I used TextView textView = view.findViewWithTag("desc"); in my onClick function.

Creating nested Linear layout dynamically with weights to have multiple buttons on same line

I'm trying to create a toggle button that will display more buttons and some TextView like shown below.
With the toggle button ON it will display the TextViews and 3 extra buttons, once switched off it will close those, Rearranging the toggle buttons below it as shown.
The part I specifically need help with is creating the second linear layout and setting weightings for each of my buttons?
Public void toggleswtich(){
layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
Button enterJob = new Button(Reports.this);
layout.setLayoutParams();
// if (button.isActivated()) {
enterJob.setText("Open");
layout.setWeightSum(1);
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
// }
// else {
enterJob.setText("Clsoe");
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
//}
}
As requested this is my xml, however there isnt much to show as everything is created with code in run time once a Job is saved.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">
<TextView
android:textSize="40dp"
android:layout_width="wrap_content"
android:text="Username"
android:layout_height="wrap_content" />
<EditText
android:textSize="40dp"
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:nestedScrollingEnabled="false">
<LinearLayout
android:id="#+id/ReportsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:textSize="40dp"
android:id="#+id/bEmptyJN"
android:text="Enter Job number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<requestFocus
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here's what it looks like with jobs, picture the jobs as toggle buttons
Let me know if you'd like anymore information

XML or JAVA code for switching elements in ANDROID

Very new to programming here. I am trying to program an activity for Android in .xml and .java and I'm trying to figure out if it is possible to switch one element for another by pressing a button.
The screen I am working on will have an option to search through a list of items and the option to add items to this list. I would like the search bar and any adding options to be hidden by default, with the option to show either of them by pressing the according button. Press the search options button and the search bar should appear, press the adding options button and the search bar should be hidden and in its place should show adding options instead.
Is this possible in .xml or .java and if so what kind of elements or methods should I be looking into to achieve this? Below you will find the code that I have so far in .xml, where the editText1 should be the search bar and which should be replaced with adding options if imageButton2 is pressed.
Many thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bgmenu"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:scaleType="fitStart"
android:src="#drawable/bproductsbanner"
android:paddingStart="5dp"
android:paddingTop="15dp"
android:id="#+id/ibProductsBanner"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bgminibutton" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:background="#00000000"
android:src="#drawable/bssearch" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="#+id/imageButton1"
android:background="#00000000"
android:src="#drawable/bsadd" />
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Search.." />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product List"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
android:gravity="center"/>
</LinearLayout>
Suppose you have defined the search bar in your xml as an EditText
<EditText android:id="#+id/search" ...
Then in your activity (java) you get a reference to it by doing
View search = findViewById(R.id.search);
and then when clicking the respective button you can play with the visibility attribute
search.setVisibility(View.VISIBLE); // View.GONE
You have to set the visibility as gone in your xml to your search bar:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Search.."
android:visibility="gone"/>
And then you have to change visibility when you click the button:
ImageButton ib = (ImageButton) findViewById(R.id.imageButton1);
final EditText search = (EditText) findViewById(R.id.editText1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search.setVisibility(View.VISIBLE);
}
});
P.s. I'm supposing that your search bar is the EditText with id editText1 and your option button is the ImageButton with id imageButton1

How can one set a OnLongClickListener to a Button in a ListView?

So what I am trying to do is, to set an OnLongClickListener to a ListView Button. I managed to put an OnClickListener to it and I also tried to put an OnLongItemClick to my whole ListView, but the problem is, that the OnLongItemClick is never called when I press the certain Button in the Item.
I was able to put the OnClickListener to the Buttons in the XML-File of the ListView-Rows by using android:onClick="myOnClickListener", but this doesn't work with the OnLongClickListener.
I also tried setting the OnLongClickListener to the button in the normal OnClickListener, but that's not what I really want because it just works after a normal click on it.
Is there any way to set the OnLongClickListener to the Buttons programmatically? They have to be unique in each row tho. Btw im not extending ListActivity.
Here is the OnClickListener for the Button:
//I attached this in my XML-File to the Button
public void handlerForPlus(View v)
{
//The LinearLayout where all my Items are in
LinearLayout myItemLayout = (LinearLayout)v.getParent();
//A random EditText in my Layout
editTextCountItem = (EditText) myItemLayout.getChildAt(0);
//The Button i want to add the OnLongClickListener to
buttonItemPlus = (Button)myItemLayout.getChildAt(1);
//Here i set the OnLongClickListener to the button, but I want to do this before it gets clicked normally.
buttonItemPlus.setOnLongClickListener(this);
editTextCountItem.setText((Integer.toString(Integer.parseInt(editTextCountItem.getText().toString()) + 1)));
myItemLayout.refreshDrawableState();
}
Here is my XML-File for the LinearLayout of the ListView-Items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="40" >
<EditText
android:id="#+id/etAnzahl"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:text="0" />
<!-- This is the Button i want to set a OnLongClickListener to. -->
<Button
android:id="#+id/bItemPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="25"
android:onClick="handlerForPlus"
android:text="Item" />
<Button
android:id="#+id/bItemMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="handlerForMinus"
android:text="-" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="10"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="#+id/tvPriceItemSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisS" />
<TextView
android:id="#+id/tvPriceItemLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisL" />
</LinearLayout>
</LinearLayout>
So does anybody know what could solve my Problem?
Best Regards, Alex.
buttonItemPlus.setOnLongClickListener(this); has the effect for entire ListView. What I think you're looking for is buttonItemPlus.setOnItemLongClickListener(this); which has the effect for particular TextView in your ListView.

Java/Android: When I put a ScrollView - I get a soft keyboard. Can't make a good layout

I have a problem with the layout. I want to place one textedit and one button at the bottom of my activity. And then I want a TextView component that fills all free space on my activity's layout.
I have a big text in the bigText component that's why I need to use the ScrollView.
I have a few questions:
I don't understand, why I get a soft keyboard when I place the ScrollView component? But I don't get this keyboard without the scrollview! If I turn off the soft keyboard, like this:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
Then how I can use my EditText?
Could you help me with my layout? The problem is: when I use the ScrollView - it fills all phone's display I even see my button and EditText which should place below the ScrollView (i don't see completely bottom of my layout). How can I fix this?
And that's my fail attempt of layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bigText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search string: " />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center_vertical|center_horizontal" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go >>" />
</LinearLayout>
</LinearLayout>
I'm not sure why adding a ScrollView would make the soft keyboard pop up, but it's easily remedied. Turn it off programmatically like this:
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getApplicationWindowToken();
if (binder != null) {
manager.hideSoftInputFromWindow(binder, 0);
}
where view can be your ScrollView or a TextView or just about anything else
Simply:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

Categories