Does append create new textView? - android

MainActivity.java file
package com.example.sunshineexample_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
TextView toyListText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toyListText = findViewById(R.id.toys_list);
String[] toyNames = ToyBox.getToyNames();
for(String Toyname : toyNames){
toyListText.append(Toyname + "\n");
}
}
}
In for each loop, the line toyListText.append(Toyname + "\n"); , does it create a new textview for every ToyName(There are about 30 toys in String[] toysName) or just extends the size of existing textview. In xml file I have created a framelayout container view and a single textView.
Xml file
<TextView
android:id="#+id/toys_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:textSize="25sp" />

No. It creates a new String, but not a new TextView. Nothing but the call to inflate creates a TextView. However, nothing in your code is updating the textview so you'll never see the changes- to see the text you'd need to call textView.setText(toyListText) after you're done generating it.

Related

Programmatically setTextColor of widget on based on user input

I want to make a clock widget where in from its app user can set the textColor according to their wish.
So I want to change the TextClock's text color dynamically and programmatically on users click.
I tried using the simple setTextColor() method but there is no such method for setting text color of widget.
Below is the code I tried but it crashed the app on user click(touch).
I expect that the color of text changes on user's touch(that can be on any Button or ImageView).
package com.example.demowidget;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
public class MainActivity extends AppCompatActivity {
TextClock tctClock;
ImageView imgIcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgIcon = findViewById(R.id.imgIcon);
tctClock = findViewById(R.id.tctClock);
imgIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tctClock.getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/widget_margin">
<TextClock
android:id="#+id/tctClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/widget_back"
android:fontFamily="#font/lato"
android:padding="10dp"
android:textColor="#11AB6B"
android:textSize="36sp" />
</RelativeLayout>
you can try for this..
first add you color in color.xml file
<color name="your_color">#094D92</color>
and replace this line--
tctClock.setTextColor(Color.parseColor("#094D92"));
to
tctClock.setTextColor(getResources().getColor(R.color.your_color))

Cannot implement linerGradient() in TextView inside onCreate() method

I want to implement linearGradient() in my textView. I want it in a way so that when the activity loads up, the linearGradient() gets applied on my textview. I was able to do it in a button click listener, but the linear gradient does not work whenever I implement it in the onCreate() method. Below is my xml layout:
<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"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="Apply Gradient Text" />
</RelativeLayout>
MainActivity.java file:
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView tv;
private Button btn;
private int mWidth;
private int mHeight;
private Shader shader;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
tv.setText(" Instagram ");
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
tv.setTypeface(face);
}
public void onClick(View view) {
mWidth=tv.getWidth();
mHeight=tv.getHeight();
Point size = new Point(mWidth,mHeight);
GradientManager gm= new GradientManager(getApplicationContext(),size);
shader=gm.getRandomLinearGradient();
tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
tv.getPaint().setShader(shader);
}
}
GradientManager.java file:
import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Point;
import android.graphics.Shader;
public class GradientManager {
private Point mSize;
public GradientManager(Context context, Point size){
this.mSize = size;
}
protected LinearGradient getRandomLinearGradient(){
LinearGradient gradient = new LinearGradient(0, 0, mSize.x, mSize.y,
new int[] {Color.parseColor("#6656C8"), Color.parseColor("#8E33A9"),Color.parseColor("#BB328C"), Color.parseColor("#ED4B3E"),
Color.parseColor("#FA8031"), Color.parseColor("#FEC65C"), Color.parseColor("#FFD374") },null,
Shader.TileMode.MIRROR
);
return gradient;
}
}
Screenshot when activity starts:
Screenshot when I press the button:
What I want to implement is given below (I removed the button):
MainActivity.java file:
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private int mWidth;
private int mHeight;
private Shader shader;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
tv.setText(" Instagram ");
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/billabong.ttf");
tv.setTypeface(face);
mWidth=tv.getWidth();
mHeight=tv.getHeight();
Point size = new Point(mWidth,mHeight);
GradientManager gm= new GradientManager(getApplicationContext(),size);
shader=gm.getRandomLinearGradient();
tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
tv.getPaint().setShader(shader);
}
}
The contents of GradientManager.java is unchanged, Only the button is removed from my layout. The above code does not show any gradient color, instead It displays the whole textView with the first color of the linear gradient color parameter [Color.parseColor("#6656C8")]. This is my screenshot:
Can someone please help me with the code? What am I missing? Any help is appreciated.
You are using tv.getWidth and getHeight during onCreate. At that time TextView is not measured yet therefore those values are not valid. İf you have to get width and height for gradient you should apply your gradient after measure step is done(i.e. using ongloballayoutlistener).

