How to program button list with 3 different texts in each - android-studio-3.0

I'm trying to make a code to make buttons with 3 different texts in each.
Like so:
Text|Text
Text|
_________
Text|Text
Text|
_________
Text|Text
Text|
https://imgshare.io/image/wHXHd
The vertical lines are joined which means there are three sections to a button. How should I go about doing this? Any help would be appreciated

This is one of many ways you can do it :
activity_main.xml
<?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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</LinearLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_layout);
for (int i = 0;i<3;i++) {
LinearLayout ll1 = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll1.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(ll1, lp);
LinearLayout ll2 = new LinearLayout(this);
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll2.setOrientation(LinearLayout.VERTICAL);
ll1.addView(ll2, lp);
Button btn1 = new Button(this);
btn1.setText("1");
ll2.addView(btn1, lp);
Button btn2 = new Button(this);
btn2.setText("2");
ll2.addView(btn2, lp);
Button btn3 = new Button(this);
btn3.setText("3");
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll1.addView(btn3, lp);
}
}
Screenshot
You'll have to change it to fit your needs.
Good luck and let me know if you need help.

Related

Xamarin forms layout initialisation issue

I'm getting the following errors with my code
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
Error CS0029 Cannot implicitly convert type 'Android.Widget.GridLayout' to 'Android.Widget.LinearLayout'
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
Error CS0029 Cannot implicitly convert type 'Android.Widget.LinearLayout' to 'Android.Widget.GridLayout'
this is confusing me because they are both the correct layout types, and if I swap the layout types in my code, it builds fine but when run it throws almost the same exemption.
protected override void OnCreate(Bundle savedInstanceState) //run on startup.
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
}
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="9"
android:id="#+id/input"
tools:gridSpec="1|8|#0093eeff|K:#ee8700ff:16,l:72,l:16,r|S:#83ee00ff:16,0,l:16,0,r">
</GridLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/select">
</LinearLayout>
You have your types swapped.
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
it should be:
//FindViewById<LinearLayout>
LinearLayout selectMain = FindViewById<LinearLayout>(Resource.Id.select);
//FindViewById<GridLayout >
GridLayout inputMain = FindViewById<GridLayout>(Resource.Id.input);

Attempt to invoke virtual method 'void android.view.View.resolveLayoutParams()' on a null object reference

I have a ScrollView added to a FrameLayout. In a button click I remove the ScrollView from the FrameLayout and add an element derived from ViewGroup. If the button is clicked again the ViewGroup is removed and the ScrollView is added back. While adding the ScrollView back to the FrameLayout after removing the ViewGroup I get the following exception from the AddView method of the FrameLayout. Actually it is thrown from the AddView() method of ViewGroup class, since FrameLayout is derived from ViewGroup.
Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.resolveLayoutParams()' on a null object reference
occurred
I have already tried to toggle the visibility of both Views instead of adding one and removing the other. But after toggling visibility, the application crashes every time when the FrameLayout's OnLayout is called such as when the Android keyboard appears or if the device orientation changes.
I am sorry that I am not able to provide the code. Because my code is very complex and I could not reproduce this problem in a simple sample. I just posted this question as a last resort.
I know it is not possible to get solution if code is not included. But any input related to this problem will be helpful.
Since you cannot provide the code here, I can only write part of the code to show your requirements:
in xaml,i add a ScrolView (with its children) and a LinearLayout(which as your ViewGroup) into a FrameLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above = "#+id/click"
>
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text in scrollview"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text iFn LinearLayout"
/>
</LinearLayout>
</FrameLayout>
<Button
android:id = "#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
in activity show and hide the Scrolview and LinearLayout by click Button:
private int num = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button button = FindViewById<Button>(Resource.Id.click);
FrameLayout frameLayout = FindViewById<FrameLayout>(Resource.Id.framelayout);
ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scroll);
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout);
button.Click += delegate
{
if (num % 2 ==0)
{
scrollView.Visibility = Android.Views.ViewStates.Gone;
linearLayout.Visibility = Android.Views.ViewStates.Visible;
}
else
{
linearLayout.Visibility = Android.Views.ViewStates.Gone;
scrollView.Visibility = Android.Views.ViewStates.Visible;
}
num++;
};
}

Xamarin Android Widget: How to change Height of ImageView programmatical?

