Problem:
Place the status TextView and the TicTacToe5x5View2D into a vertical LinearLayout and set the layout as the content view of the activity.
I tried to use setGravity; however, it throws me an error. Is there another way to make it a linearlayout?
private TicTacToe5x5Game game;
private TicTacToe5x5View2D gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
status = new TextView(this);
status.setGravity(Gravity.CENTER_VERTICAL);
status.setTypeface(Typeface.MONOSPACE);
startGame();
setContentView(status);
gameView = new TicTacToe5x5View2D(this);
}
Any help would be greatly appreciated.
Related
I'm still new to Android Studio, and for practice I want to make a simple To-Do list app. I'm troubles with creating a new TextView and displaying it.
I know that I probably need to manually insert the TextView into the layout, but I have no idea how to do that.
Here's my MainActivity code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button submitBtn;
EditText userInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitBtn = (Button)findViewById(R.id.submitBtn);
userInput = (EditText)findViewById(R.id.userInput);
submitBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.submitBtn) {
String userInputString = userInput.getText().toString();
TextView listItem = new TextView(this);
listItem.setText(userInputString);
}
}
}
I am using ConstraintLayout for my layouting. If somebody could help guide me to the right direction, that would be greatly appreciated, thanks in advance!
In the simplest case, you get your ConstraintLayout using its ID and then call addView(View) with your TextView as the parameter on it.
Assuming your ConstraintLayout has the id myLayout:
ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.myLayout);
layout.addView(listItem);
I have made image slider using viewPager and picasso. Images are loaded from my own(raw). I've put share button below image.
I want to make specific image be shared using share intent. I mean, if I'm in image of position 3 then only image of position 3 should be shared.
Activity where image slider is shown:
public class RajeshDaiActivity extends AppCompatActivity{
ViewPager viewPager;
private int[] imageUrls = new int[]{
R.drawable.oq,
R.drawable.oqqqq,
R.drawable.opt3
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rajesh_dai);
viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView ShareButton = findViewById(R.id.share);
ShareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent share = new Intent("android.intent.action.SEND");
//?? how??
}
});
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
}
From the code posted above, it looks like you need to implement an interface and capture the click event of the particular image and implement the logic for redirection in overridden implementation of the particular function in your fragment class. Hope this clarifies your question.
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.
I have a problem with creating a working Layout in View class that will not crush the program. I think everything is done correctly in the code, but probably there are some setting I don't know about I hope that You guys will reveal that to me.
I am trying to do drawing View with a "Clear" button and RadioGroup.
So first od all, that works fine, but both added content Views (btnReset = "Clear", btnScale = RadioGroup) are hoovered on each other. That is why I want to add one parent Layout, so I can easily manage these Views.
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnScale, tv.paramsRadio);}
DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
I omit most of the code because I think its irrelevant, everything works fine but the layout.
Anyway, when I try to create a layout, I simply add a little code:
Changed File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.layout, tv.params); }
Changed DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RelativeLayout layout;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(btnReset, params);
layout.addView(btnScale, paramsRadio);
I assume that should work like charm, but unfortunately, when I try to do anything with newly created layout, my app crashes. What is wrong with that code?
Well, after quite long fight I managed to solve my own problem :D I did not do it, like #pskink suggested, because honestly for me it was too advanced. I found much simpler solution.
The thing is, You can create as much views as You want in DrawView.java, but sort them in Layout created in File1.java!
Just like that:
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
DrawView tv = new DrawView(this);
ll.addView(tv.btnReset, tv.params);
ll.addView(tv.btnScale, tv.paramsRadio);
ll.addView(tv);
setContentView(ll);
And DrawView.java stays the same:
public class DrawView extends View {
public Button btnReset;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);