button onclick from a layout file

so I was working on some basics of android when I stumbled upon this. I am trying to implement a listview(having its layout stored in file eachrow.XML)and a button on my app such that when the user clicks one button "schoollife", the listview automatically imports layout stored from other file "eachrow2.XML".
the layout of eachrow.xml consists of buttons only, while the layout of eachrow2.xml consists of buttons.
Everything seems to work fine, the user clicking the button imports the other layout , but the problem starts when user tries to click buttons from the second layout. in the error logs, it is stating that the button 'subject' causing a null pointer exception
eachrow.xml:
eachrow2.xml:
===========codes========
eachrow2.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/subjectbutton"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="1dp"
android:layout_margin="1dp"
android:textAllCaps="false"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/marksbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="click the button to show marks of each subject"
android:textAlignment="center"
/>
</LinearLayout>
mainactivity.java:
package com.example.ansh.reportcard;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView l;
private String[] d_myself;
private ArrayAdapter adp;
private HashMap<String,String> d_school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.a_list);
d_myself= new String[]{"something"
};
d_school=new HashMap<>();
d_school.put("A","B");
Button B_myself= (Button)findViewById(R.id.myself);
B_myself.setOnClickListener(this);
Button B_school=(Button)findViewById(R.id.school);
B_school.setOnClickListener(this);
Button subject=(Button)findViewById(R.id.subjectbutton);
subject.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.myself){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
l.setAdapter(adp);
}
if (view.getId()==R.id.school){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
l.setAdapter(adp);
}
if (view.getId()==R.id.subjectbutton){
Button tmp=(Button)view;
CharSequence x=tmp.getText();
TextView t= (TextView) findViewById(R.id.marksbox);
t.setText(d_school.get(x.toString()));
}
}
}
According to my understanding to your question, You declaring the Button from the xml file activity_main.xml, Instead you should find it from the eachrow2.xml file.
The subjectbutton is in the eachrow2.xml so you need to find the view from this file only instead activity_main.xml.

getting error while using multispinner

I want to select multiple items in spinner but getting runtime exception due to java.lang.CastException in XML file
XML:
<com.example.lenovo.abc.MultiSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hobby"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_weight="0.91"
/>
MultiSpinner class:
Android Spinner with multiple choice
About class
package com.example.lenovo.abc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TextView intro= (TextView) findViewById(R.id.introducing);
MultiSpinner hobby= (MultiSpinner) findViewById(R.id.hobby);
List<String> h = Arrays.asList(getResources().getStringArray(R.array.hobby));
hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
}
}
hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
You are not implementing MultiSpinner.MultiSpinnerListener in this activity and passing context casting to MultiSpinner.MultiSpinnerListener. Try implementing the listener in this activity.

findViewById is not working

I have a TextView in my app and I want to change its textSize programmatically using the setTextSize method, but everytime I ran my code, I got an error message : "java.lang.NullPointerException". I tried to print the value of the TextView and it got NULL value.
Here's my code:
LoadActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text);
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
setContentView(R.layout.activity_load);
}
}
activity_load.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:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Wait"
android:id="#+id/loadTextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:typeface="monospace"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
What do you think is the problem?
setContentView(R.layout.activity_load); should be declared before initializing your views(TextView) and System.out.println("TextView: "+text); is not correct form to print value. The correct form is System.out.println("TextView: "+text.getText().toString());
So your corrected code will be like this
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Use this in your onCreate Method
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
text.setTextSize(30);
In order for you to find views by their id, you need to write
setContentView(R.layout.activity_load);
This is because it essentially gets the layout resource file that defines your UI, and then allows you to retrieve other views within that layout (with findViewById();)
Look here for more information on how to properly set up Activites
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load); // Retrieves layout that defines your UI
// After setContentView is called, you can find views within the layout
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}

Categories

Resources