addView does not working (Linear Layout) - java

This is my first question. I want to add a textview to an existing layout every second using the addView method. And I want to print the text down while they remain. But nothing appear in the layout. Hope somebody can help me.
This is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = this.findViewById(R.id.btn);
textView = this.findViewById(R.id.tv1);
linearLayout = this.findViewById(R.id.line1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
runThread();
}
});
}
private void runThread(){
new Thread() {
#Override
public void run() {
while(i++ < 10) {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView createdTextView = new TextView(MainActivity.this);
createdTextView.setText(new Date(System.currentTimeMillis()).toString() + i);
linearLayout.addView(createdTextView);
}
});
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
This is avtivity_main.xml
<LinearLayout 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:orientation="vertical"
android:id="#+id/line1"
tools:context="com.example.runonuithread.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Try this:
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView createdTextView = new TextView(MainActivity.this);
createdTextView.setText(new Date(System.currentTimeMillis()).toString() + i);
createdTextView.setLayoutParams(lparams);
linearLayout.addView(createdTextView);

Related

Onclick listener not responding to clicks on imageview

I have a problem on getting my imageview button to work onclick. what im trying to do is to make the first image invisible so that the second one behind it can appear and at the same time define int special based on what image that has been clicked.
public class TrainingActivity extends AppCompatActivity implements View.OnClickListener {
public static int special;
public static ImageView a12,b13,a12a,b13a;
#Override
protected void onCreate(Bundle savedInstanceState) {
////////////////////////////////////////////////////////////////////////
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
a12 = (ImageView) findViewById(R.id.young);
b13 = (ImageView) findViewById(R.id.old);
a12a = (ImageView) findViewById(R.id.young2);
b13a = (ImageView) findViewById(R.id.old2);
a12.setOnClickListener(this);
b13.setOnClickListener(this);
a12a.setOnClickListener(this);
b13a.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.young) {
a12.setVisibility(View.GONE);
a12a.setVisibility(View.VISIBLE);
special =0;
} else {
a12.setVisibility(View.VISIBLE);
a12a.setVisibility(View.GONE);
}
if (v.getId() == R.id.old) {
b13.setVisibility(View.GONE);
b13a.setVisibility(View.VISIBLE);
special =1;
} else {
b13.setVisibility(View.VISIBLE);
b13a.setVisibility(View.GONE);
}
}
this is my main class file. I've had already tried this codes in a new project and it works there but somehow doesn't here.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/gray_color"
tools:context="com.codestudioapps.cardioexcercise.WalkandStep.activities.TrainingActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/young2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_gravity="center_horizontal|left|center_vertical"
android:src="#drawable/img_body_surface_area1"
/>
<ImageButton
android:id="#+id/young"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_gravity="center_horizontal|left|center_vertical"
android:src="#drawable/img_body_surface_area" />
<ImageButton
android:id="#+id/old2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal|right|center_vertical"
android:src="#drawable/img_body_fat1"
/>
<ImageButton
android:id="#+id/old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal|right|center_vertical"
android:src="#drawable/img_body_fat" />
</FrameLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
And this is the layout. The problem might happen because of my manifest file but im unsure. Does anyone have any ideas?
Your code seems okay. Just, casting problem i.e. ImageButton to ImageView.
Your code should be as follows:
public class DemoActivity extends AppCompatActivity implements View.OnClickListener {
public static int special;
public static ImageButton a12, b13, a12a, b13a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
a12 = findViewById(R.id.young);
b13 = findViewById(R.id.old);
a12a = findViewById(R.id.young2);
b13a = findViewById(R.id.old2);
a12.setOnClickListener(this);
b13.setOnClickListener(this);
a12a.setOnClickListener(this);
b13a.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.young) {
a12.setVisibility(View.GONE);
a12a.setVisibility(View.VISIBLE);
special = 0;
} else {
a12.setVisibility(View.VISIBLE);
a12a.setVisibility(View.GONE);
}
if (v.getId() == R.id.old) {
b13.setVisibility(View.GONE);
b13a.setVisibility(View.VISIBLE);
special = 1;
} else {
b13.setVisibility(View.VISIBLE);
b13a.setVisibility(View.GONE);
}
}
}

