How to set a RelativeLayout background color? - java

I am trying to set a RelativeLayout backgroundColor and I get cannot resolve symbol
here is my code
package com.example.butka.clickme;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
//LayoutParameters
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.CENTER_VERTICAL);
//button
Button btn = new Button(this);
btn.setText("Click me");
btn.setBackgroundColor(Color.WHITE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
colors();
}
});
//add stuff
layout1.addView(btn, params);
setContentView(layout1);
}
//void on button click
private void colors()
{
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0)
{
layout1.setBackgroundColor(Color.BLACK);
}
}
}
everything runs good, until the color void. the error is cannot resolve symbol But the interesting thing is that I can set the color using layout.setBackgroundColor() before the void.
So the question is, how do you set a layout backgroudColor?

Use this:
layout1.setBackgroundColor(ContextCompat.getColor(this, R.color.black));
or
layout1.setBackgroundColor(Color.parseColor("#000000"));

Your RelativeLayout is in the onCreate() method scope, you must move it to class scope. Like this:
public class MainActivity extends AppCompatActivity {
RelativeLayout layout1; // Make it class scope.
#Override
protected void onCreate(Bundle savedInstanceState) {
//set layout
super.onCreate(savedInstanceState);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setBackgroundColor(Color.BLACK);
...
}
// Then you can access it from other method:
private void colors() {
Random random = new Random();
short num1 = (short)random.nextInt(9);
if(num1 == 0) {
layout1.setBackgroundColor(Color.BLACK); // You can access it now.
}
}

Related

RunTime error in android studios, with no syntax errors. Created program programmatically

I am new to android studios and was tasked with creating an app programmatically. Depending on which button is pressed, the color description is displayed. I have a feeling it has something to do with the layout I created. Every time I run the program the app crashes and I cannot figure out why. Below is my code:
package com.example.hw3ex2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout layout;
LinearLayout.LayoutParams layoutParams;
String plumDescription = getResources().getString(R.string.plum_is);
String blueDescription = getResources().getString(R.string.blue_is);
String goldDescription = getResources().getString(R.string.gold_is);
String descriptionString;
TextView tv;
Button btnPlum;
Button btnBlue;
Button btnGold;
private int button_color = getResources().getColor(R.color.button_color);
private int background_color=getResources().getColor(R.color.background_color);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGUIByCode();
}
private void buildGUIByCode() {
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create textview
tv = new TextView(this);
tv.setLayoutParams(params);
layout.addView(tv);
//create buttons and add them to layout
btnPlum = new Button(this);
btnPlum.setTag("plum");
btnPlum.setText("Plum");
btnPlum.setLayoutParams(params);
layout.addView(btnPlum);
btnPlum.setOnClickListener(this);
btnPlum.setBackgroundColor(button_color);
btnBlue = new Button(this);
btnBlue.setTag("blue");
btnBlue.setText("Blue");
btnBlue.setLayoutParams(params);
layout.addView(btnBlue);
btnBlue.setOnClickListener(this);
btnBlue.setBackgroundColor(button_color);
btnGold = new Button(this);
btnGold.setTag("gold");
btnGold.setText("Gold");
btnGold.setLayoutParams(params);
layout.addView(btnGold);
btnGold.setOnClickListener(this);
btnGold.setBackgroundColor(button_color);
setContentView(layout);
}
#Override
public void onClick(View view) {
Object tag = view.getTag();
if ("plum".equals(tag)) {//get plum_is string and display in textView
tv.setText(plumDescription);
} else if ("blue".equals(tag)) {//get blue_is string and display in textview
tv.setText(blueDescription);
} else if ("gold".equals(tag)) {//get gold_is string and display in textview
tv.setText(goldDescription);
}
System.out.println(descriptionString);
}
}
I see that you're invoking getResources() before onCreate,
you need to move all of those initialization that uses getResources() within onCreate
public class MainActivity ... {
String plumDescription; // don't initialize here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plumDescription = getResources().getString(R.string.plum_is); // initialize after onCreate
}
}

Unknown class 'name'

I am new to Android Studio and trying to get used to it. I have tried to make a trivial app where the user could press a button and a random number would be generated. However, when I used the variables in the code it gave me "unknown class 'get'" and "unknown class 'random'" errors. Code is as follows:
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button RollButton = findViewById(id.RollButton);
final TextView resultsTextView = findViewById(id.ResultsTV);
final SeekBar seekBar = findViewById(id.seekBar);
RollButton.setOnClickListener(new onClickListener()
{
Random rnd = new Random(10);
private int random = rnd.nextInt(seekBar.getProgress());
CharSequence get = resultsTextView.getText();
get = String.valueOf(random);
});
}
}
Your OnClickListener looks wrong and you can't use private in this case. So try this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button RollButton = findViewById(id.RollButton);
final TextView resultsTextView = findViewById(id.ResultsTV);
final SeekBar seekBar = findViewById(id.seekBar);
RollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random rnd = new Random(10);
int random = rnd.nextInt(seekBar.getProgress());
CharSequence get = resultsTextView.getText();
get = String.valueOf(random);
}
});
}
}

I want to copy the content of my first activity (i.e the java code ) to my fragment on the click of a button

