I'd like to change and align the text, and possibly add features on appbar in the screen above.
However, it'd be best if I could find and modify the text and align it IF I don't intend to add features.
But then I wasn't able to find where in android studio I could change the text.
I thought I could find one in the main activity xml file but I couldn't. I looked up at themes.xml and AndroidManifest.xml but couldn't find a clue.
Here is first 13 lines of my main activity xml file just in case.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="480dp"
app:layout_constraintTop_toTopOf="parent"/>
So, to cut it short, where in android studio can I change the text in toolbar? or appbar? above in the screen without having to create toolbar.xml? (+ align the text in the center and change text color)
Apparantly you don't have toolbar implemented in your xml layout
In Your style file make sure you set it to noActionBar
<style name="Theme.ChatApp" parent="Theme.AppCompat.Light.NoActionBar">
Then you have to add your own toolbar
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toolbar"
android:background="#color/white"
app:navigationIcon="#drawable/ic_baseline_dashboard_24"
app:title="here you can put your title"/>
In your activity reference your toolbar in your onCreate
setSupportActionBar(findViewById(R.id.toolbar)
PS : If you are using MaterialToolbar please consider add this material library
implementation 'com.google.android.material:material:1.1.0'
Related
I'm currently making an app for Android devices and I would like to know how can I change the way I display the fragments in differents display sizes (ex. Tablet).
At this point I have an activity for all my fragments and this causes me to replace the fragments when I change page but for tablets I would like to display 2 fragments inside that activity.
I've created a layout-large version for my main activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".PaginaInicial"
tools:showIn="#layout/app_bar_pagina_inicial">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="pt.ipp.estg.schoolhelperapp.apontamento.ListaApontFragment"
android:id="#+id/a_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
class="pt.ipp.estg.schoolhelperapp.apontamento.VerApontFragment"
android:id="#+id/b_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
The fragment container is inside layout-small version of the main activity.
One of the fragments displays a list of items (#+id/a_fragment) and the other one displays the information of one specific item that I previously selected (#+id/b_fragment).
For further details refer to this document: https://developer.android.com/training/multiscreen/screensizes
Create a default alias resource value file in res/values/refs.xml and place the following code inside it. This value will refer to your layout file that has only one fragment.
<resources>
<item name="activity_masterdetail" type="layout">#layout/layout_with_single_fragment</item>
</resources>
Now need to create an alternative resource so that the activity_masterdetail alias will point to layout_with_two_fragment.xml on larger devices.
Create a new file with same name refs.xml but with resource qualifiers for Smallest Screen Width under Available qualifiers. Use smallest dimension of 600dp or more (sw600dp qualifier)
<resources>
<item name="activity_masterdetail" type="layout">#layout/layout_with_two_fragment</item>
</resources>
Your goal here is to have logic that works like this:
For devices that are under a specified size i.e mobile phones, use layout_with_single_fragment.xml.
For devices that are over a specified size, use layout_with_two_fragment.xml.
Now in your man activity which shows a list, use setContentView(R.layout.activity_masterDetail) i.e the name of referenced layout
If this code will run on mobile phones, normal refs.xml file will be referenced by activity_masterDetailbut if this code runs on a tablet, resource qualified refs.xml file will be used.
In your activity logic, where you want to show master and detail layout on the same screen on tables but open another activity on phones, use the following logic inside onClick() of your list items
if (findViewById(R.id.a_fragment) == null) {
Intent intent = A_Activity.newIntent(this, listItem.getId());
startActivity(intent);
} else {
Fragment newDetail = B_Fragment.newInstance(list_item.getId());
getSupportFragmentManager().beginTransaction()
.replace(R.id.b_fragment, newDetail)
.commit();
}
You should take a look at this before link it should help you understand for sure more about supporting different screen sizes.
And here is an example that you can try.
I am here because I am trying to set programmatically the layout_below parameter of some cardViews without success.
These cardViews are located inside a PercentRelativeLayout (I do not know if it is relevant but these cardviews have been added programmatically too).
I show you the code to make it clear.
...
<android.support.percent.PercentRelativeLayout
android:id="#+id/prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv1"
// here I would like to add android:layout_below="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv2"
// here I would like to add android:layout_below="#+id/cv1"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
</android.support.percent.PercentRelativeLayout>
...
I tried to do something like this in my Java code but without success.
PercentRelativeLayout prl = (PercentRelativeLayout) view.findViewById(R.id.prl);
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
prl.addView(cv);
cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
RelativeLayout.LayoutParams cv_lp = (RelativeLayout.LayoutParams) cv.getLayoutParams();
cv_lp.addRule(RelativeLayout.BELOW,currentId-2);
/* I have also tried to call cv.requestLayout() but nothing changed */
prl.addView(cv);
As you can see, I am trying to generate cardviews and insert them one below the other.
Thanks in advance for your help.
PercentRelativeLayout is depreciated, so you are encouraged to use other type of layout.
This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
ConstraintLayout gives lots of opportunities.
More about PercentRelativeLayout deprecation here.
More about ConstraintLayout and how to use it here or video tutorial.
I'm working on a information orientated app using the master/detail flow and so far, so good. I would like to add images to the TextView, but it's formatted differently then what I've experienced in the past and I'm lost. from my understanding of what I've read while searching is that the scrolling text is "newer" when generating the Master/detail activity, therefore I haven't found any information on this specific issue. I would also like to pass the images in using the content activity, so it would be-
addItem(new Item(ID,Name,Detail,Image1,Image2));
what the detail XML file looks like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bobblehead_detail"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context="com.example.johnson.fallout4bobbleheadlocations.BobbleheadDetailFragment" />
I tried adding ImageView's under it, but I received errors.
tl;dr I would like to add 2 images under the scrolling TextView.
I am not sure if I clearly understand what you mean but looking at your xml it seems to me that you need to add a layout (either relative or linear) to your xml file and then add a textview and two imageviews (or whatever you want) into that layout.
Something like this:
<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"
android:padding="16dp" tools:context="com.example.somefragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id2"
android:text="Here is your text"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id"
android:src="#drawable/image1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id3"
android:src="#drawable/image2"/>
</RelativeLayout>
My plan is creating an Activity includes Tabs+Swipe for all Android version. If we set it from a default Android project, it has just support for at least API 11.
In Sherlock we have two project named : Tab Navigation, Tab Navigation(collapsed) includes Tabs but not Swipe.
They have Issue #240 in their samples that has a bug (swipe left/right when the tabs are in collapsed mode (landscape) and the selected item does not update).
Do you know any sample code that solve this problem?
You made this now with the default Android Support Library (or the ABS), with a ViewPager and a PagerTabStrip:
<?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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="#+id/tabStrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
Then create an Adapter to the ViewPager that extends of FragmentStatePagerAdapter (for example) and override the method public CharSequence getPageTitle(int position) to provide a title for each tab.
Hope it helps.
I also happened to have same situation. Follow below links and you will get all what you need.
See this tutorial
http://droidista.blogspot.com/2012/08/making-actionbarsherlock-and.html
And get demo code from here
https://github.com/zmdominguez/vpi-abs-demo
I found these links which might help you out.
A page flipper control - like the android home screens
https://github.com/JakeWharton/Android-ViewPagerIndicator
I've recently started working on an Android app. I'm not a programming noob, I have been programming in Java since 2009, but this is my first Android app. I have tried following some tutorials on how to set the background colour but it simply isn't working for me. I can't work out what I'm doing wrong.
In my layout directory I have files called: main.xml and view_fact.xml. They both use the linear layout and my LinearLayout Tag is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
And in my "values" directory I the contents of "strings.xml" is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Facts</string>
<color name="background_grey">#E8E8E8</color>
</resources>
But the background colour is not changing from the default black.
I've also tried adding: "android:theme="#android:style/Theme.Light"" in my AndroidManifest.xml file.
None of this is working, any suggestions?
Thank you.
On my view_fact.xml page it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fact:"
android:padding="10dp"
android:textColor="#080808"
/>
<TextView
android:id="#+id/factData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=""
/>
<Button
android:id="#+id/anotherFactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Another Fact"/>
</LinearLayout>
What's interesting is that the text in "factData" is grey, while the text: "Fact:" in the Text View above is white, and I've tried to change it's colour to red and it doesn't work. It stays white.
Any suggestions? I still haven't managed to get this working; thank you.
If you want to use #color/color_name you have to create a Color.xml file and then it will be accessible in the manner you tried to use it.
Forgive the somewhat obvious questions, but: are you calling setContentView(R.layout.main) in your Activity onCreate? Is there something else in the layout that is filling the LinearLayout and covering the background grey?
Looking at the pastie you linked in the comment I think you need to remove the <?xml version="1.0" encoding="utf-8"?> line at the start. Eclipse won't build a freshly created app with that xml root in a layout..
Although that's strange as the first two examples I've found on the Android Developer site (for example) include it
I'd be interested to see what happens if you remove that line...
In Eclipse if I use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E8E8E8"
>
then the background colour changes.
If I change that to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#string/background_grey"
>
by adding <string name="background_grey">#E8E8E8</string> to /res/values/strings.xml the background changes.
This also works if I declare it as android:background="#color/background_grey" in the layout and <color name="background_grey">#E8E8E8</color> in strings.xml
and if I setup res/values/colors.xml <= I've used colors.xml (while you've used color.xml) however http://developer.android.com/guide/topics/resources/more-resources.html tells us the filename is arbitrary as you can see by the declaration working in the strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_grey">#E8E8E8</color>
</resources>
and declare the layout as
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
then the background also changes...
If it doesn't behave the same for you then there's something funky going on or you're using a different layout than the simple example above and, well, there's something funky going on in that layout.
Okay, I've tried lots of different methods of trying to set the background colour and none of them are working. And I'm now having problems with text not updating. So I'm going to try remaking it. Using eclipse and ill make a new question if I'm still having issues. Thanks for the suggestions.