Hi i want to put button beside activity title in android. I can change the text using setTitle() but I haven't find anything which allows to put button beside title of Activity. I am using android 4.0.3. Can anybody help me?
Create xml file in your project name topbar.xml
<?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="40dp"
android:orientation="vertical"
android:background="#android:color/white">
<TextView
android:id="#+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyTitle: NameOfUser"
android:layout_centerVertical="true"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="20dp"/>
<Button
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Btn"
android:textColor="#android:color/white"
android:layout_marginLeft="5dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
And add this layout in any layout by just writing:
<include layout="#layout/topbar"/>
Hope this will help you.
You can use custom title bar or you can use no title theme for your Activity and set a linear layout as child layout above of your Activity's layout,so it seems that your activity has title bar.
Creating custom title bar:
edumobile.org
londatiga.net
Hidding the title bar of your application.
Related
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.
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.
Today I changed my Toast to Crouton.
But I had these problems :
1)Showing Crouton in center
2)Whenever I want to display new Crouton it is displaying previous Crouton again, though previous one hided long ago
I want to know if there is better way to solve above problems?? Cause I looked at sample and library but could not find"
Currently I'm solving these ways
activity layout:
<RelativeLayout
android:id="#+id/bigcontainer"
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"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DilmancSRActivity"
tools:ignore="MergeRootFrame" />
<RelativeLayout
android:id="#+id/alternate_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Creating my custom toast view and centering it horizontally:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/kdrw"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
android:contentDescription="#string/strnull"
android:src="#drawable/logo" />
<TextView
android:id="#+id/toasttextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Showing Crouton:
LayoutInflater inflater = localdsr.getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, null);
TextView text = (TextView) layout.findViewById(R.id.toasttextView1);
text.setText(info);
if (toast != null)
toast.cancel();
Configuration croutonConfiguration=new Configuration.Builder().setDuration(1500).build();
toast=Crouton.make(localdsr, layout ,R.id.alternate_view_group ,croutonConfiguration);
toast.show();
For solving the Second one I had to download library source and use it. .As I do not want to use animations firstly, I disabled animations
//croutonView.startAnimation(crouton.getInAnimation());
issue on removing function that was making things appear again inside
protected void removeCrouton(Crouton crouton)
//sendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.getOutAnimation().getDuration());
//I changed it with below .plus removed crouton.getOutAnimation().getDuration() so it does not throw exception
sendMessage(crouton, Messages.DISPLAY_CROUTON);
I believe I am missing something. Hope there is already right way to do those .
Making changes within the library itself is not encouraged as it might create issues that only appear within your version of Crouton.
You can change the Animation via Configuration.Builder.setInAnimation(...) and .setOutAnimation(...).
The Configuration can be re-used and does not have to be created every time you want to show the Crouton. The same is valid for your View. It does not have to be inflated every time.
Regarding the duplicate display of a Crouton: This behavior is not intended.
The code above looks like it should display a Crouton with the info text set. If your info text has changed during the display of Croutons it might be a bug within the library. If that is the case, feel free to report it.
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 );
This makes no sense. I have some code that has a simple LinearLayout and inside it there is a TextView and a ScrollView and inside the ScrollView there is a button. Well when I add the second button I get an error saying that ScrollView can only have one child. Is a Button considered a different child??? I'm sorry for my stupidity if this is really simple. If anyone can help THANKS!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:duplicateParentState="false"
android:fadeScrollbars="true"
android:gravity="center"
android:isScrollContainer="true"
android:orientation="vertical"
android:scrollbars="vertical">
<TextView
android:text="Select category:"
android:textStyle="italic"
android:textColor="#000000"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdgeLength="100dp">
<Button
android:id="#+id/Food"
android:layout_width="175dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/blackbutton"
android:text="Food"
android:textColor="#ffffff" />
<Button
android:id="#+id/Clothing"
android:layout_width="175dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/blackbutton"
android:text="Clothing"
android:textColor="#ffffff" />
</ScrollView>
</LinearLayout>
Your answer is literally in the documentation of a ScrollView:
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
In other words: you'll need to put the two buttons inside a layout of your choice, that on its turn is the sole child of the ScrollView.
ScrollView must have 1 child. So put the two buttons inside a layout.
put those two buttons inside a linear or relative layout.... and put that layout inside the scroll view. this should solve the issue.
Scroll view can only have one child control...