This is My activity on which there is a button (Source Code) and i want if i click on that button then it should paste all the content(i.e java code of the activity) to my fragment
This is my Fragment . I want to that my java code should be copied to java fragment and xml code should copy to xml fragment Only when i click the Source Code Button on my activity.
This is my Activity Code
package com.example.shivnandan.listview;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class main_act extends Activity
{
TextView t1 ,t2;
EditText e1;
Button b1,b2;
// String e;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_act_layout);
// e =( getIntent().getExtras().getString("main_act"));
t1 = (TextView)findViewById(R.id.textView3);
t2 = (TextView)findViewById(R.id.textView4);
e1 = (EditText)findViewById(R.id.editText);
b1 = (Button)findViewById(R.id.button);
b2 =(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(),main_act_2.class);
i.putExtra("name",(e1.getText().toString()));
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplication(),SourceCode_Activity.class);
startActivity(i);
}
});
}
}
This is My Fragment Activity
package com.example.shivnandan.listview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.app.Activity;
import android.widget.ZoomControls;
public class Fragment1 extends android.support.v4.app.Fragment {
ZoomControls z;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.frag1, null);
// return inflater.inflate(R.layout.frag1,container,false);
z = (ZoomControls) v.findViewById(R.id.zoomControls);
final TextView tv1 = (TextView) v.findViewById(R.id.textView7);
z.setOnZoomInClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
float x = tv1.getScaleX();
float y = tv1.getScaleY();
tv1.setScaleX((float) (x + 1));
tv1.setScaleY((float) (y + 1));
}
});
z.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
float x = tv1.getScaleX();
float y = tv1.getScaleY();
tv1.setScaleX((float) (x - 1));
tv1.setScaleY((float) (y - 1));
}
});
return v;
}
}
I dont think you can just copy the source code like that.
One easy way of achieving it would be by defining the strings which are the source codes of whatever you want to display and setting them appropriately as text to a textview in your fragment based on the button clicked.

How to set image resource in Android?

I am writing code for a poker game on eclipse and I am stuck on how to make the card clicked in the cardHand area show up in the playedCards area and have it removed from the deck I am drawing it from. Here's the code:
package com.viktorengineering.poker254;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class Poker extends Activity {
private ImageView mViewDeck;
private ImageView mViewHand;
private ImageView mViewPlayedCards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setBackgroundDrawableResource(R.drawable.green_back);
mViewDeck = (ImageView) findViewById(R.id.deckImage);
mViewDeck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int[] imagesArray = {R.drawable.clubs_ace, R.drawable.hearts_seven, R.drawable.diamonds_five,
R.drawable.clubs_three, R.drawable.hearts_eight, R.drawable.spades_six};
Random random = new Random();
mViewHand.setImageResource(imagesArray[new Random().nextInt(6)]);
}
});
mViewHand = (ImageView) findViewById(R.id.cardHand);
mViewHand.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPlayedCards.setImageResource(0);
mViewHand.setImageResource(0);
}
});
mViewPlayedCards = (ImageView) findViewById(R.id.playedCards);
}
}
Your Image in Drawable Folder means use bellow code
mViewPlayedCards.setImageResource(R.drawable.icon);
mViewHand.setImageResource(R.drawable.icon_home);

Issue with program ab

when using the ab.jar in eclipse I get the following error when trying to run the bot.
“Could not find class ‘org.alicebot.ab.Bot’, referenced from method…”. The chat class is being imported but the bot class cannot be imported.
Here is my code:
import org.alicebot.ab.Bot;
import org.alicebot.ab.Chat;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
String usertext; String response;
String botname="MAVIS";
Bot mavis=new Bot(botname);
Chat chat= new Chat(mavis);
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
final LinearLayout ll2 = (LinearLayout) findViewById(R.id.ll2);
final ScrollView scv = (ScrollView) findViewById(R.id.sv);
final Button btn = (Button) findViewById(R.id.button1);
final EditText medit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
TextView tvu=new TextView(v.getContext());
TextView tvb=new TextView(v.getContext());
TextView tvut=new TextView(v.getContext());
TextView tvbt=new TextView(v.getContext());
TextView tvdivider1=new TextView(v.getContext());
TextView tvdivider2=new TextView(v.getContext());
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tvu.setLayoutParams(lparams);
tvb.setLayoutParams(lparams);
tvut.setLayoutParams(lparams);
tvbt.setLayoutParams(lparams);
tvdivider1.setLayoutParams(lparams);
tvdivider2.setLayoutParams(lparams);
usertext = medit.getText().toString();
if(usertext.trim().length() != 0){
ll1.addView(tvu);
ll1.addView(tvb);
ll2.addView(tvut);
ll2.addView(tvbt);
ll1.addView(tvdivider1);
ll2.addView(tvdivider2);
response=chat.multisentenceRespond(usertext);
tvu.setText("User");
tvb.setText(botname);
tvbt.setText(" : "+ response);
tvut.setText(" : "+ usertext);
medit.setText(" ");
tvdivider1.setText(" ");
tvdivider2.setText(" --------------------");
}
else{
//do nothing
}
}
});
scv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
scv.post(new Runnable() {
public void run() {
scv.fullScroll(View.FOCUS_DOWN);
}
});
}
});
}
}
I have imported the external library and defined the classpath for it. I have also copied the ab.jar in my project folder and even defined the classpath for it. But nothing seems to be working. Am I doing this wrong or are there more libraries that are needed for this to work. Anybody have a solution to my problem?
Your bot name here is MAVIS..did you change the folder name from the bots.zip???else change it..or simply use the predefined bot names(alice2 etc).
Thankz

Categories