How to set widget toolbar with 4 buttons after every textview - java

I'm designing a quotes app and i m having so many quotes in one page (activity) so i want a widget toolbar and 4 share buttons after every textview, i did this through xml, i want to do this by coding in java activity so the coding length will reduce, please give me the right solution for this and please look out these pics and my code
Image
<?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"
android:background="#000000"
tools:context="technoapps4.shayari_vayari.second">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<TextView
android:id="#+id/textView"
android:onClick="tv"
android:text="#string/Funny1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:textColor="#fff"
android:textIsSelectable="true"
android:textSize="20sp"/>
<android.support.v7.widget.Toolbar
android:id="#+id/tb"
android:layout_width="match_parent"
android:layout_marginTop="05dp"
android:layout_height="match_parent"
android:background="#fff">
</android.support.v7.widget.Toolbar>
<Button
android:id="#+id/copy1"
android:onClick="copy1"
android:layout_marginLeft="08dp"
android:layout_marginTop="-50dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/copy" />
<Button
android:id="#+id/whatsapp1"
android:onClick="whatsapp1"
android:layout_marginLeft="90dp"
android:layout_marginTop="-48dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/whatsapp" />
<Button
android:id="#+id/fb1"
android:onClick="fb1"
android:layout_marginLeft="180dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/facebook" />
<Button
android:id="#+id/share1"
android:onClick="share1"
android:layout_marginLeft="275dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/share" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/Funny2"
android:textColor="#fff"
android:textIsSelectable="true"
android:textSize="20sp" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_marginTop="05dp"
android:layout_height="match_parent"
android:background="#fff">
</android.support.v7.widget.Toolbar>
<Button
android:id="#+id/copy2"
android:onClick="copy2"
android:layout_marginLeft="08dp"
android:layout_marginTop="-50dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/copy" />
<Button
android:id="#+id/what2"
android:onClick="whatsapp2"
android:layout_marginLeft="90dp"
android:layout_marginTop="-48dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/whatsapp" />
<Button
android:id="#+id/fb2"
android:layout_marginLeft="180dp"
android:onClick="fb2"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/facebook" />
<Button
android:id="#+id/share2"
android:onClick="share2"
android:layout_marginLeft="275dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/share" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>

If you will have many Quotes to display, I suggest you use RecyclerView along with RecyclerView.Adapter. LinearLayout might slow your app dow.
But if your quotes are not so much, or you are really want to use LinearLayout. You can build a listitem_quote.xml contains your quote and 4 Buttons first.
And then use an empty LinearLayout in your activity.xml like this.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
In your activity, inflate() for every quotes. And add them to your layout.
onCreate(...){
setContentView(R.layout.activity_main);
myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
for(...){ // for every quote
// create a view for quote contains buttons
View quoteView = layoutInflater.inflate(R.layout.listitem_quote, null);
TextView tvQuote = quoteView.findViewById(...);
tvQuote.setText(...);
Button btn1 = quoteView.findViewById(...);
btn1.setOnclickListener(...);
myLinearLayout.addView(quote);// add to Linearlayout
}
}
This will do the trick.
However I still suggest you to use RecyclerView instead.
You can find many examples on the internet.
It looks more complex but it's useful.

Related

Dynamically set the widths (weights) of Views inside a LinearLayout

