Unable to access TextView of the the Custom ActionBar - java

I am using different layout and showing into action bar. I want to change value of TextView programatically which I used in layout.
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splashscreen_background">
<TextView
android:id="#android:id/text1"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="M:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="#android:id/text2"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="N:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
And here is java file code:
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.test, null);
TextView tv = (TextView)customView.findViewById(R.id.text1);
ab.setCustomView(customView);
ab.setDisplayShowCustomEnabled(true);
Can any one suggest me how can I change the value of TextView programmatically.

In xml, replace android:id="#android:id/text1" with android:id="#+id/text1". Same goes for TextView with id text2 ..
and in java, you will be able to programmatically set it like this:
TextView tv = (TextView)customView.findViewById(R.id.text1);
tv.setText("Your Text");
To findout what's the difference between #android:id and #+id, Please see this:
https://stackoverflow.com/a/9530028/2534007

Related

Don's see an ImageView using Inflater

I have made XML markup for part of my program.
<?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"
android:padding="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/checked_border">
<ImageView
android:id="#+id/asset_icon"
android:layout_width="60dp"
android:layout_height="60dp"
tools:srcCompat="#drawable/ic_dollar" />
<LinearLayout
android:id="#+id/asset_name_and_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/asset_icon"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="count" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="sum" />
</RelativeLayout>
After that, I tried to add it using inflater
LayoutInflater ltInflater = getLayoutInflater();
RelativeLayout assetlayout = (RelativeLayout)
ltInflater.inflate(R.layout.asset_layout, null, false);
LinearLayout assets = findViewById(R.id.assetslayout);
assets.addView(assetlayout);
So that what I expected to see. Also, you can see a top margin.
But when I run my program I don't see an image view and there are no top margins at all.

How to update divider to the right place in a RecyclerView?

I wrote the following layout:
<?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"
android:orientation="vertical">
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
<TextView
android:id="#+id/full_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/avatar"
android:layout_toEndOf="#id/avatar"
android:textSize="20sp"
android:textColor="#color/blackColor"
android:layout_centerVertical="true" />
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/avatar"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>
It's the item layout of the RecyclerView. Basicly it creates the following layout:
[avatar] [name]
---------------------
[avatar] [name]
---------------------
[avatar] [name]
The Java code:
public void onBindViewHolder(ViewHolder holder, int position) {
// Code
// Set avatar
// Set name
holder.fullname.setText((CharSequence) name);
// Set visible/invisible divider
if (position < getItemCount() - 1) {
holder.viewDivider.setVisibility(View.VISIBLE);
} else {
holder.viewDivider.setVisibility(View.GONE);
}
}
The problem is that the divider is being set on top of the ImageView and the TextView and not on the bottom of each block. I think that I understand why it happens - the divider is already there so when I set ImageView and TextView dynamically, the divider is not being rendered to the right place. Or maybe it's related to android:layout_centerVertical? How should I fix it?
Also, In the activity which sets the adapter I call:
holder.users.setLayoutManager(new LinearLayoutManager(this.activity));
Is it ok that I call LinearLayoutManager when the layout (of the item) is RelativeLayout?
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/avatar"
android:layout_centerVertical="true" <-- REMOVE THIS LINE :)
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
Maybe you should enclose your image and textview in a layout preferably in a relative layout and then take a view and below that layout like this :
<?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"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rlLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
<TextView
android:id="#+id/full_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/avatar"
android:layout_toEndOf="#id/avatar"
android:textSize="20sp"
android:textColor="#000000"
android:text="Test String"
android:layout_centerVertical="true" />
</RelativeLayout>
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_below="#+id/rlLayout"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>

How to set widget toolbar with 4 buttons after every textview

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.

OnClick in a CardView <include> layout

I have a xml that contains some CardView <include>, I want to set onClick in the CardView includes to open an Activity in my project. I tried to set the onClick in the CardView layout as you will see in the code below, it opens normally but triggers an error sometimes and the application stops. How can I set the onClick in this include?
This is the <include> in my xml
<include layout="#layout/view_caderno"
android:id="#+id/view_caderno" />
and here is the CardView
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="15dp"
android:layout_margin="5dp"
android:onClick="Caderno"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pic1"
android:layout_width="100dp"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:padding="5dp"
android:src="#drawable/note"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pic1"
android:maxLines="3"
android:text="Diário"
android:padding="10dp"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:maxLines="3"
android:padding="5dp"
android:drawableRight="#drawable/baby2"
android:text="#string/diário"
android:textColor="#666"
android:textSize="14dp" />
</RelativeLayout>
You have to make OnClickListener programmatically :
CardView yourCardView = (CardView) findViewById(R.id.view_caderno);
yourCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do what you want on click event
}
});
Check this answer - stackoverflow.com/a/37790333/1333022
Dynamically you can add layout in your cardview.

Access the content form customview in android

I am using the customview to add a xml in my Actionbar in ICS..Here is the code...I want to access the xml contents
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.abc);
In that view i have 2 button and a textview...Now i want to access those button in onclick...
Here is the xml :
abc.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"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Delosis V2"
android:textSize="6.5pt"
android:paddingTop="15dp" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
Now any solution please....
You use the getCustomView() method.
To get the internal views
View myView = actionBar.getCustomView();
Button button1 = (Button)myView.findViewById(R.id.button1);
Button button2 = (Button)myView.findViewById(R.id.button2);
TextView textView = (TextView) myView.findViewById(R.id.textView1);
You can use the findViewById(id) method on any View to get one of its internal views.

Categories