How i can fix AsyncTask error with TextView [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 3 years ago.
I want to display a timer from 1 to 100 in my TextView, but this throws me an Exception. My application throws a RuntimeException like shown below:
Thanks in advance for your help.
java.lang.RuntimeException: An error occurred while executing
doInBackground()
My MainActivity class where the RuntimeException is thrown.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new QQ().execute();
}
});
}
public void onProgressUpdate(int i) {
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
txt.setText(i);
}
private class QQ extends AsyncTask<Integer, Integer, Integer> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Integer doInBackground(Integer... args) {
for (int i = 0; i < 1000000; i++) {
try {
Thread.sleep(1000); ///задержка
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
return null;
}
return null;
}
private void publishProgress(int i) {
publishProgressq(i);
}
protected void onPostExecute(int i) {
}
}
private void publishProgressq(int i) {
onProgressUpdate(i);
}
}
My Layout class in 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:background="#bbbbbb"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:gravity="top"
android:orientation="horizontal">
<TextView
android:id="#+id/txt"
android:layout_width="151dp"
android:layout_height="110dp"
android:textSize="45dp"
android:text="0">
</TextView>
<Button
android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="START" />
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="STOP" />
</LinearLayout>
</LinearLayout>
You can change your code as below:
public void onProgressUpdate(int i) {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
txt.setText(i);
}
});
}
It will run on main thread.
Inside your public void onProgressUpdate , set text like this :
txt.setText(String.valueOf(i));
If you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.

How to change images using next and previous buttons?

sorry for annoying you but i am in my first step in android programming and need your support
the question is,
i want to change between photos using next and previous Button
i have 3 photos in an array
and 2 button (next and previous )
put when i started my app. i should press next button twice to change the photo
also in previous i should press it twice to change back
hope someone help me to improve the code down
public class SabahList extends AppCompatActivity {
ImageView img;
int[] mario = new int[]{R.drawable.image_a,R.drawable.image_b,R.drawable.image_c};
int n =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
img= (ImageView)findViewById(R.id.imageView2);
}
public void btu_next(View view) {
img.setImageResource(mario[n]);
if(n<2)
n++;
}
public void btu_prev(View view) {
img.setImageResource(mario[n]);
if(n>0)
n--;
}
}
Here is your logic problem. Your button's function command them set resource first, then increase/decease value, therefore when you click the second time, it will be set as previous increasingly/decreasingly.
public void btu_next(View view) {
if(n < 2)
n++;
img.setImageResource(mario[n]);
}
This answer may helpful to you if you are using normal array added images from drawable
//Your xml like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.devobal.quitchet.SabahList"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="previous"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="next"/>
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//And your Activity like this
public class SabahList extends AppCompatActivity {
ImageView imageView2;
Button previous,next;
int i=0;
private int[] textureArrayWin = {R.drawable.star1,R.drawable.star2, R.drawable.star3,};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
imageView2 = (ImageView)findViewById(R.id.imageView2);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
if(i==0){
previous.setVisibility(View.GONE);
}
if (i==2){
next.setVisibility(View.GONE);
}
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView2.setImageResource(textureArrayWin[i]);
i--;
if(i==0){
previous.setVisibility(View.GONE);} else{ previous.setVisibility(View.VISIBLE);}
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//if you are loading from bitmap
imageView2.setImageResource(textureArrayWin[i]);
i++;
if(i==0){
previous.setVisibility(View.GONE); } else {previous.setVisibility(View.VISIBLE); }
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
}
}

How to implement the splash screen image blink, without clicking?

SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
blink(v);
//I am calling the animation only when the image is clicked.
//I want to perform animation as the page loads.
//Not on clicking the image.
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginOptionsActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
public void blink(View view){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
image.startAnimation(animation1);
}
}
blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.codeinks.luxehues.SplashActivity"
android:background="#drawable/splash_shirt">
<ImageView
android:layout_width="300dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#drawable/fullsize"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please suggest a way to perform the animation
as the page loads and not while clicking the image.
Try this in onCreate method and comment that onClick method and give a try to below code,
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation myAnim = AnimationUtils.loadAnimation(context,R.anim.blink);
imageView.startAnimation(myAnim);
Try below code:
Thread splashTread;
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
StartAnimations();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l = (LinearLayout) findViewById(R.id.layoutAnimation);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.blink);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageSplash);
iv.clearAnimation();
iv.startAnimation(anim);
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3000) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(SplashActivity.this, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
} catch (InterruptedException e) {
// do nothing
} finally {
SplashActivity.this.finish();
}
}
};
splashTread.start();
}
and corresponding xml layout is:
<!-- activity_splash.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutAnimation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.SplashActivity">
<ImageView
android:id="#+id/imageSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/splash"
android:scaleType="fitXY"
android:src="#drawable/splash" />
</LinearLayout>
If got any issue, plz bother me. Thinks will help You.

Can I open a seperate XML layout with a OnLongClick

I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.
This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?
XML main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.how_to_use.MainActivity"
android:id="#+id/main_view">
<TextView
android:id="#+id/long_press_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long Press To Open \n'Drag n Drop'"/>
</LinearLayout>
XML 2nd page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/swirl_img"
android:src="#drawable/Swirl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private LinearLayout mainView;
private TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.long_press_textView);
addListenerLongPressBtn();
addListenerOnClickBtn();
}
public void addListenerOnClickBtn() {
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(this, DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
}
I found a solution to my own problem.
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(view.getContext(), DragDropActivity.class);
startActivity(intent);
return true;
}
});
}

Categories