How can we set text to include button in layout - android

I create layout button and i want to use it to other place by include it and i want to change it text in per layout , is there a way??
my button(row_addbutton) :
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_banklist_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:background="#drawable/backgroundbutton_withradiusandcolorprimarylight"
android:text="#string/banklist_addtoken"
style="#style/TextAppearanceButton"
android:drawableEnd="#drawable/addtokenicon"
android:layoutDirection="ltr"
android:drawableRight="#drawable/addtokenicon"
android:paddingStart="60dp"
android:paddingEnd="60dp"
android:layout_marginTop="25dp">
</Button>
include it here :
<include
layout="#layout/row_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_below="#+id/linearlayout_banklist_basecontent"
android:layout_centerHorizontal="true"
android:layout_marginStart="70dp"
android:layout_marginTop="-25dp"
android:layout_marginEnd="70dp"
//is there any way to change text button here??
/>

A more sensible way to achieve what you want is to declare a style for such buttons in /res/values/styles.xml. So then you just have to choose your style from the drop-down list and change the button text as you usually do.

You can access all the view of included layout.
You can do it in .java class like this.
Let's assume you have included your layout in activity_main.xml then go to respective java file and in that you can do code like below :
Button button_banklist_addtoken = findViewById(R.id. button_banklist_addtoken);
button_banklist_addtoken.setText("Here is your text")
I hope it will help you

Related

Android Google Login Button and Facebook sdk 4+ Button Layout

I Am developing an application which use google and facebook integration ...I want to fix height and width of those button...and i want to set Button Text Manualy...
i had tried so far
<com.facebook.login.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:id="#+id/connectWithFbButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/fbloginText"
android:layout_marginTop="30dp"
/>
<com.google.android.gms.common.SignInButton
android:id="#+id/signin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/googleloginText"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"/>
although i had attemted something like this
fb:login_text="Login"
fb:logout_text="Logout"
or
facebook:login_text="Login"
But i Got Error::=>No resource identifier found for attribute 'login_text' in package 'com.test.b2c'
or in by coding
loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setText("Login");
Now My layout Look Something Like This
I also want to set Height And width of this buttons .. ...
help me friends .....thx in advance
UPDATE
I have no direct problem in setting width what am I really concern is the inconsistent HEIGHT shown in the picture. I need to find better way(s) to make those buttons height equally nd set Text to those Button.
If we wanna change custom text of Facebook Login ..
Go to that Facebook library project And go to string.xml
you will find something like this
<string name="com_facebook_loginview_log_in_button_long">Log in with facebook </string>
Replace this with your custom text
Although if you want to change custom text of Google Plus you can
From OnCreate call this
setGooglePlusButtonText(signinButton,"your custom text");
//Text Change Method Of Google Plus
protected void setGooglePlusButtonText(SignInButton signInButton,
String buttonText) {
for (int i = 0; i < signInButton.getChildCount(); i++) {
View v = signInButton.getChildAt(i);
if (v instanceof TextView) {
TextView tv = (TextView) v;
tv.setTextSize(15);
tv.setTypeface(null, Typeface.NORMAL);
tv.setText(buttonText);
return;
}
}
}
For Height inconsistancy of facebook i had add padding to Facebook Button XML
android:paddingTop="20dp"
android:paddingBottom="20dp"
This xml code for style customizes my app's facebook login button like in the picture (in text and button size).
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
facebook:com_facebook_login_text="Facebook"
facebook:com_facebook_logout_text="Facebook" />
Result:
Add linear layout and add these 2 button in it :
<LinearLayout
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.facebook.login.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:id="#+id/connectWithFbButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/fbloginText"
android:layout_marginTop="30dp" />
<com.google.android.gms.common.SignInButton
android:id="#+id/signin"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="#string/googleloginText"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp" />
</LinearLayout>
This will give Correct Height, Width and Margin for both button
You have to change android:layout_width to either wrap_content, or a specific size, preferably in dp's (e.g. 20dp, 30dp, 40dp).
As for the error, it is probably shown because you didn't edit your strings.xml, try using Android Studio, it will automatically create these values for you, and will also show you a preview of your app.
You just need to change the
android:layout_width="any size in dps"
android:layout_height="any size in dps"
and for setting the buttons manually you can concern with the specific docs. i,e check it or
this is a good example.
In your Facebook Widget add the following:
android:paddingTop="20dp"
android:paddingBottom="20dp"
loginView:login_text="your custom text here"
UPDATE
You also need to include this:
xmlns:loginView="http://schemas.android.com/apk/res-auto"
<com.facebook.login.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
fb:com_facebook_login_text="Your Text"

how to copy a button in layout?

