After implementing a title and subtitle for my toolbar, the wrong font size is used for some reason. What needs to be done in order to fix this?
Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar customToolbar = (Toolbar)findViewById(R.id.toolbarC);
customToolbar.setBackgroundColor(Color.parseColor("#E21836"));
TextView mTitle = (TextView) this.findViewById(R.id.toolbar_title);
mTitle.setText("Hello World");
mTitle.setSingleLine(true);
TextView mSubtitle = (TextView) this.findViewById(R.id.toolbar_subtitle);
mSubtitle.setText("Hello World");
mSubtitle.setSingleLine(true);
}
}
toolbarC.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarC"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/header_text_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
<TextView
android:id="#+id/toolbar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
add textSize to each textViews. Try like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_2lines"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/header_text_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
<TextView
android:id="#+id/toolbar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"/>
</LinearLayout>
add this line, probably it's using app_name as title
getSupportActionBar().setDisplayShowTitleEnabled(false);
and try using dp instead of sp
Found the problem and have solved it. There was a hidden duplicate of the toolbar text views within my project and the duplicate title & subtitle were what was shown to me when deploying my app.
Increase Toolbar's Height to ?actionBarSize
Related
I put the photo of the account in the navigation header. The border is rectangular, I would like to make it rounded, how can I do that?
In this code I used Glide to display the image.
my_header_navigation.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"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#color/common_google_signin_btn_text_dark_focused"
android:id="#+id/navigation_header">
<ImageView
android:id="#+id/photoImageView"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="18dp"
android:background="#color/colorAccent"
android:visibility="invisible" />
</RelativeLayout>
and in the Main Activity:
private ImageView photoImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
View hView = navigationView.getHeaderView(0);
photoImageView = hView.findViewById(R.id.photoImageView);
if(user != null){
Glide.with(List.this).load(user.getPhotoUrl()).into(photoImageView);
photoImageView.setVisibility(View.VISIBLE);
}
else{
photoImageView.setVisibility(View.INVISIBLE);
}
Your code i'ts right, you need implement CircleImageView, I show you how to implement.
CircleImageView - GitHub
nav_header_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile_image"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
</LinearLayout>
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CircleImageView circleImageView = (CircleImageView) findViewById(R.id.profile_image);
Glide.with(List.this).load(user.getPhotoUrl()).into(circleImageView);
}
My NavigationDrawer with CircleImageView
Link Image
NOTE
Remember, your image need proportional dimensioned, example (100x100, 200x200), like a perfect square, I hope I have help you.
I'm working on a gallery type app. The issue that I have at the moment is trying to fetch the typed text within an EditText. The error that I am getting is a casting exception because the code fetches the EditText block from the layout as a TextView block. This only occurs after I swipe, which causes the some of the LinearLayouts imbeded in the second GridLayout to be removed.
The LinearLayouts are put in at runtime into the Gridlayout (with the id product_details), the amount Layouts is calculated according to which picture is pressed on.
This part of the code fetches the current LinearLayouts that are imbeded in the GridLayout:
for (int i = 0; i<codes.size(); i++) {
//Sets the different elements to te correct texts
ViewGroup linearLayout = (ViewGroup) gridLayout.getChildAt(i);
TextView textCode = (TextView) linearLayout.getChildAt(0);
TextView textPrice = (TextView) linearLayout.getChildAt(1);
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
commentsAddList.add(editTextC.getText().toString());
quantityAddList.add(editTextQ.getText().toString());
priceAddList.add(textPrice.getText().toString().substring(1));
codeAddList.add(textCode.getText().toString());
productModelAddList.add(new ProductModel());
productModelAddList.get(i).setCode(codeAddList.get(i));
if(!quantityAddList.get(i).equals("")) {
productModelAddList.get(i).setQuantity(Integer.parseInt(quantityAddList.get(i)));
}else{
productModelAddList.get(i).setQuantity(0);
}
productModelAddList.get(i).setComment(commentsAddList.get(i));
if(!priceAddList.get(i).equals("")) {
productModelAddList.get(i).setPrice(Double.parseDouble(priceAddList.get(i)));
}else{
productModelAddList.get(i).setPrice(0);
}
mRequestPermissions();
adaptToText.addToList(productModelAddList.get(i));
}
The main picture xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageLayout"
android:columnCount="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fitsSystemWindows="true">
<ImageView
android:layout_columnSpan="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"/>
<GridLayout
android:id="#+id/product_details"
android:columnCount="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</GridLayout>
</GridLayout>
The LinearLayout that is inserted into the GridLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_columnSpan="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:id="#+id/orderSideLayout">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productCode"
android:text=""
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productPrice"
android:text=""
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:hint="Quantity goes here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/comments"
android:text="Comments: "
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editTextComments"
android:hint="Comments here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
I'm not sure where the casting exception is caused or why exactly it happened. If anyone is able to help, I would appreciate it a lot! Thanks in advance!
Instead of getting the view by position getting it by id would be easier.
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
Try this instead:
EditText editTextQ = (EditText) linearLayout.findViewById(R.id.editText1);
EditText editTextC = (EditText) linearLayout.findViewById(R.id.editTextComments);
This will be better because if change the xml, it could change view positions, and find views by id you wouldn't need to change the code.
I am trying to create a TextView programmatically which will be shown in another layout that doesn't belong to the same activity but the TextView isn't shown. Following is the code:
Activity:
public class LogActivity extends AppCompatActivity {
LinearLayout textLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
LayoutInflater layoutInflater=getLayoutInflater();
View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null);
textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout);
textView=new TextView(this);
textView.setText("TextView");
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout.addView(textView);
}
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#color/editTextBG">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
That's because your container layout, has other children whose heights are set to match_parent. You won't be able to see TextView you add in Java code since the other children added in XML fill all the screen.
You need to add textEntryLayout to your layout. You are modifying that inflated view without adding it to (e.g.) a layout in your layout_one.
Try:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1);
layout.addView(textEntryLayout);
I've been trying to set the title of my toolbar to start at the top of the toolbar, but while the I have set android:gravity="top" the text still start at the center (vertical) of the toolbar. I think this could have something to do with using a custom layout for the toolbar, but I use it because I need it, so I hope someone knows a way to set the text to start at the top.
Here is layout my toolbar is in:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutProjectOverview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:title="#string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="#style/toolbarTitleText"
android:background="#drawable/toolbar_basic"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutObjectNav"
app:layout_widthPercent="63%"
app:layout_aspectRatio="400%"
android:layout_toRightOf="#id/toolbar2"
android:layout_toEndOf="#id/toolbar2"
android:background="#drawable/border_basic">
<!-- Some irrelevant xml code -->
</android.support.percent.PercentRelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/layoutObjectNav">
<!-- Some more irrelevant xml code -->
</RelativeLayout>
</android.support.percent.PercentRelativeLayout>
<!-- Some more irrelevant xml code -->
</RelativeLayout>
Here is the custom toolbar layout code:
<?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">
<TextView
android:id="#+id/action_bar_title"
android:textSize="20sp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="top"
android:padding="5dp"
android:ellipsize="end"
android:maxLines="3"/>
</LinearLayout>
And here is the code where I set the custom layout (and some other stuff):
#Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.layout_toolbar);
((TextView) findViewById(R.id.action_bar_title)).setText(currentProject.getTitle());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
} else {
getSupportActionBar().setTitle(currentProject.getTitle());
}
// Some irrelevant java code
}
Does someone know what I'm doing wrong?
EDIT:
Here is the TextAppearance Style I use for the toolbar:
<style name="toolbarTitleText">
<item name="android:textSize">20sp</item>
<item name="android:maxLines">3</item>
</style>
add a textView in your toolbar
like this:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
Try to do the following :
1- Disable the title by using getSupportActionBar().setDisplayShowTitleEnabled(false);
2- Add a textView under your toolbar as your toolbar title :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v7.widget.Toolbar>
I need a TextView which scrolls automatically vertically. This is my layout code:
activity_info.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:contentDescription="#string/info"
android:src="#mipmap/infoImage" />
<TextView
android:id="#+id/tView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:maxLines="500"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_below="#+id/tView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:id="#+id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="YES" />
<Button
android:id="#+id/buttonNo"
android:layout_toEndOf="#id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="NO" />
</RelativeLayout>
</RelativeLayout>
I read that I have to input this line:
tView.setMovementMethod(new ScrollingMovementMethod())
into my onCreate method in my activity:
InfoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
TextView tView = (TextView) findViewById(R.id.tView);
tView.setMovementMethod(new ScrollingMovementMethod());
}
If I do that, the TextView does not scroll automatically, if some new input (text) is inserted into the View. I know there are some other questions concerning the problem. But I've tried most of them.
How can I solve this problem?
Try to set the textView to android:gravity="bottom"
and use textView.append("\n"+"New Input Text."); for new input text;
You are almost there. Just add a few more things to achieve this,
First of all add those properties in your TextView
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
android:gravity="bottom"
Secondly as usual use,
tView.setMovementMethod(new ScrollingMovementMethod());
This should solve your problem.
I would suggest wrapping your TextView in a ScrollView
<ScrollView
android:id="#+id/sView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/tView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="500"
android:scrollbars="vertical"
android:gravity="bottom" />
</ScrollView>
You can append new text by doing
public void appendAndScrollDown(String someText) {
tView.append("\n" + someText);
sView.fullScroll(View.FOCUS_DOWN);
}