This question already has answers here:
How to include layout inside layout?
(6 answers)
Closed last year.
I have my main activity. Inside this main activity I want to create an unknown number of layouts that each one of them include one button.
I want smart way to do it - make the layout one time and than use it a lot of time.
What is a good way to do it?
Thanks
Make One Layout Resource File Using Below Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp"
android:layout_marginTop="#dimen/_15sdp"
android:gravity="center"
android:textColor="#color/black"
android:textSize="#dimen/_13sdp" />
<Button
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="Submit"
android:textColor="#color/white"
android:textSize="#dimen/_10sdp" />
</LinearLayout>
And In Your Main Activity Layout You Can Use Only
<include layout="#layout/layoutresourcefilename"/>
Trying to be smart commonly results in the opposite. Better keep it simple;
Which means, just add three buttons and then show either one of them.
This has the advance, that the events are already bound, ready to click.
And also, meanwhile it's a whole lot more common to inflate Fragment
or to data-bind views, which would permit for hiding/showing buttons.
Related
This question already has answers here:
How to reduce the space around rating bar in android
(3 answers)
Closed 4 years ago.
i have rating bar on my application and it appears like this:
I've tried to set padding to 0dp, but nothing changed to the view.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:layout_marginStart="16dp"
android:isIndicator="false"
style="#style/Widget.AppCompat.RatingBar"/>
Expected result: I am expecting the rating bar with no right padding and bottom padding.
You can simply adjust this
style="#android:style/Widget.DeviceDefault.RatingBar.Small"
instead of
style="#style/Widget.AppCompat.RatingBar
I add this to my xml code and it solved my problems:
android:scaleX="1"
android:scaleY="1"
The built-in Android rating bar is very visually problematic, in which case I would rather use the improvements made by Android users to size each star depending on the size of the object.
Enclosing you a convenient library to work with, I believe this library will solve you a lot of problems with the development.
https://github.com/xckevin/AndroidSmartRatingBar?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=7405
Credit to the developer kevin.liu (xckevin).
use scale x and y to reduce the start size and step size to set star size
Try this xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false"
style="#style/Widget.AppCompat.RatingBar" />
</LinearLayout>
I'm making an app which allows a user to search the various countries. The user can make filtered searches (such as, for example, search only countries of a specific continent, etc...). All this info (Countries and their continents are stored in my Firebase Realtime Database).
In my FilteredResults.java fragment I want to have a variable number of ImageViews (the number of the size of a List<String>).
This sketch I drew might help you understand it better:
Each one of this rectangles are ImageViews.
This is my XML (I just have a scrollview because I don't know how to create an "array" of ImageViews...)
<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">
<TextView
android:id="#+id/txtResultadosFiltrados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="0dp"
android:text="Resultados Filtrados"
android:textColor="#323B45"
android:textSize="24sp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="28dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</RelativeLayout>
In order to solve your problem in an optimal way, you should use one of:
a plain old ListView,
a newer, more powerful RecyclerView.
These views support so called adapters which map between a model (in your case: List<String>) and a list of items (in your case a list of ImageView's).
Here you have a complete example of a simple app which loads lists of posts from the Firebase Database:
https://github.com/firebase/quickstart-android/tree/master/database
This example is based on RecyclerViews, you can find an a direct usage here:
https://github.com/firebase/quickstart-android/blob/master/database/app/src/main/java/com/google/firebase/quickstart/database/fragment/PostListFragment.java
Use RecyclerView instead of ScrollView. As keeping array of ImageView and adding them in a ScrollView will create a explosive headache.
I'm trying to create a messaging app in android and have created a custom list adapter that displays the message text and then the date as well. I have got them both displaying, now want them to be able to be displayed in a certain way. I want to be able to have sent messages on one side of the screen but received messages on the other, with the date appearing below/to the side of the message text
This is the custom xml I have used for each item on the list, any ideas on how to make them wrap appropriately and move to sides of the screen would be appreciated!
(Note: I've tried using LinearLayouts with weights and RelativeLayouts with android:layout_alignParentRight and things, but couldn't get it exactly how I wanted it so thought I'd ask here!)
(Don't think other code snippets are needed but can provide if wanted!)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:paddingRight="2dp"
android:textStyle="bold"
android:textSize="15sp" />
<TextView android:id="#+id/msgDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Cheers
I've tried using LinearLayouts with weights
Whats wrong with that? android:layout_weight="0.4"
try to use not 'fill_parent', use 'match_parent';
also, replace this
android:textSize="15sp"
to this:
android:textAppearance="?android:attr/textAppearanceMedium"
New to coding to Android, but I couldn't find an answer to this particular problem. Say I have the following XML layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view class="com.example.testapp.customView"
android:id="#+id/drawView"
android:layout_height="100dp"
android:layout_width="fill_parent" />
<Button android:id="#+id/bNormal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Update" />
</LinearLayout>
I want to use this layout to create several activities, but in each activity I want to use a different customView. Eg. for one activity I would like the custom view to be customView1, the second activity I would like it to be customView2, etc.
How would I go about doing that? Right now I just have a seperate XML layout for each of my activities, and this seems awfully redundant.
You either create several layouts, or you have to add the new view programatically (by creating an object of the proper type and adding it to the parent view) rather than doing it via xml. The idea behind xml layouts is that your layout is more or less static, so the xml specifies exactly what you want. It isn't meant to be a programming language itself where things can change.
I want to have a "comments" button on my Android e-reader app that displays the number of comments currently posted inside the icon... so basically I want a comment bubble with a number inside it.
I could have multiple drawables, each being the bubble with a different number inside of it, and use a switch statement to choose which one to load each time based on the int number_of_comments field of the element being displayed. This approach seems a little wasteful though, and in any case I have a feeling there's a more elegant way to do it.
Any ideas?
You can do better. You can have a textview on top of the image view and keep updating its value everytime a new comment is added. You can define the overlap in xml like below and adjust your code logic accordingly to increase the comment count. For now I have just set up a dummy text Hello to show on top of the ImageView. You can add your comment count using the TextView's setText method.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Hope this helps...
check out this 3rd party created widget
Android ViewBadger
You can use it to create the little number bubbles that you are looking for I think. This gives you the benefit of not having modify your layouts so much to achieve what you are trying to get.
Here is the sample code to apply a "badge"
View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();