I have a LinearLayout that contains four differently-colored Views. Each colored view represents the proportion of a workout's cumulative time spent in various heart-rate zones (blue -> green -> orange -> red).
I can get the desired effect by changing the android:layout_weight=".35" attribute in the various views in the XML. Setting these values does exactly what I want (when I don't override them in code). I think I'm just not accessing these properties correctly in code.
I have tried hard-coding nice, round sets of numbers and they don't look correct. The first one in the list appears much larger than it ought to be.
My layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ex_history_item"
android:layout_height="160dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:theme="#style/AppTheme.CurrentStyle.HabitCard"
xmlns:android="http://schemas.android.com/apk/res/android" >
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="4dp"
android:id="#+id/cardio_bar"
android:visibility="visible"
android:weightSum="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<View
android:id="#+id/cardio_none"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="#color/cardio_zone_none"
/>
<View
android:id="#+id/cardio_fatburn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="#color/cardio_zone_fatburn"
/>
<View
android:id="#+id/cardio_cardio"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:backgroundTint="#color/cardio_zone_cardio"
/>
<View
android:id="#+id/cardio_peak"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".15"
android:backgroundTint="#color/cardio_zone_peak"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/accent"
android:layout_width="35dp"
android:layout_height="match_parent"
android:background="#color/cardio"
android:backgroundTint="#00000000"
android:backgroundTintMode="add"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/ex_image"
app:layout_constraintTop_toBottomOf="#id/cardio_bar">
<TextView
android:id="#+id/bodypartLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:foregroundGravity="fill"
android:rotation="90"
android:text="Bodypart"
tools:text="Bodypart" />
</LinearLayout>
<ImageView
android:id="#+id/ex_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/accent"
app:layout_constraintTop_toBottomOf="#+id/exercise_name"
>
</ImageView>
<TextView
android:id="#+id/exercise_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise Name"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:textSize="18sp"
app:layout_constraintLeft_toRightOf="#+id/accent"
app:layout_constraintTop_toBottomOf="#+id/cardio_bar" />
<TableLayout
android:id="#+id/cardio_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="#+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
<TableRow>
<TextView android:text="Duration"/>
<TextView android:id="#+id/duration" android:text="0"/>
</TableRow>
<TableRow>
<TextView android:text="Distance"/>
<TextView android:id="#+id/distance" android:text="0"/>
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/weights_table"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
app:layout_constraintBottom_toBottomOf="#+id/ex_image"
app:layout_constraintRight_toRightOf="parent"
>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
My code for changing them is the following
private final LinearLayout cardioBar;
private final View cardioZoneNone;
private final View cardioZoneFatburn;
private final View cardioZoneCardio;
private final View cardioZonePeak;
Float total = (float)exercise.exercise.getHrTotalDuration();
holder.cardioBar.setWeightSum(total);
holder.cardioZoneNone.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrNone()));
holder.cardioZoneFatburn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrFatburn()));
holder.cardioZoneCardio.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrCardio()));
holder.cardioZonePeak.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,(float)exercise.exercise.getHrPeak()));
I have even tried hard-coding some values in place of the ones in my objects. Strangely, it seems that setting them to 0.25 (with a weightsum of 1.0) looks correct, but most others set the cardioZoneNone much, much larger than it ought to be.
When I log the values, the numbers look appropriate.
I had read that the width should only be set to zero when using weight attribute in XML, but not when setting weight from code. Perhaps this was true when that was written, but setting width to zero does seem to fix my issue

ImageView covering other contents in android