i want to make 16 buttons in one XML file but i don't want to make all of them individually ...how can i make it happen , like making one button and then copy it for many times??
like this picture :
http://i62.tinypic.com/t7cvie.png
i tried making a button
Button button = (Button) findViewById(R.id.Button1);
what shoud i do next?
and then i want to swipe the buttons so i would have other 16 buttons and go on ....
what should i do? i got confused.
help me?
Not quite sure if you were looking for this .
But there are multiple ways to reuse your code in android. One of the way to re-use your button definition that I prefer to use is to define the layout of the button first and include it where ever needed. Each include can be given unique id. Below are the steps to follow:
Define your android button layout in **layout folde**r first. This would be your only definition.
example: my_layout.xml
android:gravity="center|bottom" >
<Button
android:id="#+id/button_register"
android:background="#drawable/bordered_rounded_button"
android:layout_width="fill_parent"
style="#android:attr/buttonBarButtonStyle"
android:layout_height="30dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:shadowColor="#4D56A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_weight="0.5"
android:text="#string/button_register"
android:textColor="#color/WhiteSmoke"
android:textSize="14sp"
/>
then you can include this in your view/xml 15 times ( or any number of times) with unique id of each include .
like this :
<include android:id="#+id/include_layout_id"
layout="#layout/my_layout" <!-- make sure this matches your layout file name-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
Good Luck!
The first you must get layout where you want to add buttons, after initialize button and when add it to layout
LinearLayout layout = (LinearLayout) findViewById(R.id."layout id");
setContentView(new GraphTemperature(getApplicationContext()));
Button newButton = new Button(this);
newButton.setText("New Button");
newButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(myButton);
put this code in for loop i<=15;
You can't use the same ids in same layout if there behavior is different. So you have to create 16 Buttons in your layout. But put your button definition to your style.xml

Add multiples images to Edit Text View Android

I have 2 images one is "(" and another ")" how I can add these to EditTextView backgroud and stretch them the size of the EditTextView changes .
I tried to use layer_list without any luck .
Why don't you put those into <ImageView>s and put one to the left and one to the right of the EditText. Something like this:
<RelativeLayout>
<ImageView
android:layout_toLeftOf="#+id/edit_text"/>
<EditText android:id="#id/edit_text"/>
<ImageView
android:layout_toRightOf="#id/edit_text"/>
</RelativeLayout>
Of course you will need to add more attributes to the above, but it is a template for you to get started.
you can use
android:drawableLeft="#android:drawable/left_bracket"
android:drawableRight="#android:drawable/right bracket".
N.B.
by using android:drawableLeft the drawtable to be drawn to the left of the text.
by using android:drawableRigh the drawable to be drawn to the right of the text.
<EditText
android:id="#+id/txtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="120dip"
android:layout_marginTop="10dip"
android:hint="#string/search_titles"
android:background="#android:color/transparent"
android:drawableRight="#drawable/search_bar_end_right_inactive"
android:drawableLeft="#drawable/search_bar_end_left_inactive"
</EditText>
How I can stretch the images when the textfield is re sizing .?

onclicklistener textView in android

I have one TextView with two different colors. Here if I click mini it should redirect to one activity, if I click metro redirect to another activity. How can I achieve this?
TextView t = (TextView)v.findViewById(R.id.text);
String text = "<font color=#000000><b>"+"mini"+"</b></font><font color=#000000> added </font><font color=#1569C7>"+"Metro"+"</font><font color=#000000> as a favourite.</font>";
t.setText(Html.fromHtml(text));
What you can do is create an custom class that extends ClickableSpan and manage your clickable text. I had answered the same here.
On your click listener of textview you have to find its text and you can use getText property just compare the text and opens activity as per the text.
I think you can use two textview in linear layout with two different colours and text. Then you can implement onClicklistener() for two text view. You will achieve what you want and it looks same like what you expecting.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt1"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt2"
android:textColor="#1569C7"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
Hope it helps
In an onclick listener I'd try this:
if (text.equalsIgnoreCase("mini")) {
// call your next activity here
} else if(text.equalsIgnoreCase("metro")) {
// call your next activity here
}
Don't forget to set android:clickable="true" in your XML file of the TextView.
This answers what you want, though keep in mind that it's very hard to get the usability of such a component right. It's very likely that someone somewhere will have trouble clicking the links - especially if the text is on multiple lines and the linked words are close to each other vertically.
I would advice against such a solution.

setText in custom view

I have created a custom class from a tutorial online. It works fine, but what I want to do is make it so that I can set the text of the view, just like I would any other view. How is that accomplished. Here is the xml for the custom view
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/file_image_holder"
android:layout_width="110dip"
android:layout_height="110dip"
android:src="#drawable/pdf_icon" />
<TextView
android:id="#+id/file_name_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name Here" />
</LinearLayout>
What I am trying to achieve is this
FileView fileView = new FileView(this)
fileView.setText("text")
TextView filename = (TextView)findViewById(R.id.file_name_holder);
filename.setText("someFile.exe");
On another note ... you can use hint to give your users a greyed out text prompt that they won't have to erase when entering a filename. In your XML replace android:text with android:hint for your TextView.
replace "File Name Here" with "TEXT YOU WANT" or #string/name_of_string_in_string.xml
or the java way, which someone else answered
here no_internet is include xml view . in the include view contain text view. you can set text in include custom text view like ....
no_internet = view.findViewById(R.id.no_internet);
((TextView) no_internet).setText(R.string.no_internet_connection);

Categories

Resources