Removing duplication in Android Layout and Activity codes - java

I have a simple Activity/ LinearLayout combination that has two buttons and changes the background colour when each button is clicked. The code is as below
It appears to me that there is a lot of high-level duplication of code. For example, I should not need to define and make variables out of the buttons and the LinearLayout background in my Java file. When I express my intention to couple them, I should be getting them automatically
So, lines like btnBlue = (Button) findViewById(R.id.btnBlue); should not be necessary. A good framework should allow me to use some conventions (like give me a Java variable btnBlue if my resource file #+id/ resource is named as btnBlue, etc
Where can I find a framework that achieves these for me in Android programming?
Thanks
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:weightSum="1"
android:id="#+id/background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Linear Layout Tutorial"
android:textColor="#ff33ff"
android:textSize="32sp"
/>
<Button
android:id="#+id/btnGreen"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:text="Change to Green"
android:layout_weight="0.07" />
<Button
android:id="#+id/btnBlue"
android:layout_width="400sp"
android:layout_height="wrap_content"
android:text="Change12 to Blue"
android:layout_weight="0.19" />
</LinearLayout>
Activity Java code
package com.example.xxx.testandroid;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final String TAG = "TKT";
LinearLayout background;
Button btnGreen, btnBlue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.som_linear);
Log.d(TAG, "onCreate");
background = (LinearLayout) findViewById(R.id.background);
btnBlue = (Button) findViewById(R.id.btnBlue);
btnGreen = (Button) findViewById(R.id.btnGreen);
btnGreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#00ff00"));
}
});
btnBlue.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#006699"));
}
});
}
}

ButterKnife by Jake Wharton seems a good solution for your problem.

Related

Android Relative layout button crashes app

I'm making an app, now i want to make a button with an image and some text on it.
But every time I click the button my app crashes, android studio does compile however.
This is how i my xml code to make the layout of the screen.
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/buttonStartTest"
style="#android:style/Widget.Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView... />
<TextView.../>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code:
package com.example.user.ballbounceapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout StartTest = findViewById(R.id.buttonStartTest);
StartTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TestActivity();
}
});
}
public void TestActivity(){
Intent intent = new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
};
You may have forgot to declare your TestActivity inside your manifest.
More about activities in manifest here : https://developer.android.com/guide/topics/manifest/activity-element
So you will need something like this :
<activity android:name="TestActivity">
If the crash persists, the problem might be in your TestActivity 's OnCreate method.

Why do I get an error: cannot find symbol variable in R class

The following source code should make a picture and display it on the screen. But I get some errors. Cannot resolve symbol 'activity_fullscreen', the same for imageView1, dummy_button, fullscreen_content and fullscreen_content_controls.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
// Set up the user interaction to manually show or hide the system UI.
mContentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggle();
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);//dummy abaendern
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_fullscreen);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.dummy_button);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
Those are my imports:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.ImageView;
import android.R;
Now let us have a look at the activity_fullscreen.xml The missing parts are definitely there.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#0099cc"
tools:context="com.example.sasha.myapplication2.FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/dummy_button" />
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:src="#drawable/icon" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</FrameLayout>
</FrameLayout>
I already checked the recommendations in the following answer.
As far as I can see the names does not contain any illegal characters. I also already restarted the android studio going by File -> Invalidate Caches / Restart..
But I still did not find any solution to my problems. How is it posible that R can be resolved but not its content? Any ideas are welcome. Thanks
You imported the wrong 'R'
Remove this :
import android.R;
Add this :
import com.yourCompany.yourApp.R;
use this to import the mapping of your app.
import com.example.sasha.myapplication2.R;
You don't need to import the R File. This will remove the casting of R.Activity file. Clean the project to remove the error.

How to change size of a button by an onClick from another button?

I want to be able to click the "SizeClicker" button and change the size of the "Clicker" button. I am new to Java and Android Studios. I am working on an app. I want to be able to change the "Clicker" to whatever size I need be. If I am doing this 100% wrong could you help me change the size (The width and the height).
import android.app.Activity;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button Clicker = (Button) findViewById(R.id.Clicker);
Button Sizer = (Button) findViewById(R.id.SizeClicker);
Sizer.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
ViewGroup.LayoutParams params = Clicker.getLayoutParams();
params.height++;
params.width++;
Clicker.setLayoutParams(params);
}});
}
The XML:
<Button
style="?android:attr/buttonStyleSmall"
android:text="#string/SizeClickerText"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:id="#+id/SizeClicker"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/buttonshapesize"
android:layout_above="#+id/Clicker"
android:layout_toEndOf="#+id/Clicker" />
<Button
android:text="#string/ClickerButton"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:id="#+id/Clicker"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/ClickerDisplayText"
android:layout_marginBottom="50dp"
android:clickable="false" />
inside your onClick try
button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
http://android-coding.blogspot.in/2011/05/resize-button-programmatically-using.html
If your button is inside RelativeLayout, you should use RelativeLayout.LayoutParams
Programmatically change the width of the Button in android

Eclipse error : button1 cannot be resolved or is not a field

Here is my MainActivity.java code :
package com.dg.buttontest3;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("dg","button was clicked");
}
}
And here is the activity_main.xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="18dp"
android:text="Button" />
</LinearLayout>
I am getting the following error while running this code on Eclipse ADT : button1 cannot be resolved or is not a field.
I don't understand what is wrong with my code. Please help.
You haven't posted your XML code, but as this code seems to be working properly:
button = (Button)findViewById(R.id.button1);
I'd guess that in your activity_main.xml you never create something called button1, or that you don't use the #+id/ prefix.
It should look something like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
Just realised something else, you need to change your LinearLayout opening tag so it has a >.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
That might cause the button to never properly be recognised.

How to focus to the top of newly generated text in textView?

I have a simple jokes application. When you press the button you get new joke. If the previous one was bigger than the screen you can scroll it down. If you go down to the bottom and you go to the next joke, you are transfered to the bottom of the newly generated joke, but i want it to go to the top, and automatically display the start of the joke. How can i do this ?
I assume it would be done via the java code.
Thank you for your time.
Use the scrollTo(int x, int y) method, i like to have ScrollView around my TextView but i think the same thing will work for a TextView only. hope you understand!
Rolf
Example
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="130dp" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="long\n\n\n\n\n\n\n\n long\n\n\n\n\n\n\n very text here!" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="joke" />
</LinearLayout>
java:
package org.sample.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class AutoscrollActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button new_joke;
private TextView joke;
private ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new_joke = (Button) this.findViewById(R.id.button);
new_joke.setOnClickListener(this);
joke = (TextView) this.findViewById(R.id.text);
scroll = (ScrollView) this.findViewById(R.id.scroll);
}
#Override
public void onClick(View v) {
joke.setText("Long\n\n\n\n\n\n\n\n joke \n\n\n\n\n\n\n\nlong joke joke");
scroll.scrollTo(0, 0);
}
}

Categories

Resources