The image is covering the textView and buttons(I cant even see them at all so I'm guessing they are behind the image). Image is attached. I want the textview and buttons to appear below the imageview. Below is my xml file.
<?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"
>
<ImageView
android:id="#+id/imageView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
You can change the main layout to RelativeLayout, set the ImageView at the top and wrap the views below the ImageView, inside a separate layout.
By setting android:layout_above="#+id/linear" for the ImageView, you will be sure that the ImageView will never cover the other views.
<?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"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</RelativeLayout>
Try changing the parent layout to a RelativeView, or a ConstraintView. If you don't want to redo your positioning of your layout, you can wrap everything except the Image in a LinearLayout.
<RelativeView>
<ImageView>
<LinearLayout>
//...All your stuff
</LinearLayout>
<RelativeView>
This isn't one way of course, try to experiment. Feel free to ask if you still encounter problems.

how to make a fixed portion of scroll activity to display on every screen sizes

How can I make a fixed portion of a scroll activity display on every screen size?
I have a scrollable activity which contains a pageviewer, and below that, 4 buttons. On an average screen it just displays only these things, the rest comes below the screen, which means there are few more views below the button. If we use a wider screen, then these views also appear in main view before scrolling. I need to make the view range the same for all screen sizes, meaning for wider screen also, I need to just view the pageviewer and buttons only, the rest should be seen only when we scroll.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/idd_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="500dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
</LinearLayout>
</ScrollView>
</LinearLayout>
set ViewPager height programatically so it can match mobile height

Android app relative layout with 2-3 elements and a bottom button

I want to design an android activity with a relative layout. There should be 3 (or later 4) elements in a vertical line among each other. I've tried it, but the textview is hidden behind the listview.
What's wrong with the code, how should I change it and how should it look, if I want to insert a fourth element between the text and listview or the listview and button ... like a image.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:paddingBottom="10dp"
android:layout_above="#+id/listview"
android:text="#string/info" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button"
android:layout_marginBottom="10dp" />
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="start"
android:src="#drawable/button1" />
</RelativeLayout>
Try changing ReltiveLayout to LinearLayoutor put LinearLayout outside your RelativeLayout, whith orientation="vertical" an put the TextView outside RelativeLayout. Something like this:
<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/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/info" />
<RelativeLayout
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button"
android:layout_marginBottom="10dp" />
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="start"
android:src="#drawable/button1" />
</RelativeLayout>
</LinearLayout>

Google i/o app: where it sets toolbar_actionbar

I'm trying to study the Google i/o 2014's app.
Inside the BaseActivity it has this method:
protected Toolbar getActionBarToolbar() {
if (mActionBarToolbar == null) {
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
if (mActionBarToolbar != null) {
setSupportActionBar(mActionBarToolbar);
}
}
return mActionBarToolbar;
}
Why findViewById dosen't return null? :/
The content view of the WelcomeActivity does this: setContentView(R.layout.activity_welcome); and this is the activity_welcome.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.WelcomeActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e5e5e5">
<ImageView
android:src="#drawable/io2014_logo"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:tint="#7000"
android:layout_gravity="center|top" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="100dp"
android:clipToPadding="false">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
iosched:cardBackgroundColor="#fff"
iosched:cardCornerRadius="#dimen/card_corner_radius"
iosched:cardElevation="#dimen/card_elevation"
iosched:cardPreventCornerOverlap="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/font_fontFamily_medium"
android:text="#string/welcome_to_google_i_o_app"
android:textColor="#color/theme_primary"
android:textSize="#dimen/text_size_xlarge"
android:textStyle="#integer/font_textStyle_medium" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:autoLink="web"
android:text="#string/welcome_text"
android:textColor="#color/body_text_2"
android:textSize="#dimen/text_size_medium" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:autoLink="web"
android:text="#string/eula_legal_text"
android:textColor="#color/body_text_2"
android:textSize="#dimen/text_size_medium" />
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
</ScrollView>
</FrameLayout>
<!-- Button bar -->
<LinearLayout
android:layout_height="48dp"
android:background="#color/theme_primary"
android:layout_gravity="bottom"
android:layout_width="match_parent">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="?photoItemForeground"
android:text="#string/decline"
android:textColor="#8fff"
android:textSize="#dimen/text_size_medium"
android:textStyle="#integer/font_textStyle_medium"
android:fontFamily="#string/font_fontFamily_medium"
android:textAllCaps="true"
android:id="#+id/button_decline" />
<Button
android:id="#+id/button_accept"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="?photoItemForeground"
android:text="#string/accept"
android:textSize="#dimen/text_size_medium"
android:textStyle="#integer/font_textStyle_medium"
android:fontFamily="#string/font_fontFamily_medium"
android:textAllCaps="true"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
So it dosen't include in any way the toolbar_actionbar.xml. Also i looked inside the style.xml for some hint (maybe it was included in all files in some way) but i really can't find anything.
Thanks for help
Here is what is going on:
1) WelcomeActivity does not extends from BaseActivity, so getActionBarToolbar() will not be called on this case.
2) Activities that are subclasses of BaseActivity have <include layout="#layout/toolbar_actionbar" /> somewhere on their view hierarchy.
They have a layout named https://github.com/google/iosched/blob/master/android/src/main/res/layout/toolbar_actionbar.xml
which they < include > in their Activities layouts
Take a look at this guy over here. He explains it perfectly.
Google Documentation:
http://android-developers.blogspot.cz/2014/10/appcompat-v21-material-design-for-pre.html
http://developer.android.com/reference/android/widget/Toolbar.html
Here is Google Design Spec you can take a look:
https://www.google.com/design/spec/material-design/introduction.html
Hope it helps!!!

Categories