I have a widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
style="#style/WidgetBackground">
<ImageView
android:id="#+id/myimage"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/myimage" />
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/WidgetText.Title" />
How can I change the size of the image?
[Service]
public class UpdateService : Service
{
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
// changing textsize works perfectly:
updateViews.SetTextViewTextSize(Resource.Id.mytext, (int)ComplexUnitType.Sp, 14);
updateViews.SetViewVisibility(Resource.Id.myimage, ViewStates.Gone);
updateViews.SetTextViewText(Resource.Id.text, "new text");
// but changing imagesize does not work, I didn't find a right way to set the size, need something like this:
updateViews.SetInt(Resource.Id.myimage, "layout_width ???", 200dip ???);
ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(Widget)).Name);
AppWidgetManager manager = AppWidgetManager.GetInstance(this);
manager.UpdateAppWidget(thisWidget, updateViews);
}
updateViews has several setInt, setFloat, Set.. but I didn't find the right method. Any hint please?
It's impossible to change the height or width of the ImageView in code, but there is some workaround.
You could try to create a new widget layout that the size of your ImageView is what you want. When you call RemoteViews() use it instead of the original one. And when you update the widget, it will be redrawn.
For example:
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
//var updateViews = new RemoteViews(this.PackageName, Resource.Layout.widget);
//Using another layout instead.
var updateViews = new RemoteViews(this.PackageName, Resource.Layout.layout1);
ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(MyWidget)).Name);
AppWidgetManager manager = AppWidgetManager.GetInstance(this);
manager.UpdateAppWidget(thisWidget, updateViews);
///...
///...
}
Or you could also try to add manyImageView of different size in your layout and set the Visibility to Gone. Show the one you want to display in the code by calling SetViewVisibility().
You can try this method:
var videoContainer = FindViewById<RelativeLayout>(Resource.Id.local_video_container);
var parameters = videoContainer.LayoutParameters;
parameters.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerHeight, Resources.DisplayMetrics);
parameters.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerWidth, Resources.DisplayMetrics);
videoContainer.LayoutParameters = parameters;

YouTube Player not working with a Transparent Panel Menu Android

The YouTube Player will play videos no problem either without the transparent panel on the form or it will play them in full screen, the transparent panel has some images in it nothing special. If I take out the transparent panel, the YouTube Player works as desired, embedded in app. If I add the transparent panel to the form, this is when it will not play but in full screen. The video starts and then stops instantly. I assume it has something to do with the transparent panel but I can not understand what is happening. Any help or thoughts would be great. My java file does not change except for the initPopup would not be there. Shortened Java file version below.
XML Layout File Below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mlayout"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spnrPSA" >
</com.google.android.youtube.player.YouTubePlayerView>
</RelativeLayout>
<com.TransparentPanel
android:id="#+id/popup_window"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="left"
android:orientation="vertical"
android:padding="1px" >
<com.TransparentPanel>
</RelativeLayout>
JAVA File Below:
public final class PSA extends YouTubeFailureRecoveryActivity{
private Animation animShow, animHide;
private YouTubePlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.psa);
initPopup();
}
private void initPopup() {
final TransparentPanel popup = (TransparentPanel) findViewById(R.id.popup_window);
// Start out with the popup initially hidden.
popup.setVisibility(View.GONE);
if (PubVars.ScreenOrientation==0){
animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);
}
if (PubVars.ScreenOrientation==1){
animShow = AnimationUtils.loadAnimation( this, R.anim.l_popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.l_popup_hide);
}
//animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
//animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);
final ImageView showButton = (ImageView) findViewById(R.id.show_popup_button);
final ImageView hideButton = (ImageView) findViewById(R.id.hide_popup_button);
showButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.setVisibility(View.VISIBLE);
popup.startAnimation( animShow );
showButton.setEnabled(false);
hideButton.setEnabled(true);
}});
hideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation( animHide );
showButton.setEnabled(true);
hideButton.setEnabled(false);
popup.setVisibility(View.GONE);
}});
}
}
I must be missing something simple. Thanks in advance, I am fairly new to android.
Youtube Player View does not authorize overlays. Even if it's a transparent Layout(Relative, frame, ..).
The player stops the video and log into logcat some useful info. But this case is reported as "youtubeplayer is completely covered".
This api not permit anyone layer over this. "Layout is the problem"
I suggest you use the hierarchy View to see this on your layout.
I have many problems with it, because my layout stay over a little point over player.

TextView in PagerAdapter: scrollable in API15-17, not in API7

This FragmentActivity creates a few fragments that are put in a slider (horizontal. Only one fragment is shown here):
public class MainActivity2 extends FragmentActivity{
private ViewPager mPager;
private FragmentStatePagerAdapter mPagerAdapter;
private Logger mLogger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_container);
mLogger = new Logger();
mPager = (ViewPager)findViewById(R.id.pager);
mPagerAdapter = new SliderAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
... //goes on with activity, the adapter is set correctly..
The Logger fragment creates its view inflating this xml:
<TextView
android:id="#+id/LoggerLoTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:ems="10"
android:layout_weight="2"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000033"
android:textSize="16sp"
android:text="#string/logger"
android:layout_marginTop="30sp" />
<TextView
android:id="#+id/numberOutput"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_weight="13"
android:layout_height="fill_parent"
android:gravity="top"
android:freezesText="true"
android:maxLines = "50"
android:layout_marginLeft="22dp"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"/>
<Button
android:background="#80FFFFFF"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="fill_parent"
android:freezesText="true"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/logger_ad"
android:onClick="whatsNew"/>
</LinearLayout>
For API 7, everything works (all fragments are loaded and the slider works) fine except that the TextView "numberOutput" does not scroll and can't be selected. For API 15, 16, 17 it wors fine. Any idea?
Solved it by getting that TextView (numberOutput) at runtime and setting a movement method:
rootView = (ViewGroup) inflater.inflate(R.layout.dicelogger_lo, container, false);
mLoggerBody = (TextView)rootView.findViewById(R.id.numberOutput);
mLoggerBody.setMovementMethod(new ScrollingMovementMethod());
I did not find in the documentation that higher api levels automatically set any movement method.. so the why would be another question

Resources