I'm working on an application that has a navigation drawer and where the user is required to login.
So far, I've got the whole user login/registration thing working and I've built the navigation drawer.
What I want to do now is to make the TextViews on the navigation drawer display the user's full name and his/her email.
I've tried to do so with the LayoutInflater, but the TextViews don't change.
I've also tried passing the user information to LogCat and it displays it correctly.
I don't know where my problem is.
Here is my code:
DrawerActivity.java
public class DrawerActivity extends AppCompatActivity{
public static String[] userInfo = LoginActivity.userInfo;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.navDrawerTheme);
setContentView(R.layout.activity_drawer_layout);
//Declare instances.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
mNavigationDrawer = (LinearLayout) findViewById(R.id.navigationDrawer);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View drawerHeaderView = inflater.inflate(R.layout.drawer_header, null);
TextView tvFullName = (TextView) drawerHeaderView.findViewById(R.id.name);
TextView tvEmail = (TextView) drawerHeaderView.findViewById(R.id.email);
ImageView imgProfile = (ImageView) drawerHeaderView.findViewById(R.id.imgProfile);
tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);
Log.d("First name", "First name: " + userInfo[0]);
Log.d("Last name", "Last name: " + userInfo[1]);
Log.d("Email", "Email: " + userInfo[2]);
...
EDIT
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#444444">
<RelativeLayout
android:id="#+id/userInfoWrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toEndOf="#+id/imgProfile"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:singleLine="true"
android:textSize="14sp"
android:textStyle="bold"
android:layout_marginTop="40dp"/>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:singleLine="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textStyle="normal"
android:layout_below="#id/name"/>
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="75dip"
android:layout_height="75dip"
android:src="#drawable/ic_face_white_48dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:id="#+id/imgProfile"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
EDIT 2
activity_drawer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Activity Content-->
<FrameLayout
android:id="#+id/activity_frame"
android:layout_height="match_parent"
android:layout_width="match_parent">
</FrameLayout>
<LinearLayout
android:id="#+id/navigationDrawer"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="270dp">
<include
android:id="#+id/drawer_header"
layout="#layout/drawer_header" />
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:groupIndicator="#android:color/transparent"
android:divider="#android:color/transparent"
android:background="#444444"/>
</LinearLayout>
Thank you for your time.
I think you do not need to use LayoutInflater, simply try this and let me know
TextView tvFullName = (TextView)findViewById(R.id.name);
TextView tvEmail = (TextView)findViewById(R.id.email);
ImageView imgProfile = (ImageView)findViewById(R.id.imgProfile);
tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);
for your clarification it worked because it is already defined in a layout so you can access it directly. LayoutInflater is used in case you want to add some views runtime using defined layout.
You could try calling
drawerHeaderView.invalidate();
after setting the text to refresh the views, since the layout has already been inflated.
I'm probably wrong but it could potentially be an issue with the way you are getting an instance of LayoutInflater. Try
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
Attach the inflated view to a parent view like this:
RelativeLayout userInfoRL = (RelativeLayout)findViewById(R.id.userInfoWrapper);
userInfoRL.addView(drawerHeaderView);
//just in case use the userInfoRL.invalidate();
Try it and let me know if it works.
Related
I have a horizontalScrollView in my android app that I am trying to dynamically put a cardview with an imageView and a textView inside of the Horizontal scrollView when a button is pressed. When my app is first started up you see just a button named on the bottom of the app "findCar". When the button is pressed a method called buildCarView is called tha should put a cardView, textview and another button inside the upper portion of the screen (above the findCar button). But for some reason when i press the button nothing appears even though when I debug it the method to inflate the View is being called and the cardview/textView and the other button is being populated.
Here are the two XML's
This is the first thing you see when you open the app. It called dailouge_container.xml
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<Button
android:id="#+id/find_car_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="292dp"
android:text="Find Car"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView"
app:layout_constraintVertical_bias="0.0" />
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="310dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is the XML am trying to dynamically add when the button findCar its called Card.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="280dp"
android:layout_height="230dp"
android:id="#+id/cardView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageViewScroll"
android:layout_width="270dp"
android:layout_height="90dp"
/>
<TextView
android:id="#+id/price"
android:layout_width="270dp"
android:layout_height="50dp"
android:text="Price"
android:textSize="25dp">
</TextView>
<TextView
android:id="#+id/makeandmodel"
android:layout_width="270dp"
android:layout_height="50dp"
android:text="Make and Model"
android:textSize="22dp">
</TextView>
<Button
android:id="#+id/rentCarButton"
android:layout_width="270dp"
android:layout_height="35dp"
android:layout_marginBottom="4dp"
android:text="Rent Car"
android:textColor="#color/cardview_dark_background"
android:textSize="14dp"
></Button>
</LinearLayout>
</androidx.cardview.widget.CardView>
Below is what im doing in the OnCreate
LinearLayout linearLayout;
HorizontalScrollView horizontalScrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialoge_container);
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
userId = currentUser.getUid();
DatabaseReference lendersToRentFrom = FirebaseDatabase.getInstance().getReference().child("Users").child("Lenders");
GeoFire geoFire = new GeoFire(lendersToRentFrom);
horizontalScrollView = findViewById(R.id.scrollView);
linearLayout = findViewById(R.id.container);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
Below is the findCar method that should dynamically add the cardView/textveiw and other button the horizontal scrollView
public void buildCardView(){
View view = getLayoutInflater().inflate(R.layout.card,null);
CardView cardView = view.findViewById(R.id.cardView);
Button rentCarButton = view.findViewById(R.id.rentCarButton);
TextView price= view.findViewById(R.id.price);
TextView makeandmodel = view.findViewById(R.id.makeandmodel);
ImageView imageView =view.findViewById(R.id.imageViewScroll);
linearLayout.addView(view);
}
This question already has answers here:
create a chatView layout in android
(4 answers)
Closed 4 years ago.
in android i am trying to change the layout base on who the message is from.
if the message is from me then display the layout mymessage.xml to the right else display message.xml to the left.
i used if condition, but i don't know how to display one layout to the right and the second to the left,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO
View messageView = null;
// Get a reference to the LayoutInflater. This helps construct the
// view from the layout file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Change the layout based on who the message is from
if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.mymessage , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
return messageView;
}
try this layout for mymessage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/mytimeTextView"
android:alpha="0.5"
android:layout_alignParentRight="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/mytimeTextView"
android:id="#+id/mymessageTextView"
android:layout_alignParentRight="true"
android:text="mymessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and similarly this for message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/timeTextView"
android:alpha="0.5"
android:layout_alignParentLeft="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/timeTextView"
android:id="#+id/messageTextView"
android:layout_alignParentLeft="true"
android:text="yourmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
for message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/receive_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/message_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="#+id/tv_receiver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="User 1"
android:textColor="#color/NavDrawerTextColor"
android:textSize="14dp"/>
<TextView
android:id="#+id/tvmessagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Hi"
android:textColor="#color/MessageText"
android:textSize="14dp"/>
<TextView
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="04:16 pm"
android:textSize="10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/send_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:background="#drawable/message_sender_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="#+id/tvmessagetext_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="user 2"
android:textColor="#color/White"
android:textSize="14dp"/>
<TextView
android:id="#+id/timestamp_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Hello"
android:textColor="#color/White"
android:textSize="10dp"/>
</LinearLayout>
</RelativeLayout>
for java class: getview()
convertView = inflater.inflate(R.layout.message,parent, false);
if (messages.get(position).fromMe()) {
// for reference receive_ll is GONE and send_ll layout is VISIBLE
} else {
// for reference receive_ll layout is VISIBLE and send_ll layout GONE
}
For left.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:background="#drawable/textview"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sampleleft"
android:textColor="#000" />
</RelativeLayout>
For right.xml
<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/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:background="#drawable/textview"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sample"
android:textColor="#000" />
</LinearLayout>
just replace this
// Change the layout based on who the message is from
if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.left , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.right , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
Just make it simple .
create single layout contain two category
set the data's in the fields
And VISIBLE GONE the layout by the type
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/sender_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//sender side
</LinearLayout>
<LinearLayout
android:id="#+id/reciver_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//reciver side
</LinearLayout></LinearLayout>
if the message type is sender means visible thesender_layout else visible the reciver_layout
when you getting myMessage that time show mymessage layout other layout will hide like make below codition ...
if (messages.get(position).fromMe()) {
myMessageView = inflater.inflate(R.layout.mymessage , parent, false);
myMessageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
messageView.setVisibility(View.GONE);
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
messageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
myMessageView.setVisibility(View.GONE);
}
I want to create a scroll view list which would show "boxes" or more accurately custom made layouts (I'm using a custom class that extends a RelativeLayout - basically shows different pieces of information, some are static like places' working hours and some change dynamically and will be pulled from a server).
Though I encountered a problem - I (for the sake of seeing if my solution works) created 5 boxes and added them to the scroll view list but it seems like they are stacked upon each other. What's the proper way to make them appear one under another without manually tweaking their position coordinates? I was using addView() for that purpose but it doesn't work as intended for me or I use it poorly. If you know the answer, please briefly describe how this should be done.
Thanks in advance!
EDIT, here goes the code:
public class RestaurantBox extends RelativeLayout {
RestaurantBox (Context context){
super(context);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName = new TextView(context);
restaurantName.setText("Test Restaurant");
RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(restaurantName, restNameParams);
restaurantName.setTypeface(null, Typeface.BOLD);
TextView freeSpots = new TextView(context);
freeSpots.setText("15/20");
RelativeLayout.LayoutParams freeSpotParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
this.addView(freeSpots, freeSpotParams);
TextView book = new TextView(this.getContext());
RelativeLayout.LayoutParams bookParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
book.setText("Book");
bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
this.addView(book, bookParams);
}
}
public class BrowseRestaurants extends AppCompatActivity {
int restaurantCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_restaurants);
Intent intent = getIntent();
String login = intent.getStringExtra("LOGIN");
TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
text.setText("Welcome, " + login);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox = new RestaurantBox(this);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);
for(int i=1;i<5;i++){
final View view = relativeLayout.getChildAt(i-1);
RestaurantBox restaurantBox = new RestaurantBox(this);
restaurantBox.setTop(view.getBottom() + 50);
restaurantBox.setBottom(restaurantBox.getTop() + 300);
relativeLayout.addView(restaurantBox);
}
}
}
<!-- activity_browse_restaurants.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/txtLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Logout"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/holo_blue_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp" />
</RelativeLayout
>
you are adding view in RelativeLayout so view stacking on each other so change it to LinearLayout in activity_browse_restaurants.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
now make changes according to in BrowseRestaurants.class
replace
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
with
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);
else will be fine if you want to change variable name its up to u, let me know if any problem
I'm trying to create my own custom app bar with a center-aligned title. Android Studio's editor shows my title is properly centered, but it is not on my actual device.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.encounter_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.encounter_menu_bar_layout);
}
encounter_menu_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
encounter_menu_bar.xml doesn't have anything in it:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>
EDIT
A lot of you are recommending that I change android:layout_gravity to android:gravity. I've tried that as well and I'm still not getting it to line up properly.
I'm enabling the app bar to show up on a specific fragment, as shown here:
EncounterFragment.java
public class EncounterFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_encounter, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.encounter_menu_bar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO: This
return super.onOptionsItemSelected(item);
}
}
I also forgot to mention that I'm attaching the menu in a fragment. This is its layout:
fragment_encounter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/encounter_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
Try adding android:gravity="center".
This happens because you have to center the whole text and not just the layout
Use android:layout_gravity="center"
Example:
<LinearLayout 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"
android:orientation="vertical">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
NOTE: layout_gravity, not gravity
In your case:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
Change the textview to this
make it match parent for width, And make gravity center to center the text
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:gravity="center" />
You want to change your xml view to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
#this should be horizontal, not vertical.
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
/>
To start, you need to make your linear layout horizontal, not vertical. By making it vertical, you are aligning it to the left.
Then, you need to make sure that your textview width and height match the parent, rather than just wrap the content. Then you can use the gravity attribute to make sure that the text is centered. When you used gravity before, the textview simply wrapped the content, and thus it didn't center in the textview. By matching the parent view bounds, you will be able to then center the text as you need to.
This is how your Toolbar XML should be
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:textColor="#android:color/white"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
Then in your Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?
Here is my XML file so far:
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
use the below code. This will satisfy your requirement. I tried this and working for me.
Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="ListView Heading" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="ListView Footer" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_above="#id/textView2"
></ListView>
The UI will like this
Try this way, hope this will help you to solve your problem.
Instead of using header/footer just put as below code in your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/header_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_transaction_string">
</TextView>
</LinearLayout>
Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.
1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="#+id/header" and layout_above="2+id/footer"
I found a possible solution for your problem from a similiar post. Hope this helps you.
For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle
<RelativeLayout ...>
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_view"
android:layout_above="#+id/footer_view">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
</RelativeLayout>
You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".
Get all the elements in a array: Example:- (weatherArray)
Loop through all the elements :-
Example:-
mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
mainLayout.removeAllViews();
for (int i = 0; i < weatherArray.length(); i++) {
View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false);
TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
todayTempStatus.setText("");
}
This is an example without using listview, which we will populate lienarlayout using child view.